简体   繁体   English

插入排序的具体运行时复杂度是多少?

[英]What is the specific runtime complexity of insertion sort?

Im just going over some basic sorting algorithms.我只是回顾了一些基本的排序算法。 I implemented the below insertion sort.我实现了下面的插入排序。

  public static int[] insertionSort(int[] arr){
      int I = 0;
      for(int i = 0; i < arr.length; i++){
          for(int j = 0; j < i; j++){
              if(arr[i] < arr[j]){
                  int temp = arr[i];
                  arr[i] = arr[j];
                  arr[j] = temp;
                  
              }
              I++;
          }
      }
      System.out.println(I);
      return arr;
  }

I prints out 4950 for a sized 100 array with 100 randomly generated integers. I为一个大小为 100 的数组打印出 4950,其中包含 100 个随机生成的整数。

I know the algorithm is considered O(n^2), but what would be the more arithmetically correct runtime?我知道该算法被认为是 O(n^2),但是在算术上更正确的运行时间是什么? If it was actually O(N^2) I im assuming, would print out 10,000 and not 4950.如果I假设它实际上是 O(N^2),将打印出 10,000 而不是 4950。

Big-Oh notation gives us how much work an algorithm must do as the input size grows bigger. Big-Oh 表示法为我们提供了算法在输入大小变大时必须做的工作量。 A single input test doesn't give enough information to verify the theoretical Big-Oh.单个输入测试没有提供足够的信息来验证理论上的 Big-Oh。 You should run the algorithm on arrays of different sizes from 100 to a million and graph the output with the size of the array as the x-variable and the number of steps that your code outputs as the y-variable.您应该在从 100 到 100 万的不同大小的 arrays 上运行算法,并绘制 output 图形,其中数组的大小作为 x 变量,代码输出的步数作为 y 变量。 When you do this, you will see that the graph is a parabola.执行此操作时,您会看到图形是抛物线。

You can use algebra to get an function in the form y = a*x^2 + b*x +c that fits as close as possible to this data.您可以使用代数来获得尽可能接近此数据的y = a*x^2 + b*x +c形式的 function。 But with Big-Oh notation, we don't care about the smaller terms because they grow insignificant compared to the x^2 part.但是使用 Big-Oh 表示法,我们不关心较小的术语,因为它们与x^2部分相比变得微不足道。 For example, when x = 10^3 , then x^2 = 10^6 which is much larger than b*x + c .例如,当x = 10^3时, x^2 = 10^6远大于b*x + c If x = 10^6 then x^2 = 10^12 which again is so much larger than b*x + c that we can ignore these smaller terms.如果x = 10^6那么x^2 = 10^12又比b*x + c ,我们可以忽略这些较小的项。

You can make the following observations: On the i th iteration of the outer loop, the inner loop runs i times, for i from 0 to n-1 where n is the length of the array.您可以进行以下观察: 在外循环的第i次迭代中,内循环运行i次,因为i从 0 到 n-1,其中 n 是数组的长度。

In total over the entire algorithm the inner loop runs T(n) times where在整个算法中,内循环总共运行 T(n) 次,其中

T(n) = 0 + 1 + 2 + ... + (n-1)

This is an arithmetic series and it's easy to prove the sum is equal to a second degree polynomial on n :这是一个算术级数,很容易证明和等于n上的二次多项式:

T(n) = n*(n-1)/2 = .5*n^2 - .5*n

For n = 100, the formula predicts the inner loop will run T(100) = 100*99/2 = 4950 times which matches what you calculated.对于 n = 100,公式预测内部循环将运行 T(100) = 100*99/2 = 4950 次,这与您的计算结果相匹配。

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

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