【问题标题】:Temporal complexity on mergesort is constant归并排序的时间复杂度是恒定的
【发布时间】:2020-05-19 10:39:10
【问题描述】:

我想尝试做一个 C 实现,但结果很奇怪,因为当涉及到合并排序算法时,给定数组的长度(即使它的元素由于 rand() 是伪随机的)。每次它完成运行时,复杂性实际上是相同的。我知道,试图以这种方式理解“问题”并不容易,所以这是我从互联网上编写/复制的代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DIM 1000000

void merge(int*, int, int, int);
void mergesort(int*, int, int);
int complexity=0; //Complexity is the variable that counts how many times the program goes through the relevant cycles

int main(int argc, char *argv[]){
  int v[DIM], i;
  time_t t;
  srand((unsigned)time(&t));
  for(i=0; i<DIM; i++){
    v[i]=rand()%100;
    printf("%d\n", v[i]);
  }
  mergesort(v, 0, DIM-1);
  for(i=0; i<DIM; i++)
    printf("%d\t", v[i]);
  printf("\n");
  printf("iterations: %d\n", complexity);
  return 0;
}

void mergesort(int v[], int lo, int hi){
  int mid;
  if(lo<hi){
    mid=(lo+hi)/2;
    mergesort(v, lo, mid);
    mergesort(v, mid+1, hi);
    merge(v, lo, mid, hi);
  }
  return;
}

//This implementation is not actually mine, I got it from a site because I couldn't figure out why mine wouldn't run and order the input
void merge(int v[], int p, int q, int r) {
  int n1, n2, i, j, k, *L, *M;
  n1 = q - p + 1;
  n2 = r - q;
  //creation and initialization of the left and right vectors
  L=(int*)malloc(n1*sizeof(int));
  M=(int*)malloc(n2*sizeof(int));
  for (i = 0; i < n1; i++){
    L[i] = v[p + i];
    complexity++;
  }
  for (j = 0; j < n2; j++){
    M[j] = v[q + 1 + j];
    complexity++;
  }
  //merging section
  i = 0;
  j = 0;
  k = p;
  while (i < n1 && j < n2) {
    if (L[i] <= M[j]) {
      v[k] = L[i];
      i++;
      complexity++;
    } else {
      v[k] = M[j];
      j++;
      complexity++;
    }
    k++;
  }
  //from what I understood this should be the section where what is left out gets copied inside        the remaining spots
  while (i < n1) {
    v[k] = L[i];
    i++;
    k++;
    complexity++;
  }
  while (j < n2) {
    v[k] = M[j];
    j++;
    k++;
    complexity++;
  }
  return;
}

我将把一张图片留给我在这里用各种排序算法做的一些试验

问题来了:让计算时间复杂度的变量保持不变是否正常?我最初的想法是,这是由于计数器的错误实现,我不知道如何证明它,只是因为我对算法的了解不是那么强。 如果这最终是正确的答案,您能否指导我实现计数器的实现(不太复杂但仍然有效)以更精确地评​​估时间复杂度?

编辑:我上传的excel屏幕截图的A到I列对应随机生成的数组的长度,值为:100、500、1000、5000、10000、50000、1000000。

【问题讨论】:

  • 你的意思是迭代计数器complexity对于给定的输入大小有一个常数值,与输入数据的实际值无关?
  • @Hulk,就是这样。例如,给定一个包含 100 个元素的向量,输出始终是 1344 次循环迭代,或者对于 500 个向量,输出是 8976 个循环,与向量内的实际值无关。我忘了在图像中包含输入的长度,它们是:100、500、1000、5000、10000、50000、1000000

标签: c sorting time-complexity array-algorithms


【解决方案1】:

无论数组的内容如何,​​merge 函数都会将complexity 递增2(r-p+1)。由于merge 函数是代码中唯一依赖于数组内容的部分,这表明complexity 变量总体上递增了固定次数。

下面是为什么merge 函数将complexity 递增2(r-p+1) 的草图:

此块将其递增n1

for (i = 0; i < n1; i++){
    L[i] = v[p + i];
    complexity++;
}

n2的此块:

for (j = 0; j < n2; j++){
    M[j] = v[q + 1 + j];
    complexity++;
}

在剩下的代码中,ij 从 0 开始,在每一步中,ij 增加 1,complexity 增加。当函数返回时,in1 并且 jn2,所以这会在 complexity 中添加另一个 n1+n2

由于n1 = q-p+1n2 = r-q,总体而言merge 函数将复杂度增加了2*n1 + 2*n2 = 2(q-p+1+r-q) = 2(r-p+1)

【讨论】:

  • 所以在理论上,我实现这个计数器来跟踪迭代次数是正确的,并且我在网络上获取的合并函数(以及一般的合并排序心态)不依赖于输入向量内的实际值;比如说,快速排序和插入排序(在我的测试期间,两者都给了我相同输入长度的周期数略有变化)。这有意义吗?
猜你喜欢
  • 2023-01-18
  • 2015-07-22
  • 1970-01-01
  • 1970-01-01
  • 2019-09-06
  • 2017-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多