c++ - 不正确的比较和交换计数器输出的Quicksort功能

标签 c++

我目前正在尝试实现一个c++程序,该程序在各种不同的排序方法中比较交换次数和比较次数。该程序似乎对所有排序方法(选择排序,插入排序)都运行良好,除了quicksort之外,无论数据大小或列表的顺序如何,其仅输出比较和交换计数为0。 Ive在下面包括了完整的程序。 quicksort函数肯定只在使用它的计数元素,这并不奇怪,因为它使用了外部比较和交换函数,它们每次调用时都会增加适当的计数器。任何帮助是极大的赞赏。

#include <cstdlib>
#include <getopt.h>
#include <iostream>
#include <string>
#include<limits>

using namespace std;
unsigned long long comparisons = 0;
unsigned long long swaps = 0;

bool comp_less(int a, int b){
    ++comparisons;

    if ( comparisons == std::numeric_limits<unsigned long long>::max() )
    {
        std::cout << "Number of comparisons reached max value. Resetting it 0.\n";
        swaps = 0;
    }

    return a < b;
}

void swap(int& a, int& b)
{
    ++swaps;

    if ( swaps == std::numeric_limits<unsigned long long>::max() )
    {
        std::cout << "Number of swaps reached max value. Resetting it 0.\n";
        swaps = 0;
    }

    int t = a;
    a = b;
    b = t;
}

void selectionSort(int *first, int *last)
{

    for(int *i = first; i < (last - 1); ++i){
        int *min = i;
        for(int *j = i + 1; j < last; ++j){
            if(comp_less(*j, *min)){
                min = j;
            }
        }
        swap(*i, *min);
    }

}


void insertionSort(int* first, int* last)
{
    for (int *i = first + 1; i < last; ++i)
    {
        int temp = *i;
        int *j;
        for (j = i; j > first && comp_less(temp, *(j - 1)); --j)
        {
            swap(*j, *(j - 1));
        }
        *j = temp;
    }
}
int *partition(int *first, int *last)
{

    int *pivot = last - 1;
    int *i = first;
    int *j = last - 1;
    for (;;)
    {
        while (comp_less(*i, *pivot) && i < last)
        {
            ++i;
        }
        while (*j >= *pivot && j > first)
        {
            --j;
        }
        if (i >= j)
            break;

        swap(*i, *j);
    }
    swap(*(last - 1), *i);
    return i;
}

void quickSort(int* first, int* last) {
    {
        if ((first - last) <= 1)
            return;
        int *pivot = partition(first, last);

        quickSort(first, pivot);
        quickSort(pivot + 1, last);
    }
}

int main(int argc, char** argv)
{
    string algorithm = "selection";
    string dataset = "random";

    for (int c; (c = getopt(argc, argv, "ravqsin")) != -1;) {
        switch (c) {
            case 'r':
                dataset = "random";
                break;
            case 'a':
                dataset = "sorted";
                break;
            case 'v':
                dataset = "reverse";
                break;
            case 'q':
                algorithm = "quicksort";
                break;
            case 's':
                algorithm = "selection";
                break;
            case 'i':
                algorithm = "insertion";
                break;
            case 'n':
                algorithm = "none";
                break;
        }
    }
    argc -= optind;
    argv += optind;

    const int size = argc > 0 ? atoi(argv[0]) : 10000;
    int* data = new int[size];

    if (dataset == "sorted") {
        for (int i = 0; i < size; ++i) {
            data[i] = i;
        }
    }
    else if (dataset == "reverse") {
        for (int i = 0; i < size; ++i) {
            data[i] = size - i - 1;
        }
    }
    else if (dataset == "random") {
        for (int i = 0; i < size; ++i) {
            data[i] = rand() % size;
        }
    }

    if (algorithm == "quicksort") {
        quickSort(data, data + size);
    }
    else if (algorithm == "selection") {
        selectionSort(data, data + size);
    }
    else if (algorithm == "insertion") {
        insertionSort(data, data + size);
    }
    else if (algorithm=="none"){
        cout<< "Oops!" <<'\n';
        exit(1);
    }

    cout << "OK" << '\n';
    cout << "Algorithm:   " << algorithm << '\n';
    cout << "Data set:    " << dataset << '\n';
    cout << "Size:        " << size << '\n';
    cout << "Swaps:       " << swaps << '\n';
    cout << "Comparisons: " << comparisons << '\n';
    return 0;
}

最佳答案

quickSort函数中,更改

if ((first - last) <= 1)


if ((last - first) <= 1)

关于c++ - 不正确的比较和交换计数器输出的Quicksort功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62401356/

相关文章:

c++ - 如何在磁盘上存储一棵树并使添加/删除/交换操作变得容易

c++ - LZ4 没有正确压缩字符串

c++ - Qt/c++ QGraphicsItem 绑定(bind)到自定义类

c++ - SFML 即使在覆盖虚拟绘图功能后也无法绘制对象

C++ 如何获取存储在字符串数组中的子文件夹名称?

c++ - 无法在 qt 中为 QWidget 的派生类设置样式表

c++ - LLVM:在程序中实现安全的多实例环境

c++向网络服务器上的API发送请求然后接收json数组响应?

Python 多线程性能 - 使用 C++ 代替?

c++ - 函数声明后的 "->"是什么?