Skip to content

Instantly share code, notes, and snippets.

@cheng470
Created March 29, 2019 03:18
Show Gist options
  • Save cheng470/4e99fba88f03f9eadec5c322bc3380e3 to your computer and use it in GitHub Desktop.
Save cheng470/4e99fba88f03f9eadec5c322bc3380e3 to your computer and use it in GitHub Desktop.
java.util.Arrays.sort方法中的排序算法在JDK7中已经被替换了。如果违法了比较的约束新的排序算法也许会抛出llegalArgumentException异常。JDK6中的实现则忽略了这种情况。
[2, 24, 12, 18, 44, 38, 44, 18, 47, 27, 7, 30, 38, 16, 42, 3, 49, 4, 42, 36, 16, 44, 38, 16, 3, 24, 15, 24, 6, 49, 2, 6, 44, 15, 6, 24, 5, 11, 34, 26, 13, 0, 0, 49, 45, 39, 39, 36, 17, 30, 25, 32, 47, 24, 3, 37, 16, 5, 27, 3, 27, 26, 17, 40, 44, 9, 12, 39, 44, 8, 17, 0, 35, 5, 0, 33, 32, 2, 10, 37, 11, 49, 38, 3, 24, 43, 13, 38, 45, 7, 3, 9, 37, 20, 22, 6, 35, 41, 5, 13, 10, 19, 7, 10, 3, 22, 8, 3, 21, 34, 14, 36, 13, 33, 42, 7, 18, 29, 3, 11, 15, 22, 15, 13, 11, 15, 35, 25, 11, 7, 6, 47, 17, 0, 0, 16, 12, 2, 26, 40, 7, 47, 25, 37, 44, 10, 18, 16, 8, 20, 43, 17, 25, 26, 40, 8, 36, 40, 16, 9, 35, 45, 38, 9, 27, 42, 32, 2, 10, 26, 25, 38, 40, 35, 48, 22, 24, 14, 38, 3, 12, 42, 8, 24, 18, 47, 32, 15, 0, 21, 44, 19, 4, 31, 5, 19, 42, 2, 11, 21]
第0次排序
[3, 31, 38, 4, 16, 16, 27, 33, 0, 32, 25, 17, 44, 12, 2, 13, 9, 24, 42, 0, 11, 24, 36, 17, 3, 33, 6, 12, 4, 35, 15, 49, 8, 35, 3, 10, 16, 6, 44, 45, 42, 44, 7, 9, 6, 3, 24, 47, 38, 17, 42, 18, 0, 22, 49, 30, 38, 37, 43, 45, 5, 24, 15, 14, 10, 44, 10, 26, 22, 17, 32, 47, 37, 2, 26, 44, 2, 27, 25, 20, 21, 8, 7, 18, 36, 44, 19, 7, 0, 3, 0, 11, 37, 40, 15, 18, 47, 47, 37, 16, 5, 40, 35, 26, 2, 18, 16, 11, 41, 40, 27, 49, 35, 39, 12, 22, 24, 2, 34, 5, 16, 26, 8, 25, 38, 12, 38, 42, 25, 47, 5, 43, 9, 2, 30, 24, 49, 16, 35, 29, 44, 13, 10, 11, 15, 27, 3, 44, 6, 32, 13, 7, 42, 3, 40, 34, 39, 40, 0, 42, 26, 25, 48, 15, 36, 5, 19, 7, 17, 3, 20, 38, 36, 0, 8, 39, 24, 18, 21, 32, 3, 7, 6, 13, 11, 24, 9, 38, 13, 21, 38, 3, 22, 45, 14, 11, 19, 10, 15, 8]
第1次排序
[38, 35, 25, 26, 3, 16, 13, 24, 15, 22, 42, 26, 4, 7, 18, 2, 2, 3, 39, 0, 42, 8, 11, 0, 3, 4, 32, 24, 11, 9, 33, 40, 38, 44, 49, 31, 44, 36, 7, 3, 17, 26, 9, 0, 16, 27, 17, 25, 27, 11, 49, 48, 38, 10, 15, 13, 42, 12, 40, 36, 11, 12, 13, 36, 44, 47, 6, 8, 7, 43, 39, 39, 32, 25, 30, 5, 3, 47, 16, 44, 35, 25, 26, 5, 8, 15, 42, 38, 27, 2, 21, 34, 45, 18, 22, 49, 8, 8, 15, 7, 25, 24, 14, 24, 6, 7, 44, 17, 20, 24, 43, 3, 44, 7, 12, 2, 16, 38, 10, 6, 6, 16, 45, 21, 18, 13, 40, 37, 19, 36, 24, 29, 49, 26, 42, 35, 10, 37, 33, 16, 17, 24, 13, 3, 3, 35, 2, 15, 22, 9, 3, 3, 30, 47, 35, 16, 18, 0, 15, 24, 38, 41, 17, 40, 21, 0, 5, 5, 0, 5, 38, 11, 0, 14, 37, 44, 9, 2, 12, 45, 10, 10, 44, 37, 18, 47, 32, 19, 40, 42, 32, 47, 22, 27, 34, 6, 38, 20, 19, 11]
第2次排序
[25, 42, 3, 12, 21, 11, 40, 16, 44, 25, 35, 22, 6, 35, 16, 10, 3, 0, 6, 6, 27, 47, 11, 2, 40, 7, 14, 22, 26, 39, 44, 0, 43, 7, 17, 44, 24, 20, 45, 18, 9, 9, 13, 44, 0, 26, 24, 18, 16, 14, 30, 27, 27, 15, 24, 11, 38, 42, 3, 21, 44, 36, 48, 2, 6, 37, 43, 38, 38, 49, 3, 2, 11, 0, 47, 8, 3, 12, 22, 15, 16, 40, 33, 42, 27, 17, 38, 37, 19, 3, 12, 45, 16, 13, 18, 24, 7, 47, 30, 40, 19, 42, 4, 24, 8, 47, 40, 21, 18, 13, 38, 0, 8, 5, 35, 34, 15, 38, 16, 13, 24, 4, 6, 41, 0, 25, 18, 42, 26, 2, 38, 26, 17, 36, 3, 31, 5, 49, 25, 2, 13, 5, 24, 17, 15, 3, 45, 10, 8, 17, 37, 15, 44, 26, 44, 44, 38, 3, 22, 2, 15, 36, 3, 19, 7, 47, 34, 8, 10, 33, 49, 29, 5, 12, 0, 37, 39, 10, 36, 32, 25, 16, 35, 9, 11, 9, 49, 24, 32, 35, 32, 11, 5, 20, 32, 10, 39, 7, 7, 42]
第3次排序
[11, 13, 44, 25, 3, 17, 35, 0, 10, 4, 26, 6, 20, 22, 40, 0, 7, 25, 47, 24, 45, 42, 16, 14, 42, 42, 39, 44, 38, 31, 41, 30, 27, 24, 38, 30, 0, 45, 13, 8, 47, 36, 38, 27, 13, 8, 19, 18, 7, 16, 24, 4, 16, 8, 18, 15, 37, 14, 49, 12, 21, 24, 2, 35, 9, 15, 10, 25, 24, 18, 6, 44, 9, 45, 11, 7, 2, 27, 16, 36, 6, 18, 2, 40, 44, 27, 43, 19, 33, 44, 32, 47, 7, 13, 16, 3, 3, 35, 6, 17, 25, 47, 38, 12, 34, 37, 26, 22, 8, 24, 43, 2, 3, 48, 29, 26, 13, 18, 49, 36, 40, 0, 3, 16, 0, 26, 17, 12, 15, 26, 12, 5, 40, 3, 42, 3, 33, 15, 39, 16, 17, 2, 24, 38, 11, 44, 5, 42, 24, 20, 0, 21, 17, 5, 3, 2, 38, 15, 10, 7, 11, 47, 9, 38, 49, 10, 21, 3, 32, 32, 25, 22, 42, 44, 34, 35, 8, 11, 36, 6, 39, 19, 35, 11, 9, 49, 5, 3, 37, 15, 0, 32, 38, 44, 37, 7, 10, 22, 5, 40]
第4次排序
[36, 38, 30, 15, 9, 19, 40, 16, 42, 6, 12, 21, 49, 4, 42, 14, 15, 9, 26, 27, 33, 25, 27, 17, 44, 5, 35, 15, 42, 11, 43, 25, 25, 21, 3, 16, 32, 34, 3, 3, 48, 13, 29, 40, 16, 47, 5, 42, 7, 45, 2, 26, 39, 22, 44, 24, 38, 24, 38, 19, 49, 36, 15, 3, 13, 16, 35, 44, 25, 2, 0, 16, 8, 38, 35, 6, 19, 10, 26, 0, 43, 8, 24, 37, 18, 18, 8, 10, 12, 3, 2, 47, 17, 24, 0, 13, 11, 32, 22, 13, 4, 3, 11, 11, 38, 44, 5, 0, 44, 40, 45, 17, 20, 35, 22, 40, 33, 13, 24, 17, 38, 5, 10, 44, 34, 18, 0, 22, 24, 47, 7, 0, 18, 44, 38, 10, 6, 44, 26, 3, 11, 14, 24, 15, 2, 12, 20, 7, 2, 7, 36, 49, 26, 6, 38, 17, 3, 11, 35, 47, 32, 16, 49, 32, 37, 21, 45, 7, 8, 12, 25, 41, 0, 18, 3, 10, 6, 39, 42, 16, 40, 24, 9, 39, 47, 37, 15, 3, 42, 8, 37, 9, 5, 2, 36, 7, 30, 27, 31, 27]
第5次排序
[2, 42, 34, 5, 11, 40, 6, 13, 32, 42, 21, 6, 30, 4, 37, 0, 27, 38, 20, 44, 11, 27, 38, 22, 14, 24, 0, 2, 22, 38, 17, 25, 9, 21, 36, 11, 45, 7, 42, 38, 38, 44, 18, 44, 40, 10, 13, 9, 3, 4, 3, 44, 42, 19, 17, 9, 25, 16, 0, 7, 16, 36, 40, 35, 21, 17, 41, 37, 3, 40, 18, 44, 44, 33, 0, 24, 0, 8, 34, 8, 25, 35, 3, 43, 36, 27, 48, 15, 45, 11, 39, 7, 16, 9, 12, 31, 7, 49, 6, 24, 42, 38, 5, 26, 11, 43, 42, 26, 18, 15, 25, 14, 24, 10, 26, 24, 49, 10, 7, 17, 47, 37, 47, 22, 2, 6, 0, 13, 35, 16, 35, 25, 2, 8, 24, 40, 29, 49, 45, 16, 13, 33, 12, 39, 39, 38, 5, 15, 5, 22, 19, 24, 38, 2, 47, 32, 3, 12, 12, 44, 18, 44, 49, 15, 24, 15, 26, 30, 0, 13, 27, 10, 3, 32, 37, 3, 11, 26, 18, 10, 2, 17, 8, 16, 16, 15, 5, 7, 8, 3, 36, 3, 32, 47, 47, 6, 3, 19, 35, 20]
第6次排序
[22, 18, 0, 27, 13, 40, 42, 20, 44, 25, 22, 3, 44, 17, 35, 25, 16, 25, 0, 38, 44, 21, 24, 0, 27, 38, 36, 44, 17, 10, 45, 40, 17, 12, 12, 32, 24, 6, 9, 37, 0, 42, 35, 8, 38, 33, 6, 47, 22, 2, 3, 2, 11, 7, 5, 7, 41, 11, 39, 16, 29, 10, 32, 19, 0, 47, 15, 14, 0, 44, 8, 5, 34, 40, 4, 2, 36, 17, 3, 21, 0, 16, 37, 16, 13, 13, 38, 9, 5, 30, 24, 11, 35, 33, 36, 16, 3, 42, 42, 43, 7, 18, 44, 13, 10, 11, 19, 40, 3, 2, 19, 5, 21, 26, 25, 26, 40, 8, 48, 15, 17, 8, 7, 26, 45, 39, 11, 36, 20, 3, 32, 32, 35, 11, 25, 37, 43, 24, 4, 38, 26, 44, 18, 16, 35, 12, 6, 9, 5, 47, 49, 3, 49, 38, 31, 2, 44, 39, 12, 6, 16, 9, 10, 7, 42, 7, 3, 24, 30, 15, 42, 37, 8, 45, 34, 38, 22, 24, 18, 18, 6, 10, 14, 24, 49, 3, 15, 27, 38, 47, 47, 26, 3, 15, 15, 49, 13, 27, 24, 2]
第7次排序
[22, 24, 38, 6, 3, 35, 27, 44, 10, 19, 40, 42, 32, 11, 9, 38, 6, 15, 22, 40, 18, 25, 24, 45, 38, 11, 44, 17, 24, 26, 40, 18, 36, 47, 13, 25, 5, 31, 36, 13, 0, 0, 17, 43, 12, 38, 16, 39, 7, 18, 21, 37, 17, 3, 49, 6, 45, 16, 36, 34, 10, 37, 9, 38, 32, 10, 11, 42, 2, 7, 14, 22, 0, 3, 8, 47, 39, 12, 0, 20, 20, 26, 13, 36, 42, 21, 7, 11, 3, 38, 12, 9, 2, 43, 16, 42, 30, 5, 22, 2, 3, 38, 16, 44, 4, 33, 24, 44, 34, 40, 17, 42, 3, 30, 33, 13, 27, 47, 15, 25, 10, 37, 14, 44, 15, 5, 37, 15, 21, 3, 26, 2, 6, 0, 12, 2, 8, 45, 8, 32, 24, 7, 18, 5, 16, 25, 49, 19, 49, 13, 25, 44, 15, 32, 3, 24, 0, 24, 24, 27, 2, 16, 11, 11, 4, 0, 8, 47, 7, 35, 35, 39, 29, 44, 3, 41, 40, 3, 49, 6, 15, 26, 18, 26, 27, 44, 8, 42, 9, 38, 16, 10, 19, 7, 35, 47, 17, 5, 48, 35]
第8次排序
[49, 44, 26, 20, 44, 11, 11, 3, 42, 15, 44, 17, 16, 42, 3, 15, 0, 36, 4, 32, 27, 44, 27, 19, 47, 49, 21, 6, 10, 17, 7, 0, 38, 13, 5, 30, 24, 36, 4, 16, 24, 7, 41, 16, 22, 24, 47, 15, 14, 49, 38, 3, 42, 6, 47, 18, 9, 25, 10, 42, 24, 37, 6, 42, 30, 39, 40, 2, 40, 2, 27, 27, 35, 33, 24, 29, 47, 18, 16, 11, 14, 3, 6, 9, 5, 25, 2, 18, 3, 7, 5, 37, 7, 34, 2, 24, 44, 39, 16, 25, 26, 15, 5, 22, 3, 40, 22, 7, 35, 26, 16, 21, 6, 45, 42, 0, 15, 19, 25, 13, 8, 36, 17, 32, 31, 44, 43, 24, 17, 11, 0, 0, 20, 43, 33, 2, 45, 13, 40, 32, 3, 24, 8, 0, 9, 35, 10, 40, 13, 32, 0, 35, 38, 48, 19, 25, 39, 22, 12, 13, 21, 3, 3, 12, 44, 38, 35, 26, 38, 38, 8, 15, 18, 37, 5, 7, 36, 12, 11, 3, 38, 18, 17, 12, 26, 10, 45, 47, 49, 9, 8, 10, 34, 16, 2, 44, 38, 37, 8, 11]
第9次排序
Exception in thread "main" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeLo(TimSort.java:747)
at java.util.TimSort.mergeAt(TimSort.java:483)
at java.util.TimSort.mergeCollapse(TimSort.java:410)
at java.util.TimSort.sort(TimSort.java:214)
at java.util.TimSort.sort(TimSort.java:173)
at java.util.Arrays.sort(Arrays.java:659)
at java.util.Collections.sort(Collections.java:217)
at com.v56.service.Test.main(Test.java:18)
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* 复现jdk7排序兼容问题
*
* @author jianchengxu
*/
public class TestSort {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < 200; i++) {
list.add((int) (Math.random() * 50));
}
System.out.println(list);
for (int i = 0; i < 100; i++) {
System.out.println("第" + i + "次排序");
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
if (o1.equals(o2)) return 0;
return Math.random() < 0.5 ? 1 : -1;
}
});
System.out.println(list);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment