繁体   English   中英

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

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

我正在为各种算法进行基准测试。 要求是使用连续的随机整数数据集运行程序,这些数据集分别为 10,000、20,000、100,000、200,000、1,000,000。 我编写了程序,以便我可以手动输入数据集大小,但是,我更喜欢使用循环来运行程序一次并自动输入不同的数据集。 我不确定如何去做; 任何建议表示赞赏。 提前致谢。

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;
        }

      }
    }
  }
}

最简单的方法 - 用另一个循环包装你的代码:

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

将 main 方法中的内容提取到一个将大小作为参数的方法中,并使用传入的大小来创建数组。 然后,在 main 中,使用每个所需的大小运行该方法。

暂无
暂无

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

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