Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7029819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:35:18+00:00 2026-05-28T00:35:18+00:00

public class Main { public static void main (String args[]) { int nums[]= {2,

  • 0
public class Main
{

  public static void main (String args[])
  {
    int nums[]= {2, 3, 4, 5, 6, 7, 8, 9, 0, 1};
    for (int index = 0; index < 10; index++)
      System.out.print("  " +nums[index]);;
    System.out.println();

    quickSort(nums,0,9);
    for (int index = 0; index < 10; index++)
      System.out.print("  " +nums[index]);
    System.out.println();
  }


  private static void swap(int a[], int lft, int rt)
  {
    int temp;
    temp = a[lft];
    a[lft] = a[rt];
    a[rt] = temp;
  }


  public static int pivot(int firstpl, int lastpl)
  {
    if(firstpl >= lastpl)
      return -1;
    else
      return firstpl;
  }



  private static void quickSort(int a[], int first, int last)
  {
    int left,right;  
    int pivindex = pivot(first,last);
    if(pivindex >= 0)
    {
      left = pivindex +1;
      right = last;
      do
      {
        while ( a[left] < a[pivindex] && left <= right )
          left++;
        while (a[right] > a[pivindex])
          right--;
        if (right > left)
          swap(a,left,right);
      }
      while(left < right);

       swap (a, pivindex, right);
       quickSort( a, first, right-1);
       quickSort(a, right+1, last);

    }
  }      
}

That is the code for a quick sort. Everything works right if the array’s 0 position number is “2” but if it is anything else, it throws an exception.

For example, if I put this in for array:

5 6 8 9 7 8 0 1 8 2

It gives the following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at Main.quickSort(Main.java:48)
    at Main.quickSort(Main.java:59)
    at Main.quickSort(Main.java:59)
    at Main.main(Main.java:12)

Please help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:35:19+00:00Added an answer on May 28, 2026 at 12:35 am

    Suppose that first = 8 and last = 9 in one of the calls to quickSort(). Suppose also that a[8] > a[9]. Think about what will happen in the left++ loop…

    (Remember, the a[left] < a[pivindex] part of the condition is checked before the left <= right part)

    OK, try walking through the method:

    Starting with first = 8, last = 9, I end up with pivindex = 8, left = 9, right = 9. I check the while condition:

    while(a[9] < a[8] && 9 <= 9)
    

    It’s true, so I increment left. Now left = 10 and right = 9. I check the while condition again…

    while(a[10] < a[8] && 10 <= 9)
    

    … and go off the end of the array. Now, if only I had checked that 10 <= 9 before looking at a[left]!

    OK, if you’re still stuck I’ll just come right out and tell you. The problem in that loop is that you are trying to access a[left] before checking if left is too big. You can fix the problem in one of two ways:

    1. Switch the order of the conditions, so that you check left first, or
    2. Change the <= to a <, so that left never gets bigger than right.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import java.lang.Math; public class NewtonIteration { public static void main(String[] args) { System.out.print(rootNofX(2,9)); }
public class Main { public static void main(String[] args) { int[] x = {1,
package javaapplication8; public class Main { public static void main(String[] args) { int[] list1
package hw3; public class Main { public static void main(String[] args) { final int
public class Test { public static void main(String[] args) { } } class Outer
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class WrapperTest { public static void main(String[] args) { Integer i = 100;
public class Main3 { public static void main(String[] args) { Integer min = Integer.MIN_VALUE;
public class b { public static void main(String[] args) { byte b = 1;
Example: public class TestClass { public static void main(String[] args) { TestClass t =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.