Skip to content

Instantly share code, notes, and snippets.

@anupsavvy
Created July 25, 2011 23:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anupsavvy/1105559 to your computer and use it in GitHub Desktop.
Save anupsavvy/1105559 to your computer and use it in GitHub Desktop.
Simple Quicksort ( this is not a randomized version ).
public class Sort{
private int[] arr = null;
public Sort(int[] arr) throws SortException{
this.arr = arr;
if(this.arr == null)
throw new SortException("Null array found");
}
/*
* Considering first element of the array as pivot.
*/
public void quickSort(int pivot, int high){
if(pivot < high){
int hold = 0;
int i = pivot;
for (int j = pivot+1; j <= high ; j ++){
if(this.arr[pivot] <= this.arr[j]){
hold = this.arr[i+1];
this.arr[i+1] = this.arr[j];
this.arr[j] = hold;
i++;
}
}
hold = this.arr[i];
this.arr[i] = this.arr[pivot];
this.arr[pivot] = hold;
/*
* make recursive calls
*/
quickSort(pivot, i-1);
quickSort(i+1,high);
}
}
}
class SortException extends RuntimeException{
public SortException(String message){
super(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment