Skip to content

Instantly share code, notes, and snippets.

@neer2808
Created April 23, 2019 17:34
Show Gist options
  • Save neer2808/1ee3467c2ad4236a0439a5176ec53f21 to your computer and use it in GitHub Desktop.
Save neer2808/1ee3467c2ad4236a0439a5176ec53f21 to your computer and use it in GitHub Desktop.
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