【发布时间】: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