#include<iostream>
using namespace std;
#define MaxNumber 100
int *Number = new int[MaxNumber];
int length;
void sort(int data[],int low,int high) {
int a = low;
int b = high;
int num = 0;
bool c = true;
while (a!=b)
{
if (c) {
if (data[a]>data[b]){
num = data[a];
data[a] = data[b];
data[b] = num;
c = false;
a++;
}
else {
b--;
}
}
else {
if (data[a] > data[b]) {
num = data[a];
data[a] = data[b];
data[b] = num;
c = true;
b--;
}
else {
a++;
}
}
cout << "输出数组:";
for (int i = 0; i < low; i++)
{
cout << " ";
}
for (int i = low; i <=high; i++)
{
cout << Number[i]<<" ";
}
cout << endl;
}
if (low != a) {
sort(Number, low, a-1);
}
if (b != high) {
sort(Number, b+1, high);
}
}
int main() {
cout << "输入数组长度:";
cin >> length;
cout << endl;
cout << "输入数组:";
for (int i = 0; i < length; i++)
{
cin >> Number[i];
}
cout << endl;
cout << "---------------------" << endl;
cout << " 数据录入完成 " << endl;
cout << "---------------------" << endl;
sort(Number, 0, length - 1);
cout << "---------------------" << endl;
cout << " 数据排序完成 " << endl;
cout << "---------------------" << endl;
cout << "输出数组:";
for (int i = 0; i < length; i++)
{
cout<< Number[i]<<" ";
}
cout << endl;
}
快速排序(C++实现)
最新推荐文章于 2024-10-08 00:17:03 发布
本文详细介绍了如何使用C++实现快速排序算法,并通过实例展示了排序过程。代码中定义了一个`sort`函数,该函数采用分治策略进行排序,并在每次交换元素后输出当前数组状态。最后,对输入的数组进行了排序并输出了排序结果。
摘要由CSDN通过智能技术生成