author | yhuang |
Wed, 17 Apr 2013 01:04:45 -0700 | |
changeset 16908 | c8a0f2183fed |
parent 5506 | 202f599c92aa |
child 32991 | b27c76b82713 |
permissions | -rw-r--r-- |
3420
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
1 |
/* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
2 |
* Copyright 2009 Google Inc. All Rights Reserved. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
4 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
8 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
13 |
* accompanied this code). |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
14 |
* |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
18 |
* |
5506 | 19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
3420
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
22 |
*/ |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
23 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
24 |
import java.util.*; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
25 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
26 |
public enum Sorter { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
27 |
TIMSORT { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
28 |
public void sort(Object[] array) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
29 |
ComparableTimSort.sort(array); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
30 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
31 |
}, |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
32 |
MERGESORT { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
33 |
public void sort(Object[] array) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
34 |
Arrays.sort(array); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
35 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
36 |
}; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
37 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
38 |
public abstract void sort(Object[] array); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
39 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
40 |
public static void warmup() { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
41 |
System.out.println("start warm up"); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
42 |
Integer[] gold = new Integer[10000]; |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
43 |
Random random = new java.util.Random(); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
44 |
for (int i=0; i < gold.length; i++) |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
45 |
gold[i] = random.nextInt(); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
46 |
|
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
47 |
for (int i=0; i < 10000; i++) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
48 |
for (Sorter s : values()) { |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
49 |
Integer[] test= gold.clone(); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
50 |
s.sort(test); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
51 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
52 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
53 |
System.out.println(" end warm up"); |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
54 |
} |
bba8035eebfa
6804124: Replace "modified mergesort" in java.util.Arrays.sort with timsort
jjb
parents:
diff
changeset
|
55 |
} |