jdk 7 에서 sort 내부 알고리즘 변경
2965 단어 sort
Arrays. sort 를 예 로 들 면:
public static <T> void sort(T[] a, Comparator<? super T> c) {
if (LegacyMergeSort.userRequested)
legacyMergeSort(a, c);
else
TimSort.sort(a, c);
}
LegacyMergeSort.userRequested
Boolean b = java.security.AccessController.doPrivileged(
new sun.security.action.GetBooleanAction("java.util.Arrays.useLegacyMergeSort")).booleanValue();
System.out.println(b);
기본 값 은 false, 즉 기본 값 은 timsort 알고리즘 입 니 다.Legacy MergeSort 는 간단하게 배열 을 2 분 의 2 로 나 누 어 그룹 을 나 눈 다음 각 그룹 은 정렬 알고리즘 을 삽입 하여 각 그룹 을 처리 한 다음 정렬 합 니 다.
/**
* Old merge sort implementation can be selected (for
* compatibility with broken comparators) using a system property.
* Cannot be a static boolean in the enclosing class due to
* circular dependencies. To be removed in a future release.
*/
static final class LegacyMergeSort {
private static final boolean userRequested =
java.security.AccessController.doPrivileged(
new sun.security.action.GetBooleanAction(
"java.util.Arrays.useLegacyMergeSort")).booleanValue();
}
주의:
Cannot be a static boolean in the enclosing class due to circular dependencies. To be removed in a future release.
timsort 의 일부 변 화 는:
1. 재 귀 등 수량 을 사용 하지 않 고 2 분 으로 나 누 기
2. 내부 각 그룹 은 2 점 삽입 정렬 을 사용 합 니 다.
3. 누구 와 누구 에 게 규칙 이 있 습 니까?
인터넷 의 일부 네티즌 들 은 이전 버 전의 코드 가 jdk 7 에서 이상 하 게 던 진 것 을 발견 했다.
public class ReproJava7Exception {
public static void main(String[] args) {
int[] sample = new int[]
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-2,1,0,-2,0,0,0,0};
List<Integer> list = new ArrayList<Integer>();
for (int i : sample)
list.add(i);
// use the native TimSort in JDK 7
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// miss the o1 = o2 case on purpose
return o1 > o2 ? 1 : -1;
}
});
}
}
해결 방법 은 Comparable 인 터 페 이 스 를 실현 할 때 1, 1, 0 을 모두 나 누 어야 한다.
참조:
http://www.lifebackup.cn/timsort-java7.html
http://en.wikipedia.org/wiki/Timsort
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
VHDL로 작성된 병합 분류기 (워드 비교기)다른 기사 를 참조해 주세요. 이 문서에서는 병합 분류기 내부에서 사용되는 단어 비교기(Word_Compare)에 대해 설명합니다. 워드 비교기(Word_Compare)는 두 워드( 참조)를 비교하여 둘 중 하나를 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.