检查循环条件是否应计入比较总数
Should checking loop conditions be counted towards total number of comparisons?
我已经实现了三种不同的排序算法,现在我想确认我计算比较总数的方法是正确的。在我看来,比较的数量不应该与条件分支相关联,因为如果不满足条件,仍然进行比较以比较值。我还使用相同的思维过程在退出循环条件的比较计数中添加一个。
我的算法的实现如下。我是否在每种情况下都正确设置了计数器?
插入排序
int insertionSort(double* A, int arrayLength) {
int count = 1; // Initialized 1 to count for exit conditions of for loop
for (int j = 1; j < arrayLength; j++) {
count++;
double key = A[j];
int i = j - 1;
while (i > -1 && A[i] > key) {
count++;
A[i + 1] = A[i];
i = i - 1;
}
count+=2 // Plus 2 for the while loop exit condition
A[i + 1] = key;
}
return count;
}
堆排序
void heapSort(double* A, int arrayLength, int* c) {
int heapSize = 0;
buildMaxHeap(A, arrayLength, &heapSize, c);
for (int i = arrayLength - 1; i > 0; i--) {
swap(A[0], A[i]);
heapSize = heapSize - 1;
maxHeapify(A, 0, &heapSize, c);
}
}
void buildMaxHeap(double* A, int arrayLength, int* heapSize, int* c) {
*heapSize = arrayLength - 1;
*c = *c + 1; // Counts comparison of loop for exit condition
for (int i = floor((arrayLength)/ 2); i > -1; i--) {
*c = *c + 1;
maxHeapify(A, i, heapSize, c);
}
}
void maxHeapify(double* A, int i, int* heapSize, int* c) {
int l = (2 * i) + 1;
int r = (2 * i) + 2;
int largest;
if (l <= *heapSize && A[l] > A[i])
largest = l;
else largest = i;
if (r <= *heapSize && A[r] > A[largest])
largest = r;
if (largest != i) {
swap(A[i], A[largest]);
maxHeapify(A, largest, heapSize, c);
}
*c = *c + 5;
}
快速排序
void quickSort(double* A, int p, int r, int* c) {
if (p < r) {
int q = partition(A, p, r, c);
quickSort(A, p, q - 1, c);
quickSort(A, q + 1, r, c);
}
*c = *c + 1;
}
int partition(double* A, int p, int r, int* c) {
double x = A[r];
int i = p - 1;
for (int j = p; j < r; j++) {
if (A[j] <= x) {
i = i + 1;
swap(A[i], A[j]);
}
*c = *c + 2;
}
*c = *c + 1 // Adding 1 for for loop exit condition
swap(A[i + 1], A[r]);
return i + 1;
}
如果你看看你的坚持排序
正如您已经输入计数 = 1 一样,因为对于 for 循环的退出条件的退出。
出于同样的原因,当 while 循环取消时,里面的 count++ 不会被执行,但进行了比较,这也是有道理的。但是你做一个计数+=2。为什么是2?这是有道理的,因为您在 while 循环中进行 2 次比较时添加了 2
个(i > -1 && A[i] > key)
- I>-1
- A[i]>键
但是,您需要在每次循环中将计数器增加 2 个,而 while 是正确的 2 个比较。
int insertionSort(double* A, int arrayLength) {
int count = 1; // Initialized 1 to count for exit conditions of for loop
for (int j = 1; j < arrayLength; j++) {
count++;
double key = A[j];
int i = j - 1;
while (i > -1 && A[i] > key) {
count++;
A[i + 1] = A[i];
i = i - 1;
}
count+2 // Plus 2 for the while loop exit condition
A[i + 1] = key;
}
return count;
}
同样,您也可以检查其他算法。
为了更好的方法,我建议你阅读任何一本关于算法分析的书的开头章节。他们通常会解释如何估计算法的运行时间,这将帮助您了解如何更好地分析。
相关文章:
- 对于循环变体比较
- 比较两个字符串后卡在无限循环中
- string1 == string2 和你自己的 for 循环比较有什么区别?
- 循环比较(优化)
- 比较嵌套 for 循环中的两个 wchar 数组?
- 使用循环进行词典字符串比较
- 如何使用 for 循环来比较不同大小的数组
- 包含比较的循环的自动矢量化
- 在 while 循环中使用 Module32Next() 时比较wchar_t
- C++和Java的字符串循环性能比较
- 在循环C 时进行比较
- 符号和未签名整数表达式之间的比较,以进行循环误差
- For 循环仅比较数组的第一部分而不是整个数组
- r语言 - C++ 或 Rcpp:两个不带循环的向量的比较
- C++:循环浏览地图并比较键
- 如何准确测量和比较opencl速度以实现循环功能的简单速度
- 比较二维数组中的数字时的无限循环
- C++ 比较 2 个不同列表中的值以使用 2 for 循环摆脱重复的数字。无法正确检测重复项
- C++ 中 for 循环中的比较运算符
- 比较循环的不同类型的C++