簡體   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