/*快排(重写比较方法)(O(nlogn),O(1)) 快排思想: 1.先从队尾开始向前扫描且当low < high时,如果a[high] > pivot,则high–,但如果a[high] < pivot, 则将high的值赋值给low,即arr[low] = a[high],同时要转换数组扫描的方式,即需要从队首开始向队尾进行扫描了 2.同理,当从队首开始向队尾进行扫描时,如果a[low] < pivot,则low++,但如果a[low] > pivot了, 则就需要将low位置的值赋值给high位置,即arr[low] = arr[high],同时将数组扫描方式换为由队尾向队首进行扫描. 3.不断重复1和2,直到low=high时,low或high的位置就是该基准数据在数组中的正确索引位置. 重写比较方法: 不是直接比较两个数a、b的大小,而是比较a、b的拼接结果ab和ba的大小,若ab<ba则a排在b的前面 */ public String PrintMinNumber(int [] numbers) { if (numbers == null || numbers.length == 0) return ""; quickSort(numbers,0,numbers.length-1); StringBuffer sb = new StringBuffer(); for (int number : numbers) { sb.append(number); } return sb.toString(); } public void quickSort(int[] numbers,int low,int high) { if (low >= high) return; int pivot = numbers[low]; int originalLow = low; int originalHigh = high; while (low < high){ while (low < high){ if (compare(numbers[high],pivot) < 0){ numbers[low] = numbers[high]; low++; break; } high--; } while (low < high){ if (compare(numbers[low],pivot) > 0) { numbers[high] = numbers[low]; high--; break; } low++; } } numbers[low] = pivot; quickSort(numbers,originalLow,low - 1); quickSort(numbers,low + 1,originalHigh); } //重写比较方法 public int compare(int a,int b){ //a、b的拼接结果ab和ba可能会超过int的最大范围,因此比较它们的大小需要通过比较字符串的大小 String str1 = a + "" + b; String str2 = b + "" + a; return str1.compareTo(str2);//源码其实就是从左到右比较每一个数位的大小 }