c++ - 这个归并排序算法做了多少次比较?

标签 c++ algorithm sorting data-structures

我想将 compare++ 添加到此代码中,以计算此算法完成了多少次比较。 compare 的计数是否需要在每次执行 Merge(...)if 中的第一个 while 循环时递增else 里面的 while?这些是唯一应该增加 compare 的位置吗? (我把这个增量加在我认为应该的地方,然后注释掉了,请忽略swap函数)

#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
}

关于c++ - 这个归并排序算法做了多少次比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13708520/

相关文章:

c++ - 为什么 C++ vector 会导致 oom-killer?

c++ - 获取已在模板中延迟的类型

algorithm - 动态最短路径

algorithm - 是否可以将所有递归函数重写为尾递归?

java - 用于在一段时间之前驱逐事件的有效数据结构?

c++ - 日期格式的时间标记?

c++ - MFC slider +编辑+微调模式

python - 在 Python 中计算以 10 为底的对数的算法

c - 如何对表示时间表的数组结构对象进行排序

bash - 按日期将文件分类到子文件夹中 - bash