why are the averages time of 3 algorithms negative?

时间:2017-10-13 18:04:00

标签: java algorithm time-complexity analysis performanceanalytics

I have to use arrays of sizes ranging from 10000 to 50000 with step size 10000, give the same input to all three algorithms, and for each input repeat the execution 100 times, measure the execution in nanoseconds (using System.nanoTime()), and report the average time in milliseconds. that's what I did below but some of the averages are negative I don't know why?

import java.util.Arrays;

public class Sort{

   public static void main(String[]args){

      double[] arr5 = new double[50000];

      for(int i=0;i<arr5.length;i++)
         arr5[i] = Math.random();

      selectionSort(arr5,10000);
      bubbleSort(arr5,10000);
      quickSort(arr5,10000);

      selectionSort(arr5,20000);
      bubbleSort(arr5,20000);
      quickSort(arr5,20000);

      selectionSort(arr5,30000);
      bubbleSort(arr5,30000);
      quickSort(arr5,30000);

      selectionSort(arr5,40000);
      bubbleSort(arr5,40000);
      quickSort(arr5,40000);

      selectionSort(arr5,50000);
      bubbleSort(arr5,50000);
      quickSort(arr5,50000);
   }

   public static void selectionSort(double [] A,int n){
      int sum = 0;
      System.out.println("Algorithm 1");
      for(int s=0;s<100;s++){
         long arr[] = new long[100];

         long startTime = System.nanoTime();

         for(int i=0;i<n-1;i++){
            int min = i;
            for(int j=i+1;j<n;j++){
               if(A[j] < A[min])
                  min=j;}
            double tmp = A[i];
            A[i] = A[min];
            A[min]=tmp;}

         long endTime = System.nanoTime();

         arr[s] = endTime - startTime;
      //System.out.println(arr[s]);
         sum+=arr[s];
      }
      System.out.println("Average:" + ((sum/100)*Math.pow(10,-6)));

   }

   public static void bubbleSort(double A [],int n){
      int sum = 0;
      System.out.println("\nAlgorithm 2");
      for(int s=0;s<100;s++){
         long[] arr = new long[100];

         long startTime = System.nanoTime();

         for(int i=0;i<n-1;i++){
            for(int j=0;j<n-1-i;j++){
               if(A[j]<A[j+1]){
                  double tmp = A[j];
                  A[j] = A[j+1];
                  A[j+1] = tmp;}}}

         long endTime = System.nanoTime();

         arr[s] = endTime - startTime;
      //System.out.println(arr[s]);
         sum+=arr[s];
      }
      System.out.println("Average:" + ((sum/100)*Math.pow(10,-6)));

   }

//algorithm 3
   public static void quickSort(double A [],int n){
      int sum = 0;
      System.out.println("\nAlgorithm 3");
      long[] arr = new long[100];

      for(int i=0;i<100;i++){
         long startTime = System.nanoTime();

         Arrays.sort(A,0,n-1);

         long endTime = System.nanoTime();

         arr[i] = endTime - startTime;
      //System.out.println(arr[i]);
         sum+=arr[i];
      }
      System.out.println("Average:" + ((sum/100)*Math.pow(10,-6)));

   }

}

1 个答案:

答案 0 :(得分:5)

用于计算的变量sum属于int类型。当你增加sum时,由于endTime - startTime的值很大,它会溢出。当sum溢出时,其值会回到最小值(为负值)并继续增加。

修复:将sum的类型更改为long

另一条评论

名为arr的数组似乎是多余的,因为它们不会在计算中使用,并会在每次循环时重置。

例如,在您的bubbleSort()方法中。您在long[] arr = new long[100];循环中声明并初始化数组forfor(int s=0;s<100;s++)

现在,此数组arr的唯一目的是将endTime - startTime的结果存储在索引s,然后用于增加sum({{ 1}})。

如果这是它的唯一目的,为什么不只是sum+=arr[s];并完全删除数组?

如果您确实打算跟踪此数组中的所有结果,那么您应该将sum += endTime - startTime移到long[] arr = new long[100];循环for之外,因为它已重新声明并初始化每次迭代。

要演示考虑这个小例子,它复制了你在for(int s=0;s<100;s++)和其他方法中所做的事情:

bubbleSort()

输出结果为:

for (int i = 0; i < 5; i++) {
    int[] arr = new int[5];
    arr[i] = (i + 1);
    System.out.println(Arrays.toString(arr));
}

如果在循环之前声明了数组,那么行为会更有意义:

[1, 0, 0, 0, 0]
[0, 2, 0, 0, 0]
[0, 0, 3, 0, 0]
[0, 0, 0, 4, 0]
[0, 0, 0, 0, 5]

在这种情况下,输出为:

int[] arr = new int[5];
for (int i = 0; i < 5; i++) {
    arr[i] = (i + 1);
    System.out.println(Arrays.toString(arr));
}

将数组[1, 0, 0, 0, 0] [1, 2, 0, 0, 0] [1, 2, 3, 0, 0] [1, 2, 3, 4, 0] [1, 2, 3, 4, 5] 的内容打印到arr循环中的控制台,您就会明白我的意思。

相关问题