#include <iostream>
#include <vector>
class Sorter
{
public:
// 冒泡排序
bool bubbleSort(std::vector<int>& intVector)
{
if (intVector.empty())
{
return false;
}
int intSize = intVector.size();
for (int intOuter = 0; intOuter < intSize - 1; intOuter++)
{
for (int intInner = 0; intInner < intSize - intOuter - 1; intInner++)
{
if (intVector[intInner] > intVector[intInner + 1])
{
std::swap(intVector[intInner], intVector[intInner + 1]);
}
}
}
return true;
}
// 选择排序
bool selectionSort(std::vector<int>& intVector)
{
if (intVector.empty())
{
return false;
}
int intSize = intVector.size();
for (int intOuter = 0; intOuter < intSize - 1; intOuter++)
{
int intMinIndex = intOuter;
for (int intInner = intOuter + 1; intInner < intSize; intInner++)
{
if (intVector[intInner] < intVector[intMinIndex])
{
intMinIndex = intInner;
}
}
std::swap(intVector[intMinIndex], intVector[intOuter]);
}
return true;
}
// 插入排序
bool insertionSort(std::vector<int>& intVector)
{
if (intVector.empty())
{
return false;
}
int intSize = intVector.size();
for (int intOuter = 1; intOuter < intSize; intOuter++)
{
int intKey = intVector[intOuter];
int intInner = intOuter - 1;
while (intInner >= 0 && intVector[intInner] > intKey)
{
intVector[intInner + 1] = intVector[intInner];
intInner--;
}
intVector[intInner + 1] = intKey;
}
return true;
}
// 快速排序
bool quickSort(std::vector<int>& intVector, int intLow, int intHigh)
{
if (intVector.empty() || intLow < 0 || intHigh >= intVector.size() || intLow > intHigh)
{
return false;
}
if (intLow < intHigh)
{
int intPivotIndex = partition(intVector, intLow, intHigh);
quickSort(intVector, intLow, intPivotIndex - 1);
quickSort(intVector, intPivotIndex + 1, intHigh);
}
return true;
}
// 归并排序
bool mergeSort(std::vector<int>& intVector, int intLeft, int intRight)
{
if (intVector.empty() || intLeft < 0 || intRight >= intVector.size() || intLeft > intRight)
{
return false;
}
if (intLeft < intRight)
{
int intMiddle = intLeft + (intRight - intLeft) / 2;
mergeSort(intVector, intLeft, intMiddle);
mergeSort(intVector, intMiddle + 1, intRight);
merge(intVector, intLeft, intMiddle, intRight);
}
return true;
}
// 堆排序
bool heapSort(std::vector<int>& intVector)
{
if (intVector.empty())
{
return false;
}
int intSize = intVector.size();
for (int intIndex = intSize / 2 - 1; intIndex >= 0; intIndex--)
{
heapify(intVector, intSize, intIndex);
}
for (int intIndex = intSize - 1; intIndex > 0; intIndex--)
{
std::swap(intVector[0], intVector[intIndex]);
heapify(intVector, intIndex, 0);
}
return true;
}
private:
// 快速排序的辅助函数
int partition(std::vector<int>& intVector, int intLow, int intHigh)
{
int intPivot = intVector[intHigh];
int intIndex = intLow - 1;
for (int intInner = intLow; intInner < intHigh; intInner++)
{
if (intVector[intInner] <= intPivot)
{
intIndex++;
std::swap(intVector[intIndex], intVector[intInner]);
}
}
std::swap(intVector[intIndex + 1], intVector[intHigh]);
return intIndex + 1;
}
// 归并排序的辅助函数
void merge(std::vector<int>& intVector, int intLeft, int intMiddle, int intRight)
{
int intLeftSize = intMiddle - intLeft + 1;
int intRightSize = intRight - intMiddle;
std::vector<int> intLeftVector(intLeftSize), intRightVector(intRightSize);
for (int intIndex = 0; intIndex < intLeftSize; intIndex++)
{
intLeftVector[intIndex] = intVector[intLeft + intIndex];
}
for (int intIndex = 0; intIndex < intRightSize; intIndex++)
{
intRightVector[intIndex] = intVector[intMiddle + 1 + intIndex];
}
int intLeftIndex = 0, intRightIndex = 0, intMergedIndex = intLeft;
while (intLeftIndex < intLeftSize && intRightIndex < intRightSize)
{
if (intLeftVector[intLeftIndex] <= intRightVector[intRightIndex])
{
intVector[intMergedIndex] = intLeftVector[intLeftIndex];
intLeftIndex++;
}
else
{
intVector[intMergedIndex] = intRightVector[intRightIndex];
intRightIndex++;
}
intMergedIndex++;
}
while (intLeftIndex < intLeftSize)
{
intVector[intMergedIndex] = intLeftVector[intLeftIndex];
intLeftIndex++;
intMergedIndex++;
}
while (intRightIndex < intRightSize)
{
intVector[intMergedIndex] = intRightVector[intRightIndex];
intRightIndex++;
intMergedIndex++;
}
}
// 堆排序的辅助函数
void heapify(std::vector<int>& intVector, int intSize, int intIndex)
{
int intLargest = intIndex;
int intLeftChild = 2 * intIndex + 1;
int intRightChild = 2 * intIndex + 2;
if (intLeftChild < intSize && intVector[intLeftChild] > intVector[intLargest])
{
intLargest = intLeftChild;
}
if (intRightChild < intSize && intVector[intRightChild] > intVector[intLargest])
{
intLargest = intRightChild;
}
if (intLargest != intIndex)
{
std::swap(intVector[intIndex], intVector[intLargest]);
heapify(intVector, intSize, intLargest);
}
}
};
int main() {
Sorter sorter;
std::vector<int> arr = {64, 25, 12, 22, 11};
std::cout << "Original array: ";
for (int x : arr) std::cout << x << " ";
std::cout << std::endl;
sorter.bubbleSort(arr);
std::cout << "Bubble Sorted array: ";
for (int x : arr) std::cout << x << " ";
std::cout << std::endl;
arr = {64, 25, 12, 22, 11}; // 重置数组
sorter.selectionSort(arr);
std::cout << "Selection Sorted array: ";
for (int x : arr) std::cout << x << " ";
std::cout << std::endl;
arr = {64, 25, 12, 22, 11}; // 重置数组
sorter.insertionSort(arr);
std::cout << "Insertion Sorted array: ";
for (int x : arr) std::cout << x << " ";
std::cout << std::endl;
arr = {64, 25, 12, 22, 11}; // 重置数组
sorter.quickSort(arr, 0, arr.size() - 1);
std::cout << "Quick Sorted array: ";
for (int x : arr) std::cout << x << " ";
std::cout << std::endl;
arr = {64, 25, 12, 22, 11}; // 重置数组
sorter.mergeSort(arr, 0, arr.size() - 1);
std::cout << "Merge Sorted array: ";
for (int x : arr) std::cout << x << " ";
std::cout << std::endl;
arr = {64, 25, 12, 22, 11}; // 重置数组
sorter.heapSort(arr);
std::cout << "Heap Sorted array: ";
for (int x : arr) std::cout << x << " ";
std::cout << std::endl;
return 0;
}
代码说明
- Sorter类:包含六种排序方法:冒泡排序、选择排序、插入排序、快速排序、归并排序和堆排序。
- 排序方法:每种排序方法都对
std::vector<int>
类型的数组进行排序。 - 辅助函数:
partition
:用于快速排序的分区操作。merge
:用于归并排序的合并操作。heapify
:用于堆排序的堆化操作。
- 主函数:演示如何使用
Sorter
类对数组进行不同的排序操作。每次排序前会重置数组,确保每种排序方法在相同的初始条件下运行。
输出示例
运行程序后,将输出各排序方法排序后的数组:
plaintext
复制代码
Original array: 64 25 12 22 11
Bubble Sorted array: 11 12 22 25 64
Selection Sorted array: 11 12 22 25 64
Insertion Sorted array: 11 12 22 25 64
Quick Sorted array: 11 12 22 25 64
Merge Sorted array: 11 12 22 25 64
Heap Sorted array: 11 12 22 25 64
这个类提供了一个简单而实用的框架,用于演示各种常见排序算法的实现和比较。你可以根据需要扩展或修改这些排序算法。