java-快速排序Demo
阿新 • • 發佈:2018-12-18
public class QuickSort { public static void main(String[] args) throws NumberWrongException { int[] numbers = ArrayTools.getRandomInts2(7, -9, 9); ArrayTools.printInts(numbers); quickSort(numbers, 0, numbers.length - 1); ArrayTools.printInts(numbers); } public static int[] quickSort(int[] numbers, int low, int high) { if (low < high) { int mid = getMid(numbers, low, high); quickSort(numbers, low, mid - 1); quickSort(numbers, mid + 1, high); } return numbers; } private static int getMid(int[] numbers, int low, int high) { int temp = numbers[low]; while (low < high) { while (low < high && numbers[high] >= temp) { high--; } numbers[low] = numbers[high]; while (low < high && numbers[low] <= temp) { low++; } numbers[high] = numbers[low]; } numbers[low] = temp; return low; } }