| 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 merge sort implementation that uses at most O(log(n)) memory |
| 12 |
* and O(n*log(n)*log(n)) time. |
| 13 |
* |
| 14 |
* @param <T> the element type |
| 15 |
*/ |
| 16 |
public class InPlaceStableMergeSort<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 InPlaceStableMergeSort<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 |
mergeSort(0, d.length - 1); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Sort a block recursively using merge sort. |
| 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 |
void mergeSort(int from, int to) { |
| 79 |
if (to - from < INSERTION_SORT_SIZE) { |
| 80 |
binaryInsertionSort(from, to); |
| 81 |
return; |
| 82 |
} |
| 83 |
int m = (from + to) >>> 1; |
| 84 |
mergeSort(from, m); |
| 85 |
mergeSort(m + 1, to); |
| 86 |
merge(from, m + 1, to); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Sort a block using the binary insertion sort algorithm. |
| 91 |
* |
| 92 |
* @param from the index of the first entry to sort |
| 93 |
* @param to the index of the last entry to sort |
| 94 |
*/ |
| 95 |
private void binaryInsertionSort(int from, int to) { |
| 96 |
for (int i = from + 1; i <= to; i++) { |
| 97 |
T x = data[i]; |
| 98 |
int ins = binarySearch(x, from, i - 1); |
| 99 |
for (int j = i - 1; j >= ins; j--) { |
| 100 |
data[j + 1] = data[j]; |
| 101 |
} |
| 102 |
data[ins] = x; |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Find the index of the element that is larger than x. |
| 108 |
* |
| 109 |
* @param x the element to search |
| 110 |
* @param from the index of the first entry |
| 111 |
* @param to the index of the last entry |
| 112 |
* @return the position |
| 113 |
*/ |
| 114 |
private int binarySearch(T x, int from, int to) { |
| 115 |
while (from <= to) { |
| 116 |
int m = (from + to) >>> 1; |
| 117 |
if (comp.compare(x, data[m]) >= 0) { |
| 118 |
from = m + 1; |
| 119 |
} else { |
| 120 |
to = m - 1; |
| 121 |
} |
| 122 |
} |
| 123 |
return from; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Merge two arrays. |
| 128 |
* |
| 129 |
* @param from the start of the first range |
| 130 |
* @param second start of the second range |
| 131 |
* @param to the last element of the second range |
| 132 |
*/ |
| 133 |
private void merge(int from, int second, int to) { |
| 134 |
int len1 = second - from, len2 = to - second + 1; |
| 135 |
if (len1 == 0 || len2 == 0) { |
| 136 |
return; |
| 137 |
} |
| 138 |
if (len1 + len2 == 2) { |
| 139 |
if (comp.compare(data[second], data[from]) < 0) { |
| 140 |
swap(data, second, from); |
| 141 |
} |
| 142 |
return; |
| 143 |
} |
| 144 |
if (len1 <= temp.length) { |
| 145 |
System.arraycopy(data, from, temp, 0, len1); |
| 146 |
mergeSmall(data, from, temp, 0, len1 - 1, data, second, to); |
| 147 |
return; |
| 148 |
} else if (len2 <= temp.length) { |
| 149 |
System.arraycopy(data, second, temp, 0, len2); |
| 150 |
System.arraycopy(data, from, data, to - len1 + 1, len1); |
| 151 |
mergeSmall(data, from, data, to - len1 + 1, to, temp, 0, len2 - 1); |
| 152 |
return; |
| 153 |
} |
| 154 |
mergeBig(from, second, to); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Merge two (large) arrays. This is done recursively by merging the |
| 159 |
* beginning of both arrays, and then the end of both arrays. |
| 160 |
* |
| 161 |
* @param from the start of the first range |
| 162 |
* @param second start of the second range |
| 163 |
* @param to the last element of the second range |
| 164 |
*/ |
| 165 |
private void mergeBig(int from, int second, int to) { |
| 166 |
int len1 = second - from, len2 = to - second + 1; |
| 167 |
int firstCut, secondCut, newSecond; |
| 168 |
if (len1 > len2) { |
| 169 |
firstCut = from + len1 / 2; |
| 170 |
secondCut = findLower(data[firstCut], second, to); |
| 171 |
int len = secondCut - second; |
| 172 |
newSecond = firstCut + len; |
| 173 |
} else { |
| 174 |
int len = len2 / 2; |
| 175 |
secondCut = second + len; |
| 176 |
firstCut = findUpper(data[secondCut], from, second - 1); |
| 177 |
newSecond = firstCut + len; |
| 178 |
} |
| 179 |
swapBlocks(firstCut, second, secondCut - 1); |
| 180 |
merge(from, firstCut, newSecond - 1); |
| 181 |
merge(newSecond, secondCut, to); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* Merge two (small) arrays using the temporary array. This is done to speed |
| 186 |
* up merging. |
| 187 |
* |
| 188 |
* @param target the target array |
| 189 |
* @param pos the position of the first element in the target array |
| 190 |
* @param s1 the first source array |
| 191 |
* @param from1 the index of the first element in the first source array |
| 192 |
* @param to1 the index of the last element in the first source array |
| 193 |
* @param s2 the second source array |
| 194 |
* @param from2 the index of the first element in the second source array |
| 195 |
* @param to2 the index of the last element in the second source array |
| 196 |
*/ |
| 197 |
private void mergeSmall(T[] target, int pos, T[] s1, int from1, int to1, |
| 198 |
T[] s2, int from2, int to2) { |
| 199 |
T x1 = s1[from1], x2 = s2[from2]; |
| 200 |
while (true) { |
| 201 |
if (comp.compare(x1, x2) <= 0) { |
| 202 |
target[pos++] = x1; |
| 203 |
if (++from1 > to1) { |
| 204 |
System.arraycopy(s2, from2, target, pos, to2 - from2 + 1); |
| 205 |
break; |
| 206 |
} |
| 207 |
x1 = s1[from1]; |
| 208 |
} else { |
| 209 |
target[pos++] = x2; |
| 210 |
if (++from2 > to2) { |
| 211 |
System.arraycopy(s1, from1, target, pos, to1 - from1 + 1); |
| 212 |
break; |
| 213 |
} |
| 214 |
x2 = s2[from2]; |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Find the largest element in the sorted array that is smaller than x. |
| 221 |
* |
| 222 |
* @param x the element to search |
| 223 |
* @param from the index of the first entry |
| 224 |
* @param to the index of the last entry |
| 225 |
* @return the index of the resulting element |
| 226 |
*/ |
| 227 |
private int findLower(T x, int from, int to) { |
| 228 |
int len = to - from + 1, half; |
| 229 |
while (len > 0) { |
| 230 |
half = len / 2; |
| 231 |
int m = from + half; |
| 232 |
if (comp.compare(data[m], x) < 0) { |
| 233 |
from = m + 1; |
| 234 |
len = len - half - 1; |
| 235 |
} else { |
| 236 |
len = half; |
| 237 |
} |
| 238 |
} |
| 239 |
return from; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Find the smallest element in the sorted array that is larger than or |
| 244 |
* equal to x. |
| 245 |
* |
| 246 |
* @param x the element to search |
| 247 |
* @param from the index of the first entry |
| 248 |
* @param to the index of the last entry |
| 249 |
* @return the index of the resulting element |
| 250 |
*/ |
| 251 |
private int findUpper(T x, int from, int to) { |
| 252 |
int len = to - from + 1, half; |
| 253 |
while (len > 0) { |
| 254 |
half = len / 2; |
| 255 |
int m = from + half; |
| 256 |
if (comp.compare(data[m], x) <= 0) { |
| 257 |
from = m + 1; |
| 258 |
len = len - half - 1; |
| 259 |
} else { |
| 260 |
len = half; |
| 261 |
} |
| 262 |
} |
| 263 |
return from; |
| 264 |
} |
| 265 |
|
| 266 |
/** |
| 267 |
* Swap the elements of two blocks in the data array. Both blocks are next |
| 268 |
* to each other (the second block starts just after the first block ends). |
| 269 |
* |
| 270 |
* @param from the index of the first element in the first block |
| 271 |
* @param second the index of the first element in the second block |
| 272 |
* @param to the index of the last element in the second block |
| 273 |
*/ |
| 274 |
private void swapBlocks(int from, int second, int to) { |
| 275 |
int len1 = second - from, len2 = to - second + 1; |
| 276 |
if (len1 == 0 || len2 == 0) { |
| 277 |
return; |
| 278 |
} |
| 279 |
if (len1 < temp.length) { |
| 280 |
System.arraycopy(data, from, temp, 0, len1); |
| 281 |
System.arraycopy(data, second, data, from, len2); |
| 282 |
System.arraycopy(temp, 0, data, from + len2, len1); |
| 283 |
return; |
| 284 |
} else if (len2 < temp.length) { |
| 285 |
System.arraycopy(data, second, temp, 0, len2); |
| 286 |
System.arraycopy(data, from, data, from + len2, len1); |
| 287 |
System.arraycopy(temp, 0, data, from, len2); |
| 288 |
return; |
| 289 |
} |
| 290 |
reverseBlock(from, second - 1); |
| 291 |
reverseBlock(second, to); |
| 292 |
reverseBlock(from, to); |
| 293 |
} |
| 294 |
|
| 295 |
/** |
| 296 |
* Reverse all elements in a block. |
| 297 |
* |
| 298 |
* @param from the index of the first element |
| 299 |
* @param to the index of the last element |
| 300 |
*/ |
| 301 |
private void reverseBlock(int from, int to) { |
| 302 |
while (from < to) { |
| 303 |
T old = data[from]; |
| 304 |
data[from++] = data[to]; |
| 305 |
data[to--] = old; |
| 306 |
} |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Swap two elements in the array. |
| 311 |
* |
| 312 |
* @param d the array |
| 313 |
* @param a the index of the first element |
| 314 |
* @param b the index of the second element |
| 315 |
*/ |
| 316 |
private void swap(T[] d, int a, int b) { |
| 317 |
T t = d[a]; |
| 318 |
d[a] = d[b]; |
| 319 |
d[b] = t; |
| 320 |
} |
| 321 |
|
| 322 |
} |