Skip to content

Commit

Permalink
Merge pull request #130 from EnzoRobaina/master
Browse files Browse the repository at this point in the history
Ref Issue #117 | Added quickSort in java
  • Loading branch information
ambujraj authored Oct 1, 2018
2 parents 721d072 + 96cdfe7 commit 5a6ba05
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions sorting/quickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class QuickSort{
//Quick Sort
//To call, use QuickSort.quickSort(arr, 0, arr.length-1);
public static int quickSort(int A[], int start, int end)
{
int pivot = A[end];
int i = (start-1);
int exchanges = 0;
int comparisons = 0;

for (int j=start; j<end; j++){
comparisons++;

if (A[j] <= pivot){
comparisons++;
i++;
int temp = A[i];
A[i] = A[j];
A[j] = temp;
exchanges += 3;
}
}

int temp = A[i+1];
A[i+1] = A[end];
A[end] = temp;
exchanges += 3;

System.out.println("Exchanges: "+exchanges+" Comparisons: "+comparisons);
return i+1;
}

//Main quickSort function
public static void quickSortMain(int A[], int start, int end){

if (start < end){

int pi = quickSort(A, start, end);

quickSortMain(A, start, pi-1);
quickSortMain(A, pi+1, end);
}
}

}

0 comments on commit 5a6ba05

Please sign in to comment.