码迷,mamicode.com
首页 > 其他好文 > 详细

QuickSort

时间:2016-09-10 01:26:59      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

递归版:

 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

标签:

原文地址:http://www.cnblogs.com/upside-down/p/5858498.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!