简体   繁体   English

如何使用for循环将数组大小加倍以比较不同数组大小的选择排序性能?

[英]How to double an array size using a for loop to compare selection sort performance with different array sizes?

My goal for this project was to measure the performance of selection sort using random numbers.我在这个项目中的目标是使用随机数来衡量选择排序的性能。 I wanted to begin with an array size of n=100, and I wanted to double that array size up to 200,000 (n = 100 * 2, n = 200 * 2, n = 400 * 2....) I did a pretty good job of setting up my selectionSort method in a class, but I am having trouble writing a for loop that will double my array size of 100. Here is my code:我想从 n=100 的数组大小开始,我想将该数组大小加倍到 200,000 (n = 100 * 2, n = 200 * 2, n = 400 * 2 ....) 我做了一个在类中设置我的 selectionSort 方法非常好,但我在编写一个 for 循环时遇到了麻烦,该循环将使我的数组大小加倍,为 100。这是我的代码:

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

       }
     }




public static void main(String[] args) {
         int[] bigdata = new int[100];
                randomFill(bigdata, 100000);
                   for(int i = 100; i < 200000; i *= 2){ // this is where I am having trouble
                      bigdata[i] = bigdata[i] * 2;}
                      display(bigdata);
                      System.out.println("Is sorted before selectionSort: " + isSorted(bigdata));
                      long start = System.currentTimeMillis();
                      selectionSort(bigdata);
                      display(bigdata);
                      long elapsed = System.currentTimeMillis() - start;
                      System.out.println("Is sorted after selectionSort: " + isSorted(bigdata));
                      System.out.println("Elapsed time in milliseconds " + elapsed);
          }
       }

My code in main has successfully displayed selectionSort performance for a random array with a size of 100, but I am unsure where to go next with writing a forloop to double my array "big data" up to 200000.我在 main 中的代码已经成功地显示了一个大小为 100 的随机数组的 selectionSort 性能,但我不确定下一步写一个 forloop 将我的数组“大数据”加倍到 200000 的位置。

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

       }
     }
public static void main(String[] args) {
         int[] bigdata;

                   for(int i = 100; i < 200000; i *= 2){ 
                      // Here you double the size of bigdata array
                      bigdata = new int[i];
                      // You fill the i sized array
                      randomFill(bigdata, 100000);
                      display(bigdata);
                      System.out.println("Is sorted before selectionSort: " + isSorted(bigdata));
                      long start = System.currentTimeMillis();
                      selectionSort(bigdata);
                      display(bigdata);
                      long elapsed = System.currentTimeMillis() - start;
                      System.out.println("Is sorted after selectionSort: " + isSorted(bigdata));
                      System.out.println("Elapsed time in milliseconds " + elapsed);
          }
       }

Consider using an implementation of java.util.List , ex: java.util.ArrayList .考虑使用java.util.List的实现,例如: java.util.ArrayList This will ensure that you do not have to bother about managing the size of the list.这将确保您不必费心管理列表的大小。 It will automatically expand as and when you keep adding more elements.当您不断添加更多元素时,它会自动扩展。

int[] bigdata = new int[100];

can be changed to:可以改为:

List<Integer> bigdata = new ArrayList<>();

The randomFill(int[], int) method can be changed to randomFill(List<Integer>, int) . randomFill(int[], int)方法可以更改为randomFill(List<Integer>, int)

This should help you get started in the right direction.这应该可以帮助您朝着正确的方向开始。

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

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