快速排序
public class QuickSort { public static int getMidIndex(int[] list, int low, int high) { int temp = list[low]; while(low < high) { while(low<high && list[high] >= temp) { high --; } list[low] = list[high]; while(low<high && list[low] <= temp) { low ++; } list[high] = list[low]; } list[low] = temp; return low; } public static void quickSort(int[] list, int low, int high) { //递归一定要注意递归结束的条件,否则就是死循环 if(low < high) { int middle = getMidIndex(list, low, high); quickSort(list, low, middle-1); quickSort(list, middle+1, high); } } public static void main(String[] args) { int[] list = { 8, 23,6, 6,7,7,3,4,0,9, 17}; quickSort(list, 0, list.length-1); for(int i = 0 ; i < list.length; i++) { System.out.print(list[i] + " "); } } }
做人第一,做学问第二。