1 package com.yzy.test; 2 3 public class Test { 4 5 /** 6 * @param args 7 */ 8 public static void main(String[] args) { 9 int[] array = { 43, 64, 21, 6565, 3424, 22, 6523, 345 }; 10 ArraySort(array, 0, array.length - 1); 11 for (int i : array) { 12 System.out.print(i + " "); 13 } 14 } 15 16 // 快速排序方法 17 private static void ArraySort(int[] array, int lowIndex, int highIndex) { 18 int low = lowIndex; 19 int high = highIndex; 20 int mid; 21 if (lowIndex < highIndex) { 22 while (low <= high) { 23 mid = array[(lowIndex + highIndex) / 2]; 24 while ((low < highIndex) && (array[low] < mid)) { 25 ++low; 26 } 27 while ((high > lowIndex) && (array[high] > mid)) { 28 --high; 29 } 30 if (low <= high) { 31 wrap(array, low, high); 32 ++low; 33 --high; 34 } 35 } 36 if (low < highIndex) { 37 ArraySort(array, low, highIndex); 38 } 39 if (high > lowIndex) { 40 ArraySort(array, lowIndex, high); 41 } 42 } 43 44 } 45 46 // 交換數組元素 47 private static void wrap(int[] array, int low, int high) { 48 // TODO Auto-generated method stub 49 int temp = array[low]; 50 array[low] = array[high]; 51 array[high] = temp; 52 } 53 }
技術要點:快速排序是對氣泡排序的一種改進,其排序速度相對較快。基本思想是:經過一趟排序將要排序數據分割成獨立的兩部分,其中一部分的全部數據都比另一部分的全部數據小,而後再按此方法對這兩部分數據分別進行快速排序。整個排序過程能夠遞歸進行,以此是整個數據變成有序序列。數組