java,排序数组。如何记录大型迭代中的单个执行时间以平均它们?
我正在编写一个冒泡排序算法,它需要排序和使用多达1000次,并平均执行时间,我已经让程序在大部分情况下工作,它生成一个大小为n的数组,用范围为x->y的随机数填充数组,对数进行排序并重复程序n次,给出每次迭代的运行时间。我一直在为上面提到的迭代绘制单个运行时的问题上,比如说..25,这样我就可以对它们进行平均,这样我就不必绘制一百万个RUMBER。我不确定我是否应该创建一个新类来完成这个任务,并将其导入或是什么,我对编程是全新的。任何帮助都将不胜感激,我所看到的其他问题都没有一个足够的答案java,排序数组。如何记录大型迭代中的单个执行时间以平均它们?,java,sorting,timer,runtime,iteration,Java,Sorting,Timer,Runtime,Iteration,我正在编写一个冒泡排序算法,它需要排序和使用多达1000次,并平均执行时间,我已经让程序在大部分情况下工作,它生成一个大小为n的数组,用范围为x->y的随机数填充数组,对数进行排序并重复程序n次,给出每次迭代的运行时间。我一直在为上面提到的迭代绘制单个运行时的问题上,比如说..25,这样我就可以对它们进行平均,这样我就不必绘制一百万个RUMBER。我不确定我是否应该创建一个新类来完成这个任务,并将其导入或是什么,我对编程是全新的。任何帮助都将不胜感激,我所看到的其他问题都没有一个足够的答案
package termproject;
import java.util.Scanner; //call up classes to be used in the program
import java.util.Random;
public class TermProject {
public static void main(String[] args) {
int n; //designate variables
int randomNumMin;
int randomNumMax;
int iteration;
int swap;
Scanner scan = new Scanner(System.in); //create scanner
System.out.print("How many numbers should be in the array? "); //get input from user to designate the size of the array to be used
n = scan.nextInt(); //this method allows for ease of use when changing the perameters of the program
System.out.print("What should the minimum value for the array be? "); //user input, same reasoning, ease of use when changing perameters
randomNumMin = scan.nextInt();
System.out.print("What about the maximum value? ");
randomNumMax = scan.nextInt();
System.out.print("How many times do you want to run the program? "); //user input to designate how many times the program should execute
iteration = scan.nextInt();
int[] ar = new int[n]; //create new array
Random random = new Random(); //create foundation for random number generation
long startTime = System.nanoTime();
for (int r = 0; r < iteration; r++) { //tell program to start iterations here and run repitions
System.out.println(); //up to the corresponding parse
System.out.println("\n");
System.out.println(r + 1 + ". " + "The program will sort this array: "); //a label for the unsorted array
for (int i = 0; i < n; i++) {
ar[i] = random.nextInt(ar.length); //populate unsorted array with randomly generated numbers
System.out.print(" " + ar[i] + ", "); //print out the randomly generated array
}
for (int i = 0; i < (n - 1); i++) { //sort the previously generated array
for (int d = 0; d < n - i - 1; d++) { //bubblesort method
if (ar[d] > ar[d + 1]) {
swap = ar[d];
ar[d] = ar[d + 1];
ar[d + 1] = swap;
}
}
}
System.out.println("\n"); //print out a label for the sorted array
System.out.println();
System.out.println(" After Bubblesorting the array becomes: ");
for (int i = 0; i < n; i++) //populate the array with sorted information and print out
{
System.out.print(" " + ar[i] + ", ");
}
long executeTime = System.nanoTime() - startTime;
System.out.println("\n");
System.out.println(+executeTime/1E9 +" Seconds");
}
}
}
我不太确定我是否正确理解你,但这是你想要的吗?只需存储执行每个排序所需的时间,并最终获取它们的平均值
import java.util.Scanner; //call up classes to be used in the program
import java.util.Random;
public class TermProject {
public static void main(String[] args) {
int n; //designate variables
int randomNumMin;
int randomNumMax;
int iteration;
int swap;
Scanner scan = new Scanner(System.in); //create scanner
System.out.print("How many numbers should be in the array? "); //get input from user to designate the size of the array to be used
n = scan.nextInt(); //this method allows for ease of use when changing the parameters of the program
System.out.print("What should the minimum value for the array be? "); //user input, same reasoning, ease of use when changing parameters
randomNumMin = scan.nextInt();
System.out.print("What about the maximum value? ");
randomNumMax = scan.nextInt();
System.out.print("How many times do you want to run the program? "); //user input to designate how many times the program should execute
iteration = scan.nextInt();
int[] ar = new int[n]; //create new array
Random random = new Random(); //create foundation for random number generation
long startTime;
double[] eTime = new double[iteration]; // array used to store execution times
for (int r = 0; r < iteration; r++) { //tell program to start iterations here and run repitions
System.out.println(); //up to the corresponding parse
System.out.println("\n");
System.out.println(r + 1 + ". " + "The program will sort this array: "); //a label for the unsorted array
startTime = System.nanoTime(); //store the current time when starting the loop
for (int i = 0; i < n; i++) {
ar[i] = random.nextInt(ar.length); //populate unsorted array with randomly generated numbers
System.out.print(" " + ar[i] + ", "); //print out the randomly generated array
}
for (int i = 0; i < (n - 1); i++) { //sort the previously generated array
for (int d = 0; d < n - i - 1; d++) { //bubblesort method
if (ar[d] > ar[d + 1]) {
swap = ar[d];
ar[d] = ar[d + 1];
ar[d + 1] = swap;
}
}
}
System.out.println("\n"); //print out a label for the sorted array
System.out.println();
System.out.println(" After Bubblesorting the array becomes: ");
for (int i = 0; i < n; i++) //populate the array with sorted information and print out
{
System.out.print(" " + ar[i] + ", ");
}
eTime[r] = (System.nanoTime() - startTime)/1e9; //storing the time in the array
System.out.println();
System.out.println("\n" + eTime[r] +" Seconds");
}
// just printing out the sorting times **************
System.out.println();
int itNo = 1;
for(Double d: eTime)
{
System.out.println("Iteration " + itNo + " time is:" + d);
itNo++;
}
//Averaging the times **************************
System.out.println();
double average = 0.0;
for(double d: eTime)
{
average += d;
}
average /= eTime.length; //storing the average in the average variable.
System.out.println("The average execution time is:" + average + ".");
}
}
希望这就是您的想法?您使用runtime一词会造成混乱。我会使用类似executionTime的东西。请在提问之前准备好代码,即至少正确格式化代码。重新格式化代码,确保它编译成功。正如我所说,这是一个新的完美的工作方式,我没有想到使用另一个数组来存储时间。我一直在想《泰晤士报》并把它们列出来。感谢您的帮助现在您可以让输入的randomNumMin+Max实际更改生成的随机数以填充数组:我一直在看,我假设我得到的随机数在1和数组大小之间,按照我现在得到的方式,嗯?是的,所以你可以改变ar[i]=random.nextIntar.length;to ar[i]=random.nextIntrandomNumMax+1;,如果ar[i]