1

I am quite new to programming and I am just starting using java. My task was to write a program using quick sort, I managed to write it but it always gives me an index out of bounds. Could anyone take a look at my code and help me by identifying what I am doing wrong? Thanks

This is the code for the main class.

package quicksort;

public class Quicksort {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

    int[] x = {5,3,10,1,9,8,7,4,2,6,0};

    quicksort_class q = new quicksort_class(x);

    q.sort();

    for(int i = 0; i < 11-1; i++)
    {
        System.out.println(x[i]);
    }
   }
 }

This is the code for quicksort_class.

public class quicksort_class {

int[] array1 = new int[11];


public quicksort_class(int[] w)
{
    array1 = w;
}
public void partitionstep(int leftlimit, int rightlimit)
{   
    int LPointer = leftlimit;

    int RPointer = rightlimit;

    Random random = new Random();

    int midpoint = random.nextInt(11);

    int checknumber =  array1[midpoint];

    while(LPointer < RPointer)
    {
        while(array1[LPointer] <= checknumber)
        {
            LPointer = LPointer + 1;
        }

        while(array1[RPointer] >= checknumber)  
        {
            RPointer = RPointer --;
        }

     swap(LPointer, RPointer);

     partitionstep(leftlimit, midpoint - 1);

     partitionstep(midpoint + 1, rightlimit);
    }
}

public void swap(int x, int y)
{
int temp = array1[x];

array1[x] = array1[y];

array1[y] = temp;
}

public void sort()
{
partitionstep(0, array1.length - 1);
}
}
6
  • 2
    Where are you getting an index out of range exception? Please print the actual error message. Commented Feb 5, 2014 at 16:03
  • Can't the randomization of midpoint going to give you a point outside of the boundaries? Commented Feb 5, 2014 at 16:11
  • @Khanser It shouldn't because its staying within the bounds of the array. nextInt(11) will return a random value from [0 - 11). The array will always be size 11. As they partition, however, they should be adjusting the range that they're looking at (getting smaller than 11). Commented Feb 5, 2014 at 16:12
  • @bblincoe I meant outside of the boundaries of leftLimit and rightLimit. Commented Feb 5, 2014 at 16:14
  • General question: In your main, why are you looping for 11-1? Why not simply loop until x.length? Commented Feb 5, 2014 at 16:14

1 Answer 1

3

Your midpoint value should be calculated based on your leftLimit and rightLimit. It should not be a random value based off of the fixed value 11.

Sign up to request clarification or add additional context in comments.

1 Comment

Correct, updated wording. I believe they need to base their random value on the current size of the limited array (based on left and right limit). They are partitioning the array after all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.