0

I have written the Java code below after reading up on quick sort. There are no errors when running the code but the quick sort does not sort the array. It just returns the original unsorted array. I appreciate any leads.

public class quickSort {

public void quickSort(int array[], int low, int high){
    if(low<high){
        int pivot=partition(array, low, high);
        quickSort(array, low, pivot-1);
        quickSort(array, pivot+1, high);
    }
}
private int partition(int array[], int low, int high){
    int pivot=array[high];
    int i=low-1;

    for (int j = low; j < high; j++) {
        if(array[j]<pivot){
            i++;
            swap(array[i], array[j]);
        }
    }
    swap(array[i+1], array[high]);
    return i+1;
}
private  void swap(int a , int b){

    int temp=a;
    a=b;
    b=temp;
}

public static void main(String[] args){
    int a[]={4,6,3,7,9,0};
    quickSort q=new quickSort();
    q.quickSort(a, 0, a.length-1);

    for(int i=0;i<a.length;i++){
        System.out.print(a[i]+" ");
    }
}
}
Ashraf.Shk786
  • 618
  • 1
  • 11
  • 23
terra-firma
  • 65
  • 2
  • 7
  • 5
    Well, one problem is that your `swap` method does nothing. – Eran Aug 15 '17 at 05:24
  • The 2 ints passed into swap are values not references. You need to reassign to the target indexes in the array (i.e. inline the swap into your partition method). – charles-allen Aug 15 '17 at 05:26

0 Answers0