首页 > 代码库 > 快速排序

快速排序

 1 /** 2  * 功能:快速排序 3  */ 4 public class QuickSort { 5  6     public int[] process(int[] array) { 7  8         if (null == array || 0 == array.length) { 9             return array;10         }11 12         sort(array, 0, array.length - 1);13 14         return array;15     }16 17     public void sort(int[] array, int low, int high) {18 19         int middle; //枢纽的位置20 21         if (low < high) {22             middle = partition(array, low, high);23             sort(array, low, middle - 1);24             sort(array, middle + 1, high);25         }26 27     }28 29     public int partition(int[] array, int low, int high) {30 31         int midValue = http://www.mamicode.com/array[low]; //存储枢纽的值32 33         while (low < high) {34             while (low < high && midValue <= array[high]) {35                 high--;36             }37 38             swap(array, low, high);39 40             while (low < high && midValue >= array[low]) {41                 low++;42             }43 44             swap(array, low, high);45         }46 47         return low;48     }49 50     public void swap(int[] array, int a, int b) {51 52         int temp;53 54         temp = array[a];55         array[a] = array[b];56         array[b] = temp;57     }58 }

 

快速排序