package q;
public class QuickSort<E extends Comparable> {
public static void quickSort(Integer [] list){
quickSort(list,0,list.length-1);
}
private static void quickSort(Integer[] list,Integer first,Integer last){
if(last > first){
Integer pivotIndex = partition(list,first,last);
quickSort(list,first,pivotIndex - 1);
quickSort(list,pivotIndex + 1,last);
}
}
private static Integer partition(Integer[] list,Integer first,Integer last){
Integer pivot = list[first];
Integer low = first +1;
Integer high = last;
while(high > low){
while (low <= high && list[low] <= pivot)
low++;
while (low <= high && list[high] >= pivot)
high--;
if(high > low){
int temp = list[high];
list[high] = list[low];
list[low] = temp;
}
}
while(high > first && list[high] >= pivot)
high--;
if(pivot > list[high]){
list[first] = list[high];
list[high] = pivot;
return high;
}
else{
return first;
}
}
}
快速排序
最新推荐文章于 2021-07-24 23:27:25 发布