??????????5??mergeSort(Object[]src ?? Object[]dst ?? int low ?? int high ?? int off)
private static void mergeSort(Object[] src??
Object[] dest??
int low??
int high??
int off) {
int length = high - low;
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low &&
((Comparable) dest[j-1]).compareTo(dest[j])>0; j--)
swap(dest?? j?? j-1);
return;
}
int destLow  = low;
int destHigh = high;
low  += off;
high += off;
int mid = (low + high) >>> 1;
mergeSort(dest?? src?? low?? mid?? -off);
mergeSort(dest?? src?? mid?? high?? -off);
if (((Comparable)src[mid-1]).compareTo(src[mid]) <= 0) {
System.arraycopy(src?? low?? dest?? destLow?? length);
return;
}
for(int i = destLow?? p = low?? q = mid; i < destHigh; i++) {
if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}
/**
* Swaps x[a] with x[b].
*/
private static void swap(Object[] x?? int a?? int b) {
Object t = x[a];
x[a] = x[b];
x[b] = t;
}
???????????????????????????ε??????????????????м??low??high??????????仯????????????ε??С????INSERTIONSORT_THRESHOLD???????7????????????Σ????κ????Σ????????????????????????????????????????м????λ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????鰴??????????????????С?д?????飬??????????д???????????????????????????ж?????????????????????????????ж????д??β????У?
????for(int i = destLow?? p = low?? q = mid; i < destHigh; i++)
????{
????if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[q])<=0)
????dest[i] = src[p++];
????else
????dest[i] = src[q++];
????}
??????ζ?????????????????????????д?????????if?????????????????д?ú????
?????????????????????????????????????Comparable???????????comareTo?????????????????????implements Comparable?????Collections.sort(List<T>list)???????????????????????????Arrays.sort(Object []t)?????????????????????????????????java.lang.ClassCastExceptionXXXDO cannot be cast to java.lang.Comparable
??????????????????????????mergeSort???????????????????Comparator?????
??????????6?? mergeSort(Object[]src??Object[]dst??int low??int high??intoff??Comparator c)
private static void mergeSort(Object[] src??
Object[] dest??
int low?? int high?? int off??
Comparator c) {
int length = high - low;
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low && c.compare(dest[j-1]?? dest[j])>0; j--)
swap(dest?? j?? j-1);
return;
}
int destLow  = low;
int destHigh = high;
low  += off;
high += off;
int mid = (low + high) >>> 1;
mergeSort(dest?? src?? low?? mid?? -off?? c);
mergeSort(dest?? src?? mid?? high?? -off?? c);
if (c.compare(src[mid-1]?? src[mid]) <= 0) {
System.arraycopy(src?? low?? dest?? destLow?? length);
return;
}
for(int i = destLow?? p = low?? q = mid; i < destHigh; i++) {
if (q >= high || p < mid && c.compare(src[p]?? src[q]) <= 0)
dest[i] = src[p++];
else
dest[i] = src[q++];
}
}
?????????????????????????????????????????????????compare?????????comparator????????????κ?????
????????????????????java?????????????Ч??????????????????????д???????????????????TreeSet?е???add??????????????????????????comparator????д????????????????棬?????????????comparator????????????????TreeSet???漲??????TreeeMap????TreeMap?е?????????????????Comparator??
??????????7??TreeSet???TreeMap????Comparator???????
????public TreeSet(Comparator<? super E> comparator) {
????this(new TreeMap<E??Object>(comparator));
????}
????TreeSet(NavigableMap<E??Object> m) {
????this.m = m;
????}
????public TreeMap(Comparator<? super K> comparator) {
????this.comparator = comparator;
????}
?????????д??????Comparator????????????????TreeMap??????????????????TreeMap??add??????
??????????8??TreeSet#add(E e)
????public boolean add(E e) {
????return m.put(e??PRESENT)==null;
????}
???????m???????????????????7?????????m??????????????TreeMap??????????????TreeMap??put????
???????????9??TreeMap#put(K key ?? V value)
public V put(K key?? V value) {
Entry<K??V> t = root;
if (t == null) {
root = new Entry<K??V>(key?? value?? null);
size = 1;
modCount++;
return null;
}
int cmp;
Entry<K??V> parent;
// split comparator and comparable paths
Comparator<? super K> cpr = comparator;
if (cpr != null) {
do {
parent = t;
cmp = cpr.compare(key?? t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
} while (t != null);
}
else {
if (key == null)
throw new NullPointerException();
Comparable<? super K> k = (Comparable<? super K>) key;
do {
parent = t;
cmp = k.compareTo(t.key);
if (cmp < 0)
t = t.left;
else if (cmp > 0)
t = t.right;
else
return t.setValue(value);
} while (t != null);
}
Entry<K??V> e = new Entry<K??V>(key?? value?? parent);
if (cmp < 0)
parent.left = e;
else
parent.right = e;
fixAfterInsertion(e);
size++;
modCount++;
return null;
}
?????????ж?????????Comparator???в???????д?????λ??????????????????????????????????????????????????????????????ú??????????????????Щ??????????????????????????????????????????????????Щ??????????????TreeSet???????????????????????????????????????????add???????????????????
???????????java???????????Щ????????????????????????????????????????????????????java??????????????????????????????д??????????????????Щ???????bug??????Ч??δ????java????????????????Ч??