我有一个问题,我无法自己解决,因为我无法找到问题,我不知道在哪里看。 我期待测量我的合并和插入算法。我很确定我的算法没问题,它们可以作为应用程序,但是关闭的东西是测量时间的值。所有测试的值都几乎相同,值以E14结束? 从纳秒时间到秒或毫秒转换我做错了吗? 看一看。
我的结果显示如下:
Merge Sort integer
Before:[97, 25, 91, 95, 60, 55, 59, 73, 75, 80]
After: [25, 55, 59, 60, 73, 75, 80, 91, 95, 97]
Measured time: 3.213631365062379E14 seconds
import java.util.Arrays;
import java.util.Random;
import jv222dp_assign3.SortingAlgorithms;
public class AlgorithmMeasure {
private static int intSize = 10;
private static int[] arr = new int[intSize];
private static String[] sArr = new String[10];
private static Random random = new Random();
public static String randomString(){
char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}
String output = sb.toString();
return output;
}
public static int randomInteger(){
int temp = 0;
for (int i = 0; i<arr.length; i++){
temp = random.nextInt(arr.length*10);
}
return temp;
}
public static double getIntSelectionSortTime(){
for (int i = 0; i < 10; i++){
arr[i] = randomInteger();
}
System.out.println("Before:"+Arrays.toString(arr));
long startTime = System.nanoTime();
int[] temp = SortingAlgorithms.insertionSort(arr);
long endTime = System.nanoTime();
System.out.println("After: "+Arrays.toString(temp));
double sec = (double) endTime - startTime/1000000000.0;
return sec;
}
public static double getStringSelectionSortTime(){
for (int j = 0; j < 10; j ++){
sArr[j] = randomString();
}
System.out.println("Before: "+Arrays.toString(sArr));
long startTime = System.nanoTime();
String[] temp = SortingAlgorithms.insertionSort(sArr);
long endTime = System.nanoTime();
double sec2 = (double) endTime - startTime/1000000000.0;
System.out.println("After: "+Arrays.toString(temp));
return sec2;
}
public static double getIntMergeSortTime(){
for (int i = 0; i < 5; i++){
arr[i] = randomInteger();
}
System.out.println("Before:"+Arrays.toString(arr));
long startTime2 = System.nanoTime();
int[] temp = SortingAlgorithms.insertionSort(arr);
long endTime2 = System.nanoTime();
System.out.println("After: "+Arrays.toString(temp));
double sec2 = (double) endTime2 - startTime2/1000000000.0;
return sec2;
}
public static double getStringMergeSortTime(){
for (int j = 0; j < 10; j ++){
sArr[j] = randomString();
}
System.out.println("Before: "+Arrays.toString(sArr));
long startTime2 = System.nanoTime();
String[] temp = SortingAlgorithms.insertionSort(sArr);
long endTime2 = System.nanoTime();
double sec3 = (double) endTime2 - startTime2/1000000000.0;
System.out.println("After: "+Arrays.toString(temp));
return sec3;
}
public static void main(String[] args) {
System.out.println("Selection Sort integer ");
System.out.println("Measured time: "+getIntSelectionSortTime()+" seconds");
System.out.println("Selection Sort string ");
System.out.println("Measured time: "+getStringSelectionSortTime()+" seconds");
System.out.println("Merge Sort integer ");
System.out.println("Measured time: "+getIntMergeSortTime()+" seconds");
System.out.println("Merge Sort string ");
System.out.println("Measured time: "+getStringMergeSortTime()+" seconds");
}
}