0

I'm coding a quick sort in Java, and I've completed the partitioning portion, but for some reason, my program keeps running forever with:

x was swapped with x

where x is some random integer, but the same integer every time it is printed. I've tried to debug this, but it runs fine every time in the debugger. What am I doing wrong? Here's my code excluding the parent class, array generator, etc.

class QuickSort extends BasicSort {

public QuickSort() {
    super("QuickSort");
}

public void sort() {
    int pivot = data[data.length / 2];
    int low = 0, high = data.length - 1;

    while (true) {
        while (low < (data.length - 1) && data[low] < pivot) {
            System.out.println(data[low] + " is less than " + pivot);
            low++;
        }
        while (high > 0 && data[high] > pivot) {
            System.out.println(data[high] + " is greater than " + pivot);
            high--;
        }

        if (low >= high) {
            break;
        } else {
            int temp = data[low];
            data[low] = data[high];
            data[high] = temp;

            System.out.println(data[low] + " was swapped with " + data[high]);
        }
    }
}
}
Jason Khalili
  • 101
  • 1
  • 9
  • 2
    Put A print statement inside the `if block` to check if that condition is being satisfied. – Mathews Mathai Apr 22 '16 at 01:11
  • 4
    Possible duplicate: http://stackoverflow.com/questions/20355634/quicksort-infinite-loop-if-there-are-repeating-values – Nier Apr 22 '16 at 01:14
  • Yup, that's what it seems to be. It keeps printing when the values are the same. It's just strange how it rarely happened in the debugger and never when i ran it regularly. – Jason Khalili Apr 22 '16 at 01:18
  • @JasonKhalili That's weird, I tested you code in my debugger and it still gets into a infinite loop. Can you describe more about how you debug your program? – Nier Apr 22 '16 at 01:20

1 Answers1

1

This is an issue with identical numbers, I have to write code for that condition. Thanks guys.

Jason Khalili
  • 101
  • 1
  • 9