在System.nanoTime中处理测量QuickSort效率的作业。我有代码适用于其他数组大小(例如100; 1,000; 10,000),但是,当我尝试使用大小为100,000的数组的方法时,我收到StackOverflowError。值得注意的是,我已经为InsertionSort和BubbleSort提供了大小为100,000的数组。
目标是通过QuickSort运行105次,在前5个之后测量100次,使用数组,以nanoTime测量运行时间。
首先,我正在创建一个预定大小的随机int数组。接下来,我克隆那个int数组并将克隆传递给所需的排序方法(在本例中为QuickSort)。最后,我创建了一个使用QuickSort运行所需次数的方法。方法如下:
public static void quickSort(int[] unsortedArray, int size, int max) {
long averageTime = 0;
for (int i = 0; i < RUN_TIMES; i++) {
if (i < 5) {
QuickSort.quickSort(unsortedArray);
} else {
long startTime = System.nanoTime();
QuickSort.quickSort(unsortedArray);
long stopTime = System.nanoTime();
long runTime = stopTime - startTime;
averageTime = averageTime + runTime;
}
}
System.out.println("Quicksort time for size " + size +
" and max " + max + ": " + averageTime / DIVISOR);
}
RUN_TIMES设置为105,DIVISOR设置为100.教师提供的QuickSort代码如下:
public static void quickSort(int[] list) {
quickSort(list, 0, list.length - 1);
}
private static void quickSort(int[] list, int first, int last) {
if (last > first) {
int pivotIndex = partition(list, first, last);
quickSort(list, first, pivotIndex - 1);
quickSort(list, pivotIndex + 1, last);
}
}
private static int partition(int[] list, int first, int last) {
int pivot = list[first]; // Choose the first element as the pivot
int low = first + 1; // Index for forward search
int high = last; // Index for backward search
while (high > low) {
// Search forward from left
while (low <= high && list[low] <= pivot)
low++;
// Search backward from right
while (low <= high && list[high] > pivot)
high--;
// Swap two elements in the list
if (high > low) {
int temp = list[high];
list[high] = list[low];
list[low] = temp;
}
}
while (high > first && list[high] >= pivot)
high--;
// Swap pivot with list[high]
if (pivot > list[high]) {
list[first] = list[high];
list[high] = pivot;
return high;
}
else {
return first;
}
我做错了什么?最后一个想法,我在较新的MacBook Pro上使用NetBeans。只是想确定我分享了一切。任何帮助将不胜感激!
更新:这是我用来生成数组的代码:
private static int[] createArray(int size, int maxValue) {
int arraySize = size;
/*
Create array of variable size
Random int 1 - 999
*/
// Constructor for random and variable declaration
Random random = new Random();
int x;
// Constructor for ArrayList<Integer>
ArrayList<Integer> tempArrayList = new ArrayList<>();
// While loop used to create ArrayList w/100 unique values
while (tempArrayList.size() < arraySize) {
// Creates int value between 1 and 999
x = random.nextInt(maxValue) + 1;
// Checks if generated value already exists within ArrayList
if(!tempArrayList.contains(x)) {
// Add to ArrayList
//System.out.println("Added: " + x);
tempArrayList.add(x);
} else {
// Do nothing
} // end if - else
} // end while loop
// Convert ArrayList<Integer> to int[]
int[] arrayList = tempArrayList.stream().mapToInt(i -> i).toArray();
return arrayList;
} // end createArray method
答案 0 :(得分:1)
简短版本:您的列表已排序,QS需要对排序列表进行大量递归。
QuickSort是一种递归算法,最坏情况下的时间复杂度为O(n)
,对于您的数据选择(第一项),列表排序时可以获得。
在第一次迭代之后,它是因为QuickSort就地排序,即它修改了输入数组。
第一次迭代后,该阵列上的QuickSort需要n
次递归或100000
。好多啊。考虑一个更好的排序算法,或者不使用递归的算法,或者考虑在不递归的情况下重写它。
答案 1 :(得分:0)
你编写了一个递归算法,它给堆栈施加了很大的压力,然后给它一个大的数据集,假设堆栈压力与问题大小的平方成比例,那么为什么你有一个堆栈没有任何谜团溢出。将算法重新编码为循环,不要在堆栈上留下太多垃圾。