Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static <T extends Comparable<? super T>> void quickSort(T[] data, int min, int max) {
- int indexofpartition;
- if (max > min) {
- /** Create partitions */
- indexofpartition = findPartition(data, min, max);
- /** Sort the left side */
- quickSort(data, min, indexofpartition - 1);
- /** Sort the right side */
- quickSort(data, indexofpartition + 1, max);
- }
- }
- private static <T extends Comparable<? super T>> int findPartition(T[] data, int min, int max) {
- int left, right;
- T temp, partitionelement;
- int middle = (min + max) / 2;
- partitionelement = data[middle]; // use middle element as partition
- left = min;
- right = max;
- while (left < right) {
- /** search for an element that is > the partitionelement */
- while (data[left].compareTo(partitionelement) < 0) {
- left++;
- }
- /** search for an element that is < the partitionelement */
- while (data[right].compareTo(partitionelement) > 0 ) {
- right--;
- }
- /** swap the elements */
- temp = data[left];
- data[left] = data[right];
- data[right] = temp;
- /** let left skip elements with the same value as the partition element */
- if(data[left].compareTo(data[right]) == 0
- && left != right){
- left++;
- }
- }
- return right;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement