Skip to content

Instantly share code, notes, and snippets.

@digvijaybhakuni
Created September 28, 2014 19:39
Show Gist options
  • Save digvijaybhakuni/157c166aea68ba78dadb to your computer and use it in GitHub Desktop.
Save digvijaybhakuni/157c166aea68ba78dadb to your computer and use it in GitHub Desktop.
package com.learn.dbhakuni.ds;
import java.util.Arrays;
/**
* Created with IntelliJ IDEA.
* User: dbhakuni
* Date: 28/9/14
* Time: 10:04 PM
* To change this template use File | Settings | File Templates.
*/
public class QuickSort {
private static int partationIt(int[] arr, int s, int e){
int pivot = arr[e];
int t = e;
int b = s-1;
boolean w = true;
while (w){
while (w){
b++;
if(t==b) {
w = false;
break;
}
if(arr[b] > pivot ){
arr[t] = arr[b];
break;
}
}
while (w){
t--;
if(t==b) {
w = false;
break;
}
if(arr[t] < pivot ){
arr[b] = arr[t];
break;
}
}
arr[t] = pivot;
}
return t;
}
public static int[] quickSort(int[] arr, int s, int e){
if(s < e){
System.out.println("sort ... ");
int split = partationIt(arr,s,e);
quickSort(arr, s, split-1);
quickSort(arr, split+1, e);
}
return arr;
}
public static void main(String[] a){
int[] arr = new int[]{25,56,14,96,1,66,36,74,12,10};
quickSort(arr,0, arr.length-1);
for (int i : arr){
System.out.print(i+" ");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment