Skip to content

Instantly share code, notes, and snippets.

@pfirpfel
Created February 5, 2013 14:47
Show Gist options
  • Save pfirpfel/4714886 to your computer and use it in GitHub Desktop.
Save pfirpfel/4714886 to your computer and use it in GitHub Desktop.
public class QuickSortMSP {
private static int noOfSwaps = 0;
public static void main(String[] args) {
int arr[] = {5,5,5,5,5};
sort(arr);
System.out.print("Swaps: "+noOfSwaps);
}
public static void sort(int[] arr){
qsort(arr, 0, arr.length - 1);
}
private static void qsort(int[] arr, int left, int right){
if(left < right){
int p = arr[(left+right)/2];
int l = left;
int r = right;
while(l < r){
while(arr[l] < p) l++;
while(arr[r] > p) r--;
if(l <= r){
noOfSwaps++;
int t = arr[l]; arr[l] = arr[r]; arr[r] = t;
l++; r--;
}
}
qsort(arr, left,r); qsort(arr,l,right);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment