Java实现快速排序

public class QuickSort {

public static void quickSort(int [] list){
	quickSort(list, 0, list.length-1);
}
public static void quickSort(int[] 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 int partition(int[] list, int first, int last){
	int pivot = list[first];
	int low = first;
	int high = last;
	while(high>low){
		while(low<high&&list[high]>=pivot) high--;
		list[low] = list[high];
		while(low<high&&list[low]<=pivot) low++;
		list[high] = list[low];
		
	}
	list[low] = pivot;
	return low;
}
//测试
public static void main(String[] args) {
	int [] list = {2,3,2,5,6,1,-2,3,14,12};
	quickSort(list);
	for(int num:list)
		System.out.print(num+" ");

}

}

posted @ 2018-05-07 22:03  Loading~  阅读(139)  评论(0编辑  收藏  举报