简体   繁体   English

使用排序算法创建具有连续整数个数的数组以进行基准测试

[英]Creating arrays with successive numbers of integers for benchmarking purposes using sorting algorithms

I am working on a benchmarking assignment for various algorithms.我正在为各种算法进行基准测试。 The requirements are to run the programs with successive data sets of random integers totaling 10,000, 20,000, 100,000, 200,000, 1,000,000 respectively.要求是使用连续的随机整数数据集运行程序,这些数据集分别为 10,000、20,000、100,000、200,000、1,000,000。 I have written the programs so that I can manually input the data set sizes, but, I would prefer to use a loop to run the program once and automatically input the different data sets.我编写了程序,以便我可以手动输入数据集大小,但是,我更喜欢使用循环来运行程序一次并自动输入不同的数据集。 I'm not really sure how to go about doing this;我不确定如何去做; any advice is appreciated.任何建议表示赞赏。 Thanks in advance.提前致谢。

package bubble.sort;



public class BubbleSort {

public static void main(String[] arg) throws Exception{

    int array[] = new int [1000];
    int i;

    for (int k = 1; k<= 100; k++){

        for (i=0;i<array.length;i++){
        //for loop that will populate array with random numbers 
        array[i] = 1 + (int)(Math.random() * 10);
        }//end for loop

    // get the start time in nanoseconds
    long startTime = System.nanoTime();

    //call mergesort to sort the entire array
    bubbleSort(array);

    // get the end time in nanoseconds
    long endTime = System.nanoTime();

    // calculate elapsed time in nanoseconds
    long duration = endTime - startTime;

    // print the elapsed time in seconds   (nanaoseconds/ 1 billion)
    System.out.printf("%12.8f %n", (double)duration/100000000) ;


    }
}
      public static void bubbleSort( int [ ] array){
      int temp = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 1; j < (array.length - i); j++) {
        if (array[j - 1] > array[j]) {
            temp = array[j - 1];
            array[j - 1] = array[j];
            array[j] = temp;
        }

      }
    }
  }
}

The simplest way - wrap your code with another loop:最简单的方法 - 用另一个循环包装你的代码:

int dataSetSizes[] = {10000, 20000, 100000, 200000, 1000000};
    for (int dataSetSize : dataSetSizes) {
        int array[] = new int[dataSetSize];
        // rest of your code
    }

Extract what is inside of your main method in to a method that takes in the size as a parameter and use that passed in size in order to create the array.将 main 方法中的内容提取到一个将大小作为参数的方法中,并使用传入的大小来创建数组。 Then, in main, run that method with each of the required sizes.然后,在 main 中,使用每个所需的大小运行该方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM