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,但我把冒泡排序的吊用注释掉之后就是正常的输出,请问我的代码问题出在哪里
修改 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做比较。
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!