private <T extends Comparable<T>> void sort(T[] array, int lo, int hi, boolean ascend) {
if (lo >= hi) {
return;
}
T toFinal = array[lo];
int leftIdx = lo;
int rightIdx = hi;
int i = lo + 1;
while (i <= rightIdx) {
int compare = array.compareTo(toFinal);
if (compare == 0) {
i++;
} else if (compare < 0 == ascend) {
exchange(array, leftIdx++, i++);
} else {
exchange(array, rightIdx--, i);
}
}
// partially sort left array and right array
// no need to include the leftIdx-th to rightIdx-th elements
// since they are already in its final position
sort(array, lo, leftIdx - 1, ascend);
sort(array, rightIdx + 1, hi, ascend);
}
}
一篇文章里放不下,只能分3次发了,为啥有10000字节限制......:dizzy: