与在相反方向预先排序的相同列表相比,此快速排序采用不同数量的比较来对预先排序的列表进行排序。帮助赞赏!
我从同学那里听说不应该这样。显然,两个方向都应该进行相同数量的比较。
快速排序方法:
public static void QS (String[] a){
QS_Recurse(a,0,(a.length-1));
}
private static void QS_Recurse(String[] sorting, int beginning, int end){
if (end > beginning){
String pivot_switcher;
String string_switcher;
String pivot = sorting[beginning];
int middle = beginning;
for (int r = beginning + 1; r <= end; r++){
if (sorting[r].compareTo(pivot) <= 0){
string_switcher = sorting[middle+1];
sorting[middle+1] = sorting[r];
sorting[r] = string_switcher;
middle++;
}
comparisons++;
}
pivot_switcher = sorting[middle];
sorting[middle] = pivot;
sorting[beginning] = pivot_switcher;
QS_Recurse(sorting,beginning,middle-1);
QS_Recurse(sorting,middle+1,end);
}
}