快速排序思想是选择一个中轴(基础值),选择一个结束值,而后从结束值往前遍历,若发现有比基础值小,则中止遍历,记录当前下标;接着从起始值向前遍历,若发现有比基础值大,则中止遍历,记录下标,而后交换刚刚取得两个下标对应的值;最后继续遍历,直到起始遍历下标大于等于结束遍历下标.最后将当前起始下标的值和中轴的值交换.这就是一趟快速排序,而后比较左边已经比较好的比中轴值小的队列,右边同理.ui
public static void quickSort(int[] sort, int low, int high) {
int i,j,base,temp;
if(low > high) {
return;
}
i = low;
j = high;
base = sort[low];
while(i < j) {
while(i < j && base <= sort[j]) {
j--;
}
while(i < j && base >= sort[i]) {
i++;
}
if(i < j) {
temp = sort[j];
sort[j] = sort[i];
sort[i] = temp;
}
}
sort[low] = sort[i];
sort[i] = base;
for(int ii = 0; ii <= sort.length - 1 ; ii++) {
System.out.print(sort[ii] + " ");
}
System.out.println("========================");
quickSort(sort, low, j-1);
quickSort(sort, j+1, high);
}排序