I tried looking over the Java source code today. I pulled up the source to Collections and I could not find the sort() method. I didn't have time to spend on the issue though so I stopped. Anyone else look at the source yet?
Collections:
public static void sort(List list) {
Object a[] = list.toArray();
Arrays.sort(a);
ListIterator i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set(a[j]);
}
}
which calls Arrays:
public static void sort(Object[] a) {
Object aux[] = (Object[])a.clone();
mergeSort(aux, a, 0, a.length, 0);
}
which calls:
private static void mergeSort(Object src[], Object dest[],
int low, int high, int off) {
int length = high - low;
// Insertion sort on smallest arrays
if (length < INSERTIONSORT_THRESHOLD) {
for (int i=low; i<high; i++)
for (int j=i; j>low &&
((Comparable)dest[j-1]).compareTo((Comparable)dest[j])>0; j--)
swap(dest, j, j-1);
return;
}
// Recursively sort halves of dest into src
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 list is already sorted, just copy from src to dest. This is an
// optimization that results in faster sorts for nearly ordered lists.
if (((Comparable)src[mid-1]).compareTo((Comparable)src[mid]) <= 0) {
System.arraycopy(src, low, dest, destLow, length);
return;
}
// Merge sorted halves (now in src) into dest
for(int i = destLow, p = low, q = mid; i < destHigh; i++) {
if (q >= high || p < mid && ((Comparable)src[p]).compareTo(src[QQ])<=0)
dest
= src[p++];
else
dest = src[q++];
}
}
---------------
At this point, just use an IDE to look this over. There are more methods that you might want to look at. I think some of the code got reformatted thanks to brackets so you might want to just DL the source and look it over.
The source code:
http://www.sun.com/software/co...2se/java2/download.xml
You need to login to DL it.