Created
July 25, 2011 23:45
-
-
Save anupsavvy/1105559 to your computer and use it in GitHub Desktop.
Simple Quicksort ( this is not a randomized version ).
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 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