内排序性能测试代码问题

main.cpp
#include<iostream>
#include"swap.h"
#include"InsertSort.h"//直接插入排序
#include"ShellSort.h"//希尔排序
#include"SelectSort.h"//直接选择排序
#include"HeapifySort.h"//堆排序
#include"BubbleSort.h"//冒泡排序
#include"QuickSort.h"//快速排序
#include<cstdlib>//包含srand(),rand()
#include<ctime>//包含time()
#include<chrono>
#include<thread>
using namespace std;
const int testDataCount = 5;  // 测试数据组数
const int arrayLength = 10;  // 数组长度

//生成随机数组
void createRandom(int* arr, int length)
{
    srand(time(NULL));//生成随机数种子
    for (int i = 0; i < length; i++)
    {
        arr[i] = rand() % arrayLength;//随机生成0-99
        cout << arr[i] << " ";
    }
    cout << endl;
}

//输出结果
void printResults(const string& Name, int comparisons, int moves)
{
    cout << "排序方法: " <<Name << endl;
    cout << "关键字比较次数: " << comparisons << endl;
    cout << "关键字移动次数: " << moves << endl;
    cout << endl;
}
int main()
{

    int testData[testDataCount][arrayLength];  // 存储测试数据

    
    

    for (int i = 0; i < testDataCount; i++)
    {
        // 生成测试数据      
        createRandom(testData[i], arrayLength);
       
        // 运行排序算法并输出结果
        cout << "测试数组" << i + 1 << endl;
        //冒泡排序
        comparisons5 = 0;
        moves5 = 0;
        BubbleSort(testData[i], arrayLength, comparisons5, moves5);
        printResults("冒泡排序", comparisons5, moves5);
        //快速排序
        comparisons6 = 0;
        moves6 = 0;
        QuickSort(testData[i], 0, arrayLength - 1, comparisons6, moves6);
        printResults("快速排序", comparisons6, moves6);

        //延迟一段时间,防止每次生成的数据一样
        this_thread::sleep_for(chrono::milliseconds(1000));
    }
}
BubbleSort.h
#pragma once
extern int comparisons5;
extern int moves5;
void BubbleSort(int arr[], int n, int& comparisons5, int& moves5);
BubbleSort.cpp
#include"BubbleSort.h"
#include"swap.h"
int comparisons5 = 0;
int moves5 = 0;
void BubbleSort(int arr[], int n, int& comparisons5, int& moves5) 
{
    for (int i = 0; i < n - 1; i++)
    {
        for (int j = 0; j < n - i - 1; j++)
        {
            if (arr[j] > arr[j + 1]) {
                swap(arr[j], arr[j + 1]);
                moves5 += 3;//swap函数关键字移动三次
            }
            comparisons5++;//关键字比较一次
        }
    }
}
QuickSort.h
#pragma once
extern int comparisons6;
extern int moves6;
void QuickSort(int arr[], int low, int high, int& comparisons6, int& moves6);


QuickSort.cpp
#include"QuickSort.h"
#include"swap.h"
int comparisons6 = 0;
int moves6 = 0;

void QuickSort(int arr[], int low, int high, int& comparisons6, int& moves6)
{
    int i = low, j = high;
    int temp = arr[low];//取第一个元素为标准数据元素
    while (i < j)
    {
        while (i < j && temp <= arr[j]) //在数组的右端扫描 
        {
            j--;
            comparisons6++;//关键字比较一次
        }
        if (i < j)
        {
            arr[i] = arr[j];
            i++;
            moves6++;//关键字移动一次
        }

        while (i < j && arr[i] < temp) //在数组的左端扫描
        {
            i++;
            comparisons6++;//关键字比较一次 
        }
        if (i < j)
        {
            arr[j] = arr[i];
            j--;
            moves6++;//关键字移动一次
        }
    }
    arr[i] = temp;
    moves6++;//关键字移动一次
    if (low < i) QuickSort(arr, low, i - 1, comparisons6, moves6);//对左端子集合进行递归
    if (i < high) QuickSort(arr, j + 1, high, comparisons6, moves6);//对右端子集合进行递归
}
swap.h
void swap(int& a, int& b)
{
    int temp = a;
    a = b;
    b = temp;
}

不知道为什么,我每次快速排序输出的都是45和10,但我把冒泡排序的吊用注释掉之后就是正常的输出,请问我的代码问题出在哪里

基于Monster 组和GPT的调写:

  • 在你的代码中,你在进行快速排序之前使用了冒泡排序,这将导致数组已经被排序过。因此,当你试图对已经排序过的数组进行快速排序时,你得到的结果可能会与预期的不同。
  • 每次排序后,你应该使用一份新的、未排序的数组来进行下一个排序操作。你可以尝试在进行每种排序之前先复制一份原始的测试数组,这样每次排序都是在原始的、未排序的数组上进行的。这样可以确保每种排序方法都是在相同的、未排序的数组上进行操作,从而得到准确的比较结果。

修改 main 函数中的代码,添加对每种排序算法使用新的数组副本:

int main()
{
    int testData[testDataCount][arrayLength];  // 存储测试数据
    int testDataCopy[arrayLength];  // 存储测试数据副本
 
    for (int i = 0; i < testDataCount; i++)
    {
        // 生成测试数据      
        createRandom(testData[i], arrayLength);
       
        // 运行排序算法并输出结果
        cout << "测试数组" << i + 1 << endl;
        //冒泡排序
        memcpy(testDataCopy, testData[i], sizeof(testData[i])); // 复制原始数据
        comparisons5 = 0;
        moves5 = 0;
        BubbleSort(testDataCopy, arrayLength, comparisons5, moves5);
        printResults("冒泡排序", comparisons5, moves5);
        //快速排序
        memcpy(testDataCopy, testData[i], sizeof(testData[i])); // 复制原始数据
        comparisons6 = 0;
        moves6 = 0;
        QuickSort(testDataCopy, 0, arrayLength - 1, comparisons6, moves6);
        printResults("快速排序", comparisons6, moves6);
 
        //延迟一段时间,防止每次生成的数据一样
        this_thread::sleep_for(chrono::milliseconds(1000));
    }
}

注意这个解决方案需要在你的程序中包含 <cstring> 库以使用 memcpy 函数。你可以在 main.cpp 文件的顶部添加 #include<cstring> 来包含这个库。

1.在 main 函数中调用 QuickSort 时,传递的参数是否正确,尤其是 low 和 high。

2.在 QuickSort 函数中,当 low < i 时递归调用 QuickSort 函数时,第二个参数应该是 i 而不是 i - 1,因为 i 已经被包含在了左子数组中。

3.在 QuickSort 函数中,while 循环条件中 temp <= arr[j] 应该改为 temp >= arr[j],因为选取的标准数据元素是第一个元素,arr[j]应该与temp做比较。

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632