我已经分配了这个项目,我需要找到不同阵列的执行时间。我的代码完全正常,除了我的数组从选择排序移动到气泡,从气泡移动到插入和合并时的部分。问题是当我的数组在1到1000之间的随机数传递给选择排序并将其排序并且排序的数组被传递给其余的排序方法。我尝试再次查看我的代码,但我无法找到问题所在。我应该填充一个数组还是什么?
感谢任何帮助
package sorting;
import java.util.Arrays;
import java.util.Random;
import java.lang.Math;
import java.io.*;
import java.util.*;
public class sorting {
public static void main(String[] args) {
double[] arr1 = getArray(10000);
double[] arr2 = getArray(20000);
double[] arr3 = getArray(40000);
double[] arr4 = getArray(80000);
System.out.println("Problem 1: Selection sort, Bubble sort, Insertion sort, and Merge sort with thier execution time");
System.out.println("");
System.out.printf("%12s%15s%15s%17s%13s\n", "Number of Elements |", "Selection Sort", "Bubble Sort", "Insertion Sort", "Merge Sort");
System.out.println("--------------------------------------------------------------------------------");
long b = System.currentTimeMillis();
long startTime = System.currentTimeMillis();
selectionSort(arr1);
long endTime = System.currentTimeMillis();
long executionTime = endTime - startTime;
System.out.printf("%11s", arr1.length);
System.out.printf("%18s%2s", executionTime, " ms");
startTime = System.currentTimeMillis();
bubbleSort(arr1);
endTime = System.currentTimeMillis();
executionTime = endTime - startTime;
System.out.printf("%12s%2s", executionTime, " ms");
startTime = System.currentTimeMillis();
insertionSort(arr1);
endTime = System.currentTimeMillis();
executionTime = endTime - startTime;
System.out.printf("%12s%2s", executionTime, " ms");
startTime = System.currentTimeMillis();
(new MergeSort()).mergeSort(arr1);
endTime = System.currentTimeMillis();
executionTime = endTime - startTime;
System.out.printf("%12s%2s\n", executionTime, " ms");
System.out.printf("%11s", arr2.length);
startTime = System.currentTimeMillis();
selectionSort(arr2);
endTime = System.currentTimeMillis();
executionTime = endTime - startTime;
System.out.printf("%18s%2s", executionTime, " ms");
startTime = System.currentTimeMillis();
bubbleSort(arr2);
endTime = System.currentTimeMillis();
executionTime = endTime - startTime;
System.out.printf("%12s%2s", executionTime, " ms");
startTime = System.currentTimeMillis();
insertionSort(arr2);
endTime = System.currentTimeMillis();
executionTime = endTime - startTime;
System.out.printf("%12s%2s", executionTime, " ms");
startTime = System.currentTimeMillis();
(new MergeSort()).mergeSort(arr2);
endTime = System.currentTimeMillis();
executionTime = endTime - startTime;
System.out.printf("%12s%2s\n", executionTime, " ms");
System.out.printf("%11s", arr3.length);
startTime = System.currentTimeMillis();
bubbleSort(arr3);
endTime = System.currentTimeMillis();
executionTime = endTime - startTime;
System.out.printf("%18s%2s", executionTime, " ms");
startTime = System.currentTimeMillis();
bubbleSort(arr3);
endTime = System.currentTimeMillis();
executionTime = endTime - startTime;
System.out.printf("%12s%2s", executionTime, " ms");
startTime = System.currentTimeMillis();
insertionSort(arr3);
endTime = System.currentTimeMillis();
executionTime = endTime - startTime;
System.out.printf("%12s%2s", executionTime, " ms");
startTime = System.currentTimeMillis();
(new MergeSort()).mergeSort(arr3);
endTime = System.currentTimeMillis();
executionTime = endTime - startTime;
System.out.printf("%12s%2s\n", executionTime, " ms");
System.out.printf("%11s", arr4.length);
startTime = System.currentTimeMillis();
bubbleSort(arr4);
endTime = System.currentTimeMillis();
executionTime = endTime - startTime;
System.out.printf("%18s%2s", executionTime, " ms");
startTime = System.currentTimeMillis();
bubbleSort(arr4);
endTime = System.currentTimeMillis();
executionTime = endTime - startTime;
System.out.printf("%12s%2s", executionTime, " ms");
startTime = System.currentTimeMillis();
insertionSort(arr4);
endTime = System.currentTimeMillis();
executionTime = endTime - startTime;
System.out.printf("%12s%2s", executionTime, " ms");
startTime = System.currentTimeMillis();
(new MergeSort()).mergeSort(arr4);
endTime = System.currentTimeMillis();
executionTime = endTime - startTime;
System.out.printf("%12s%2s\n", executionTime, " ms");
}
public static double[] getArray(int n){
double[] arr = new double[n];
for(int i=0; i<n; i++){
arr[i] = ((double)(Math.random()*1000));
}
return arr;
}
public static void selectionSort(double[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
double min = arr[i];
int currentMin = i;
for (int j = i + 1; j < arr.length; j++) {
if (min > arr[j]) {
min = arr[j];
currentMin = j;
}
}
if (currentMin != i) {
arr[currentMin] = arr[i];
arr[i] = min;
}
}
}
public static double[] insertionSort(double[] arr) {
double temp;
for (int i = 1; i < arr.length; i++) {
for (int j = i; j > 0; j--) {
if (arr[j] < arr[j - 1]) {
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
}
return arr;
}
public static double[] bubbleSort(double[] arr) {
double temp;
for (int i = 0; i < arr.length; i++) {
// bubble up
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
public static class MergeSort {
private double[] array;
private double[] temp;
private int length;
public double[] mergeSort(double[] arr) {
this.array = arr;
this.length = arr.length;
this.temp = new double[length];
merge1(0, length - 1);
return arr;
}
private void merge1(int left, double right) {
if (left < right) {
double middle = left + (right - left) / 2;
merge1(left, middle);
merge1((int) (middle + 1), right);
merge2(left, middle, right);
}
}
private void merge2(int left, double middle, double right) {
for (int i = left; i <= right; i++) {
temp[i] = array[i];
}
int i = left;
int j = (int) (middle + 1);
int k = left;
while (i <= middle && j <= right) {
if (temp[i] <= temp[j]) {
array[k] = temp[i];
i++;
} else {
array[k] = temp[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = temp[i];
k++;
i++;
}
}
}
}
这就是我输出的结果 问题1:选择排序,冒泡排序,插入排序和具有执行时间的合并排序
Number of Elements | Selection Sort Bubble Sort Insertion Sort Merge
Sort
----------------------------------------------------------------------------
10000 85 ms 31 ms 31 ms 16 ms
20000 385 ms 100 ms 123 ms 10 ms
40000 3085 ms 401 ms 347 ms 8 ms
80000 13231 ms 1972 ms 1484 ms 35 ms
答案 0 :(得分:0)
问题是我的数组在1到1000之间的随机数是 传递给选择排序,它将其排序并排序 数组正在传递给其他的排序方法
你应该在Is Java "pass-by-reference" or "pass-by-value"?
找到原因的根源对你而言,这是因为你在调用
时传递了数组arr1
的值
selectionSort(arr1);
在这种情况下对arr1
进行的操作会更新arr1
。下次将其传递给
bubbleSort(arr1); // this uses the updated array.
您可以更新代码以使用现有方法中随机生成的数组:
selectionSort(getArray(10000)); // similarily for other cases