public class 快排 {
public static void main(String[] args) {
int numbers[] = {10,20,15,0,6,7,2,1,-5,55};
quickSort(numbers, 0, numbers.length - 1);
for (int i = 0; i < numbers.length; i++) {
System.out.print(numbers[i] + "\t");
}
}
public static int getMiddle(int[] numbers, int low, int high){//记录中轴
int temp = numbers[low];
while(low < high){
while((numbers[high] >= temp) && (low < high))
high --;
numbers[low] = numbers[high];
while((numbers[low] <= temp) && (low < high))
low ++;
numbers[high] = numbers[low];
}
numbers[high] = temp;
return high;
}
public static void quickSort(int[] numbers,int low,int high){
if(low >= high){
return;
}
int index = getMiddle(numbers, low, high);
quickSort(numbers, low, index - 1);
quickSort(numbers, index + 1, high);
}
}