您的位置:首页 > 其它

快速排序

2015-09-02 18:22 246 查看
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;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: