0

I have written code of Quick Sort but it is showing Exception Array Index Out of bound.This works properly when written in C but in Java it is not working.Please tell what is the problem with it.Any Suggestion is appreciated.It is two way partition standard CLRS algorithm.

package Sorting;

public class QuickSort {

    public static void main(String arg[]) {
        int a[] = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };

        Sort(a, 0, a.length - 1);
        for (int i = 0; i < 9; i++) {
            System.out.print(" " + a[i]);
        }

    }

    private static void Sort(int[] a, int i, int j) {
        // TODO Auto-generated method stub
        if (i < j) {
            int k = Partition(a, i, j);
            Sort(a, i, k - 1);
            Sort(a, k + 1, j);

        }
    }

    private static int Partition(int[] a, int i, int j) {
        // TODO Auto-generated method stub
        int temp;
        int x = a[j];
        int l = i - 1;
        for (int n = i; n < j; n++) {
            if (a[n] >= x) {
                l++;
                temp = a[l];
                a[l] = a[n];
                a[n] = temp;
            }
        }
        temp = a[x];
        a[x] = a[l + 1];
        a[l + 1] = temp;
        return l + 1;

    }
}

Output: 9 3 5 7 4 1 6 2 8

6
  • Where does the error occur? Commented Jun 26, 2014 at 13:22
  • I dont want to spend time going into the logic here but the error is in your for loop in partition. for (int n = i; i < j; n++) int i never gets incremented in the loop so this will run forever Commented Jun 26, 2014 at 13:26
  • At face value, Partition can't work. See the for loop. The condition is i < j. Neither i nor j changes in the body. Once execution enters, it's the Hotel California: "You can check out any time you like, but you can never leave." Commented Jun 26, 2014 at 13:26
  • @user3728933 I dont think you fully understand the quick sort algorithm. Your code doesnt look like its choosing a pivot to sort around at all/correctly. Here is a good link which explains how quick sort works youtube.com/watch?v=y_G9BkAm6B8 Commented Jun 26, 2014 at 14:46
  • Now your post says there is an out of bounds error and also gives output. Which is it? Assuming there is no more bounds error, I suggest you insert trace code (System.err.print), to figure out why Partition is not working. You will never learn if we debug your program for you. Commented Jun 26, 2014 at 15:46

3 Answers 3

2

there are some minor mistakes in your code, here is the updated code:

    private static int Partition(int[] a, int i, int j) {
    // TODO Auto-generated method stub      
    int temp;
    int x = a[j];
    int l = i - 1;
    for (int n = i; n < j; n++) {
        if (a[n] <= x) {      /*******Condition was wrong******/
            l++;
            temp = a[l];
            a[l] = a[n];
            a[n] = temp;
        }
    }
    temp = a[j];      /******a[j] not a[x] x is value of pivot element******/
    a[j] = a[l + 1];
    a[l + 1] = temp;
    return l + 1;
}
Sign up to request clarification or add additional context in comments.

Comments

1

In this loop in partition:

for (int n = i; i < j; n++)

You probably meant n<j than i<j. i never changesm so i<j is always true. n eventually becomes 9. a[9] does not exist, so the exception.

3 Comments

Now it is showing wrong output ,exception is removed.
Exception is removed = your question is answered. Now you need to debug why it is not working :-) Please post any specific questions and we can help.
Okk but question is updated now.If you can help please do.Effort is appreciated.
1

i is always lower than j bacause i anj doesn't change

for (int n = i; i < j; n++)

so you have an endless loop.

5 Comments

No, because a[n] will cause an exception as soon as n>=a.size()
@ammoQ That is right. But n > a.size() because of the endless loop.
@user3728933 The question "why is my code not working" is off-topic.
@Jenes Not Working Output is 1 3 5 4 6 7 2 8 9

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.