Created
February 5, 2013 14:47
-
-
Save pfirpfel/4714886 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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