1 package org.apache.turbine.util;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 /***
20 * QuickSort - adapted from Doug Lea's Public Domain collection
21 * library.
22 *
23 * @author <a href="mailto:mbryson@mindspring.com">Dave Bryson</a>
24 * @version $Id: QuickSort.java 264148 2005-08-29 14:21:04Z henning $
25 * @deprecated This class will be removed after the 2.3 release. It is
26 * not part of the Web Framework scope. If you need it, please
27 * use a sorting library for quicksort.
28 */
29 public class QuickSort
30 {
31 /***
32 * Sort array of Objects using the QuickSort algorithm.
33 *
34 * @param s An Object[].
35 * @param lo The current lower bound.
36 * @param hi The current upper bound.
37 * @param cmp A Comparable to compare two elements.
38 */
39 public static void quickSort(Object s[],
40 int lo,
41 int hi,
42 Comparable cmp)
43 {
44 if (lo >= hi)
45 return;
46
47
48
49
50
51 int mid = (lo + hi) / 2;
52
53 if (cmp.compare(s[lo], s[mid]) > 0)
54 {
55
56 Object tmp = s[lo];
57 s[lo] = s[mid];
58 s[mid] = tmp;
59 }
60
61 if (cmp.compare(s[mid], s[hi]) > 0)
62 {
63
64 Object tmp = s[mid];
65 s[mid] = s[hi];
66 s[hi] = tmp;
67
68 if (cmp.compare(s[lo], s[mid]) > 0)
69 {
70
71 Object tmp2 = s[lo];
72 s[lo] = s[mid];
73 s[mid] = tmp2;
74 }
75 }
76
77
78 int left = lo + 1;
79
80
81 int right = hi - 1;
82
83
84 if (left >= right)
85 return;
86
87 Object partition = s[mid];
88
89 for (; ;)
90 {
91 while (cmp.compare(s[right], partition) > 0)
92 --right;
93
94 while (left < right &&
95 cmp.compare(s[left], partition) <= 0)
96 ++left;
97
98 if (left < right)
99 {
100
101 Object tmp = s[left];
102 s[left] = s[right];
103 s[right] = tmp;
104
105 --right;
106 }
107 else
108 break;
109 }
110 quickSort(s, lo, left, cmp);
111 quickSort(s, left + 1, hi, cmp);
112 }
113
114 /***
115 * Sorts and array of objects.
116 *
117 * @param data An Object[].
118 * @param cmp A Comparable to compare two elements.
119 */
120 public void sort(Object[] data,
121 Comparable cmp)
122 {
123 QuickSort.quickSort(data,
124 0,
125 data.length - 1,
126 cmp);
127 }
128 }