14 |
|
* limitations under the License. |
15 |
|
*/ |
16 |
|
|
17 |
< |
import java.util.*; |
17 |
> |
package java.util; |
18 |
|
|
19 |
|
/** |
20 |
|
* This is a near duplicate of {@link TimSort}, modified for use with |
26 |
|
* comparator that simply returns {@code ((Comparable)first).compareTo(Second)}. |
27 |
|
* If this is the case, you are better off deleting ComparableTimSort to |
28 |
|
* eliminate the code duplication. (See Arrays.java for details.) |
29 |
+ |
* |
30 |
+ |
* @author Josh Bloch |
31 |
|
*/ |
32 |
|
class ComparableTimSort { |
33 |
|
/** |
145 |
|
|
146 |
|
// If array is small, do a "mini-TimSort" with no merges |
147 |
|
if (nRemaining < MIN_MERGE) { |
148 |
< |
int initRunLen = countRunAndMakeAscending(a, lo, nRemaining); |
148 |
> |
int initRunLen = countRunAndMakeAscending(a, lo, hi); |
149 |
|
binarySort(a, lo, hi, lo + initRunLen); |
150 |
|
return; |
151 |
|
} |
198 |
|
* @param lo the index of the first element in the range to be sorted |
199 |
|
* @param hi the index after the last element in the range to be sorted |
200 |
|
* @param start the index of the first element in the range that is |
201 |
< |
* not already known to be sorted (@code lo <= start <= hi} |
201 |
> |
* not already known to be sorted ({@code lo <= start <= hi}) |
202 |
|
*/ |
203 |
|
@SuppressWarnings("fallthrough") |
204 |
|
private static void binarySort(Object[] a, int lo, int hi, int start) { |
236 |
|
*/ |
237 |
|
int n = start - left; // The number of elements to move |
238 |
|
// Switch is just an optimization for arraycopy in default case |
239 |
< |
switch(n) { |
239 |
> |
switch (n) { |
240 |
|
case 2: a[left + 2] = a[left + 1]; |
241 |
|
case 1: a[left + 1] = a[left]; |
242 |
|
break; |
266 |
|
* @param a the array in which a run is to be counted and possibly reversed |
267 |
|
* @param lo index of the first element in the run |
268 |
|
* @param hi index after the last element that may be contained in the run. |
269 |
< |
It is required that @code{lo < hi}. |
269 |
> |
It is required that {@code lo < hi}. |
270 |
|
* @return the length of the run beginning at the specified position in |
271 |
|
* the specified array |
272 |
|
*/ |
279 |
|
|
280 |
|
// Find end of run, and reverse range if descending |
281 |
|
if (((Comparable) a[runHi++]).compareTo(a[lo]) < 0) { // Descending |
282 |
< |
while(runHi < hi && ((Comparable) a[runHi]).compareTo(a[runHi - 1]) < 0) |
282 |
> |
while (runHi < hi && ((Comparable) a[runHi]).compareTo(a[runHi - 1]) < 0) |
283 |
|
runHi++; |
284 |
|
reverseRange(a, lo, runHi); |
285 |
|
} else { // Ascending |
670 |
|
dest += count1; |
671 |
|
cursor1 += count1; |
672 |
|
len1 -= count1; |
673 |
< |
if (len1 <= 1) // len1 == 1 || len1 == 0 |
673 |
> |
if (len1 <= 1) // len1 == 1 || len1 == 0 |
674 |
|
break outer; |
675 |
|
} |
676 |
|
a[dest++] = a[cursor2++]; |