好吧,我知道这是一个奇怪的问题,但我发现这个快速排序算法的 java 代码:
private static void quickSort (ArrayList <String> list, int first, int last){
int g = first;
int h = last;
int midIndex = (first + last) / 2;
String dividingValue = list.get(midIndex);
do{
while (list.get(g).compareTo(dividingValue) < 0) g++;
while (list.get(h).compareTo(dividingValue) > 0) h--;
if (g <= h)
{
swap(list,g,h);
g++;
h--;
}
}
while (g < h);
if (h > first) quickSort (list,first,h);
if (g < last) quickSort (list,g,last);
}
private static void swap (ArrayList <String> list, int first, int h)
{
String temp = list.get(first);
list.set(first, list.get(h));
list.set(h, temp);
}
我工作,但这对我来说真的没有意义。有人能以某种方式给我解释这段代码吗?或者如果可能的话,为快速排序提供更简单的代码?