【发布时间】:2021-11-06 14:32:15
【问题描述】:
我的目标是使用各种排序方法对两个随机(每次)数组运行 500 次迭代,计算运行时间并将其存储到数组中。当所有 500 次迭代完成后,我想平均时间并在最后打印出每个数组大小的平均值,而不是每次循环运行时。我目前收到以下错误:“索引 500 超出长度 500 的范围”每次循环运行时我也得到相同的平均值。如何每次通过我的循环运行一个未排序的数组?如何存储价值?对于其他数组大小,我该如何再次执行此操作?
我的冒泡排序示例如下:
public static void main(String[] args) {
{
int[] smallUnsortedArray = createArrayWithRandomInts(99);
bubbleSort(smallUnsortedArray);
int[] largeUnsortedArray = createArrayWithRandomInts(10000);
bubbleSort(largeUnsortedArray);
}
}
/**
* Sorting an array of ints in ascending order using bubbleSort
* Best-Case Complexity: O(n), Average Complexity: O(n^2), Worst-Case Complexity: O(n^2)
* O(n) is achieved in Best-Case (already sorted array) using the alreadySorted flag
* @param array
* @return
*/
static int[] bubbleSort(int[] array)
{
int bubbleSortTime[] = new int[500];
int iterations = 0;
while(iterations < 500) {
int temp;
boolean alreadySorted = true;
long start = System.nanoTime();
{
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array.length - 1; j++)
{
if (array[j] > array[j + 1])
{
alreadySorted = false;
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
if (alreadySorted == true)
{
break;
}
;
long end = System.nanoTime();
long bubbleSortRunTime = end - start;
bubbleSortTime[i] = (int) bubbleSortRunTime;
i++;
}
// getting array length
int length = bubbleSortTime.length;
// default sum value.
int sum = 0;
// sum of all values in array using for loop
for (int i = 0; i < bubbleSortTime.length; i++) {
sum += bubbleSortTime[i];
}
double average = sum / bubbleSortTime.length;
System.out.println("Average of bubble sort : "+average + " nano seconds.");
}
iterations++;
}
return array;
}
static int[] createArrayWithRandomInts(int size)
{
int[] array = new int[size];
for (int i = 0; i < size; i++)
{
array[i] = (int) (Math.random() * Math.random() * 100000);
}
return array;
}
}
我曾这样尝试过(原创):
static int[] bubbleSort(int[] array)
{
int bubbleSortTime[] = new int[500];
int x, y;
for (x = 0; x< 500; x++) {
int temp;
boolean alreadySorted = true;
long start = System.nanoTime();
{
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array.length - 1; j++)
{
if (array[j] > array[j + 1])
{
alreadySorted = false;
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
if (alreadySorted == true)
{
break;
}
}
long end = System.nanoTime();
long bubbleSortRunTime = end - start;
bubbleSortTime[x] = (int) bubbleSortRunTime;
x++;
}
// getting array length
int length = bubbleSortTime.length;
// default sum value.
int sum = 0;
// sum of all values in array using for loop
for (int i = 0; i < bubbleSortTime.length; i++) {
sum += bubbleSortTime[i];
}
double average = sum / length;
System.out.println("Average of bubble sort : "+average + " nano seconds.");
}
return array;
}
我不确定我的逻辑哪里出了问题。我认为我当前的构建比以前的构建更好,但我没有得到原始版本的越界错误。
【问题讨论】:
-
抱歉误读。如果您实际上发布了错误发生的位置,则调试起来会更容易。
i++;看起来也很狡猾。 -
int bubbleSortTime[] = new int[500];使用数组的大小,而不是硬编码值。 -
好的,应该是新的 int[array.length]?
-
为什么 for (x = 0; x
-
哦,那是旧代码。当数组大小可能只有 99 或 10000 时,为什么你只迭代 500 次?
标签: java arrays sorting benchmarking bubble-sort