Created
April 23, 2019 17:33
-
-
Save neer2808/f3982bde31a1804004d10d2e210a9685 to your computer and use it in GitHub Desktop.
This file contains 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 Quick { | |
public static void main(String[] args) { | |
int[] intarray = {20,35,-15,7,55,1,-22}; | |
quickSort(intarray, 0,intarray.length); | |
for(int i = 0;i<intarray.length;i++) | |
{ | |
System.out.println(intarray[i]); | |
} | |
} | |
public static void quickSort(int[] input, int start, int end) | |
{ | |
if(end-start <2) | |
{ | |
return; | |
} | |
int pivotIndex = partition(input, start,end); | |
quickSort(input,start,pivotIndex); | |
quickSort(input,pivotIndex+1,end); | |
} | |
public static int partition(int[] input, int start, int end) | |
{ | |
int pivot = input[start]; | |
int i = start; | |
int j = end; | |
while(i<j) | |
{ | |
// empty loop body | |
while(i<j && input[--j]>= pivot); | |
if(i<j){ | |
input[i] = input[j]; | |
} | |
// empty loop body | |
while(i<j && input[++i] <= pivot); | |
if(i<j){ | |
input[j] = input[i]; | |
} | |
} | |
input[j] = pivot; | |
return j; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment