此合并排序算法执行多少个比较
How many compares does this Merge Sort algorithm do?
我想在此代码中添加compare++
,以计算此算法完成了多少次比较。 在 Merge(...)
中的第一个 while 循环以及 while 中的if
和else
中,每次执行时是否需要增加compare
计数?这些位置是否是唯一应该增加compare
的位置?(我在我认为它所属的地方添加了这个增量并注释掉了。 请忽略交换功能)
#include "MergeSort.h"
template<class ItemType>
void MergeClass<ItemType>::sort(ItemType values[], int first, int last)
// Post: The elements in values are sorted by key.
{
if (first < last)
{
int middle = (first + last) / 2;
sort(values, first, middle);
sort(values, middle + 1, last);
Merge(values, first, middle, middle + 1, last);
}
}
template<class ItemType>
void MergeClass<ItemType>::Merge(ItemType values[], int leftFirst, int leftLast,
int rightFirst, int rightLast)
// Post: values[leftFirst]..values[leftLast] and
// values[rightFirst]..values[rightLast] have been merged.
// values[leftFirst]..values[rightLast] are now sorted.
{
ItemType tempArray[5];
int index = leftFirst;
int saveFirst = leftFirst;
while ((leftFirst <= leftLast) && (rightFirst <= rightLast))
{
if (values[leftFirst] < values[rightFirst])
{
tempArray[index] = values[leftFirst];
leftFirst++;
//compare++;
}
else
{
tempArray[index] = values[rightFirst];
rightFirst++;
//compare++;
}
index++;
//compare++;
}
while (leftFirst <= leftLast)
// Copy remaining items from left half.
{
tempArray[index] = values[leftFirst];
leftFirst++;
index++;
}
while (rightFirst <= rightLast)
// Copy remaining items from right half.
{
tempArray[index] = values[rightFirst];
rightFirst++;
index++;
}
for (index = saveFirst; index <= rightLast; index++)
values[index] = tempArray[index];
}
template<class ItemType>
inline void MergeClass<ItemType>::Swap(ItemType& item1, ItemType& item2)
// Post: Contents of item1 and item2 have been swapped.
{
ItemType tempItem;
tempItem = item1;
item1 = item2;
item2 = tempItem;
}
template<class ItemType>
MergeClass<ItemType>::MergeClass()
{
compare = 0;
swap = 0;
}
template<class ItemType>
void MergeClass<ItemType>::sortPreformance()
{
cout << "Comparisons made: " << compare <<endl;
cout << "Swaps made: "<< swap <<endl;
}
如果它严格用于分析,我会将计数逻辑放在排序类之外。这类似于以下内容(仅计算std::sort
使用的比较和交换次数):
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
template<typename T>
struct CountingItem {
CountingItem(const T& val = T()) : val_(val) {}
bool operator<(const CountingItem<T>& rhs) const {
++compares;
return val_ < rhs.val_;
}
static size_t compares;
static size_t swaps;
T val_;
};
template<typename T>
size_t CountingItem<T>::compares = 0;
template<typename T>
size_t CountingItem<T>::swaps = 0;
template<typename T>
void swap(CountingItem<T>& a, CountingItem<T>& b) {
++CountingItem<T>::swaps;
std::swap(a, b);
}
int main()
{
const size_t num_items = 10000;
CountingItem<int> items[num_items];
for(int i = 0; i < num_items; i++) items[i] = rand() % 100;
sort(items, items+num_items);
cout << "Compares = " << CountingItem<int>::compares << endl;
cout << "Swaps = " << CountingItem<int>::swaps << endl;
// Reset CountingItem<int>::compares and swaps here if you're running another test
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 为什么比较运算符如此快速
- 我可以使用 g++ 进行三种比较 (<=>) 吗?
- 复制列表初始化的隐式转换的等级是多少
- while循环中while循环的时间复杂度是多少
- 比较字符数组
- 将模板化的类型与C++中的某些类/类型进行比较
- C++自定义比较函数
- 如何比较自定义类的std::变体
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- std::设置自定义比较器
- 布尔比较运算符是如何在C++中工作的
- C++将目录中的所有文件与::filesystem进行比较
- 如何检查一个c++字符串中有多少相同的字符/数字
- C++有多少类型的循环
- shell排序中的交换和比较
- 在这里,当我们比较 if(vc[i]==vc1[i]) 时,它是向量数组. 实际上比较的值是多少,
- 原子 RMW 操作和函数调用的比较成本是多少?
- 此合并排序算法执行多少个比较
- 在一棵有191个节点且高度为9的二叉树中找到一个键所需的最大比较次数是多少?