所以这段代码是我用来对包含数字列表的文件进行排序的类。一切似乎都可以正常工作,但我在显示交换和比较数量的程序方面遇到了问题。
public class IntQuickSort {
private static void swap(int array[], int a, int b) {
int temp;
temp = array[a];
array[a] = array[b];
array[b] = temp;
}
public static void quicksort(int array[], int start, int end) {
int pivotPoint;
if (start < end) {
pivotPoint = partition(array, start, end);
quicksort(array, start, pivotPoint - 1);
quicksort(array, pivotPoint + 1, end);
}
}
private static int partition(int array[], int start, int end) {
int pivotValue;
int pivotIndex;
int mid;
int swapAmount = 0;
int compare = 0;
mid = (start + end) / 2;
swap(array, start, mid);
pivotIndex = start;
pivotValue = array[start];
for (int scan = start + 1; scan <= end; scan++) {
compare++;
if (array[scan] < pivotValue) {
pivotIndex++;
swap(array, pivotIndex, scan);
swapAmount++;
}
}
swap(array, start, pivotIndex);
System.out.println("Quick sort");
System.out.println("Comparisons " + compare);
System.out.println("Swaps " + swapAmount);
return pivotIndex;
}
}
因此,交换和比较当前位于代码的分区部分。但是,当我尝试显示它们时,交换和比较似乎会循环,并且它们不保留其值。