我刚刚编写了一个代码,用于从data.txt
,quicksort
读取数据并写入data2.txt
输入格式为:
145 567
12 3456
1345 67
我需要输出格式:
12 3456
145 567
1345 67
但我明白了:
12 3456
1345 67
145 567
以下是代码:
private static ArrayList<String> sort(ArrayList<String> ar, int lo, int hi){
if (lo < hi){
int splitPoint = partition(ar, lo, hi);
sort(ar, lo, splitPoint);
sort(ar, splitPoint +1, hi);
}
return ar;
}
private static int partition(ArrayList<String> ar, int lo, int hi){
String pivot = ar.get(lo);
lo--;
hi++;
while (true){
lo++;
while (lo<hi && ar.get(lo).compareTo(pivot) < 0){
lo++;
}
hi--;
while (hi>lo && ar.get(hi).compareTo(pivot) >= 0){
hi--;
}
if (lo<hi){
swap(ar, lo, hi);
}else {
return hi;
}
}
}
private static ArrayList<String> swap(ArrayList<String> ar, int a, int b){
String temp = ar.get(a);
ar.set(a, ar.get(b));
ar.set(b, temp);
return ar;
}
感谢您的帮助。