#include<iostream>
#include<time.h>
#define size 10
using namespace std;
void Qsort(int a[], int low, int high)
{
if (low>=high)
{
return;
}
int first = low;
int last = high;
int key = a[first];/*用字表的第一个记录作为枢轴*/
while (first<last)
{
while (first<last&&a[last]>=key)
{
--last;
}
a[first] = a[last];/*将比第一个小的移动到最低端*/
while (first<last&&a[first]<=key)
{
++first;
}
a[last] = a[first];//将比第一个大的移到高端
}
a[first] = key;//轴记录到位
Qsort(a, low, first - 1);
Qsort(a, first + 1, high);
}
int main()
{
srand(time(NULL));
int arr[size];
for (int i = 0; i < size; i++)
{
arr[i] = rand() / 1000 + 100;
}
cout << "排序前的数组:" << endl;
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
Qsort(arr, 0, size - 1);
cout << endl;
cout << "排序后的数组:" << endl;
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
system("pause");
}
快速排序
最新推荐文章于 2023-12-01 23:14:38 发布