2019.2.23
public static <T extends Comparable<T>> void quickSort(T[] list){
quickSort(list, 0, list.length-1);
}
public static <T extends Comparable<T>> void quickSort(T[] list, int first, int last){
if(last > first){
int pivotIndex = partition(list,first, last);
quickSort(list, first, pivotIndex-1);
quickSort(list, pivotIndex+1, last);
}
}
public static <T extends Comparable<T>>int partition(T[] list, int first, int last){
T pivot = list[first];
int low = first + 1;
int high = last;
while(high > low){
while(low <= high && list[low].compareTo(pivot) <= 0)
low++;
while(low <= high && list[high].compareTo(pivot) > 0)
high--;
if(high > low){
T tmp = list[high];
list[high] = list[low];
list[low] = tmp;
}
}
while (high > first && list[high].compareTo(pivot) >= 0)
high--;
if(pivot.compareTo(list[high]) > 0){
list[first] = list[high];
list[high] = pivot;
return high;
}else{
return first;
}
}