首页 > 代码库 > QuickSort

QuickSort

递归版:

 1 public static void quickSort(Object[] array) { 2     quickSort(array, 0, array.length - 1); 3 } 4  5 private static void quickSort(Object[] array, int left, int right) { 6     if (right > left) { 7         Object pivot = array[left]; 8         int ptr = partition(array, pivot, left, right); 9         quickSort(array, left, ptr - 1);10         quickSort(array, ptr + 1, right);11     }12 }13 14 @SuppressWarnings({ "unchecked", "rawtypes" })15 private static int partition(Object[] array, Object pivot, int left, int right) {16     int lp = left;17     int rp = right + 1;18     Comparable pt = (Comparable) pivot;19     while (lp < rp) {20         while (lp < right && pt.compareTo(array[++lp]) >= 0)21             ;22         while (rp > left && pt.compareTo(array[--rp]) <= 0)23             ;24         if (rp > lp)25             swap(array, lp, rp);26     }27     swap(array, left, rp);28 29     return rp;30 }

 

QuickSort