Debian Codegraph @

src/tools/org/h2/dev/sort/InPlaceStableQuicksort.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.dev.sort;
7
8 import java.util.Comparator;
9
10 /**
11 * A stable quicksort implementation that uses O(log(n)) memory. It normally
12 * runs in O(n*log(n)*log(n)), but at most in O(n^2).
13 *
14 * @param <T> the element type
15 */
16 public class InPlaceStableQuicksort<T> {
17
18 /**
19 * The minimum size of the temporary array. It is used to speed up sorting
20 * small blocks.
21 */
22 private static final int TEMP_SIZE = 1024;
23
24 /**
25 * Blocks smaller than this number are sorted using binary insertion sort.
26 * This usually speeds up sorting.
27 */
28 private static final int INSERTION_SORT_SIZE = 16;
29
30 /**
31 * The data array to sort.
32 */
33 private T[] data;
34
35 /**
36 * The comparator.
37 */
38 private Comparator<T> comp;
39
40 /**
41 * The temporary array.
42 */
43 private T[] temp;
44
45 /**
46 * Sort an array using the given comparator.
47 *
48 * @param data the data array to sort
49 * @param comp the comparator
50 */
51 public static <T> void sort(T[] data, Comparator<T> comp) {
52 new InPlaceStableQuicksort<T>().sortArray(data, comp);
53 }
54
55 /**
56 * Sort an array using the given comparator.
57 *
58 * @param d the data array to sort
59 * @param c the comparator
60 */
61 public void sortArray(T[] d, Comparator<T> c) {
62 this.data = d;
63 this.comp = c;
64 int len = Math.max((int) (100 * Math.log(d.length)), TEMP_SIZE);
65 len = Math.min(d.length, len);
66 @SuppressWarnings("unchecked")
67 T[] t = (T[]) new Object[len];
68 this.temp = t;
69 quicksort(0, d.length - 1);
70 }
71
72 /**
73 * Sort a block using the quicksort algorithm.
74 *
75 * @param from the index of the first entry to sort
76 * @param to the index of the last entry to sort
77 */
78 private void quicksort(int from, int to) {
79 while (to > from) {
80 if (to - from < INSERTION_SORT_SIZE) {
81 binaryInsertionSort(from, to);
82 return;
83 }
84 T pivot = selectPivot(from, to);
85 int second = partition(pivot, from, to);
86 if (second > to) {
87 pivot = selectPivot(from, to);
88 pivot = data[to];
89 second = partition(pivot, from, to);
90 if (second > to) {
91 second--;
92 }
93 }
94 quicksort(from, second - 1);
95 from = second;
96 }
97 }
98
99 /**
100 * Sort a block using the binary insertion sort algorithm.
101 *
102 * @param from the index of the first entry to sort
103 * @param to the index of the last entry to sort
104 */
105 private void binaryInsertionSort(int from, int to) {
106 for (int i = from + 1; i <= to; i++) {
107 T x = data[i];
108 int ins = binarySearch(x, from, i - 1);
109 for (int j = i - 1; j >= ins; j--) {
110 data[j + 1] = data[j];
111 }
112 data[ins] = x;
113 }
114 }
115
116 /**
117 * Find the index of the element that is larger than x.
118 *
119 * @param x the element to search
120 * @param from the index of the first entry
121 * @param to the index of the last entry
122 * @return the position
123 */
124 private int binarySearch(T x, int from, int to) {
125 while (from <= to) {
126 int m = (from + to) >>> 1;
127 if (comp.compare(x, data[m]) >= 0) {
128 from = m + 1;
129 } else {
130 to = m - 1;
131 }
132 }
133 return from;
134 }
135
136 /**
137 * Move all elements that are bigger than the pivot to the end of the list,
138 * and return the partitioning index. The partitioning index is the start
139 * index of the range where all elements are larger than the pivot. If the
140 * partitioning index is larger than the 'to' index, then all elements are
141 * smaller or equal to the pivot.
142 *
143 * @param pivot the pivot
144 * @param from the index of the first element
145 * @param to the index of the last element
146 * @return the first element of the second partition
147 */
148 private int partition(T pivot, int from, int to) {
149 if (to - from < temp.length) {
150 return partitionSmall(pivot, from, to);
151 }
152 int m = (from + to + 1) / 2;
153 int m1 = partition(pivot, from, m - 1);
154 int m2 = partition(pivot, m, to);
155 swapBlocks(m1, m, m2 - 1);
156 return m1 + m2 - m;
157 }
158
159 /**
160 * Partition a small block using the temporary array. This will speed up
161 * partitioning.
162 *
163 * @param pivot the pivot
164 * @param from the index of the first element
165 * @param to the index of the last element
166 * @return the first element of the second partition
167 */
168 private int partitionSmall(T pivot, int from, int to) {
169 int tempIndex = 0, dataIndex = from;
170 for (int i = from; i <= to; i++) {
171 T x = data[i];
172 if (comp.compare(x, pivot) <= 0) {
173 if (tempIndex > 0) {
174 data[dataIndex] = x;
175 }
176 dataIndex++;
177 } else {
178 temp[tempIndex++] = x;
179 }
180 }
181 if (tempIndex > 0) {
182 System.arraycopy(temp, 0, data, dataIndex, tempIndex);
183 }
184 return dataIndex;
185 }
186
187 /**
188 * Swap the elements of two blocks in the data array. Both blocks are next
189 * to each other (the second block starts just after the first block ends).
190 *
191 * @param from the index of the first element in the first block
192 * @param second the index of the first element in the second block
193 * @param to the index of the last element in the second block
194 */
195 private void swapBlocks(int from, int second, int to) {
196 int len1 = second - from, len2 = to - second + 1;
197 if (len1 == 0 || len2 == 0) {
198 return;
199 }
200 if (len1 < temp.length) {
201 System.arraycopy(data, from, temp, 0, len1);
202 System.arraycopy(data, second, data, from, len2);
203 System.arraycopy(temp, 0, data, from + len2, len1);
204 return;
205 } else if (len2 < temp.length) {
206 System.arraycopy(data, second, temp, 0, len2);
207 System.arraycopy(data, from, data, from + len2, len1);
208 System.arraycopy(temp, 0, data, from, len2);
209 return;
210 }
211 reverseBlock(from, second - 1);
212 reverseBlock(second, to);
213 reverseBlock(from, to);
214 }
215
216 /**
217 * Reverse all elements in a block.
218 *
219 * @param from the index of the first element
220 * @param to the index of the last element
221 */
222 private void reverseBlock(int from, int to) {
223 while (from < to) {
224 T old = data[from];
225 data[from++] = data[to];
226 data[to--] = old;
227 }
228 }
229
230 /**
231 * Select a pivot. To ensure a good pivot is select, the median element of a
232 * sample of the data is calculated.
233 *
234 * @param from the index of the first element
235 * @param to the index of the last element
236 * @return the pivot
237 */
238 private T selectPivot(int from, int to) {
239 int count = (int) (6 * Math.log10(to - from));
240 count = Math.min(count, temp.length);
241 int step = (to - from) / count;
242 for (int i = from, j = 0; i < to; i += step, j++) {
243 temp[j] = data[i];
244 }
245 T pivot = select(temp, 0, count - 1, count / 2);
246 return pivot;
247 }
248
249 /**
250 * Select the specified element.
251 *
252 * @param d the array
253 * @param from the index of the first element
254 * @param to the index of the last element
255 * @param k which element to return (1 means the lowest)
256 * @return the specified element
257 */
258 private T select(T[] d, int from, int to, int k) {
259 while (true) {
260 int pivotIndex = (to + from) >>> 1;
261 int pivotNewIndex = selectPartition(d, from, to, pivotIndex);
262 int pivotDist = pivotNewIndex - from + 1;
263 if (pivotDist == k) {
264 return d[pivotNewIndex];
265 } else if (k < pivotDist) {
266 to = pivotNewIndex - 1;
267 } else {
268 k = k - pivotDist;
269 from = pivotNewIndex + 1;
270 }
271 }
272 }
273
274 /**
275 * Partition the elements to select an element.
276 *
277 * @param d the array
278 * @param from the index of the first element
279 * @param to the index of the last element
280 * @param pivotIndex the index of the pivot
281 * @return the new index
282 */
283 private int selectPartition(T[] d, int from, int to, int pivotIndex) {
284 T pivotValue = d[pivotIndex];
285 swap(d, pivotIndex, to);
286 int storeIndex = from;
287 for (int i = from; i <= to; i++) {
288 if (comp.compare(d[i], pivotValue) < 0) {
289 swap(d, storeIndex, i);
290 storeIndex++;
291 }
292 }
293 swap(d, to, storeIndex);
294 return storeIndex;
295 }
296
297 /**
298 * Swap two elements in the array.
299 *
300 * @param d the array
301 * @param a the index of the first element
302 * @param b the index of the second element
303 */
304 private void swap(T[] d, int a, int b) {
305 T t = d[a];
306 d[a] = d[b];
307 d[b] = t;
308 }
309
310 }