0
package sort;

public class QuickSort {


    public static void quickSort(int[] arr, int start, int end) {
        if (start >= end) {    // if only one element
            return;
        }

        int key = start;   // first element is pivot
        int i = start + 1;
        int j = end;
        int temp;

        while(i <= j) {   // repeat until cross
            while(arr[i] <= arr[key]) {  // move right until bigger than key
                i++;
            }
            while(arr[j] >= arr[key] && j > start) {  // move left until smaller than key
                j--;
            }
            if(i > j) {  // swap if its crossed
                temp = arr[j];
                arr[j] = arr[key];
                arr[key] = temp;
            } else {
                temp = arr[j];
                arr[j] = arr[i];
                arr[i] = temp;
            }
        }

        quickSort(arr, start, j - 1);
        quickSort(arr, j + 1, end);

    }

    public static void main(String[] args) {

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

        quickSort(arr, 0, number-1 );
    }
}

I tried to learn Quick Sort. This is my code. and keep getting this message

What went wrong:

Execution failed for task ':QuickSort.main()'.

Process 'command 'C:/jdk1.8/bin/java.exe'' finished with non-zero exit value 1

I don't understand why I keep getting fail. I watched youtube and followed every step.

coding cat
  • 23
  • 4
  • What is the error if you disable Gradle delegation and run the app directly via this option: https://i.imgur.com/HpeUaUE.png? – CrazyCoder Mar 05 '21 at 23:18
  • When I run your code from the command line I get: `Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at QuickSort.quickSort(QuickSort.java:15) at QuickSort.quickSort(QuickSort.java:33) at QuickSort.main(QuickSort.java:42)` – Stephen C Mar 06 '21 at 00:49
  • You should be able to diagnose this based on https://stackoverflow.com/questions/5554734 – Stephen C Mar 06 '21 at 00:52

0 Answers0