#include<stdio.h>
#include<iostream>
#define MAX 100
using namespace std;
void quick_sort(int *arr,int left, int right) {
if (right < left)
return;
int l=left;
int r=right;
int temp = arr[l];
while (l!=r )
{
while (arr[r] >= temp && r > l) {
r--;
}
if (r > l) {
int k = arr[l];
arr[l] = arr[r];
arr[r] = k;
l++;
}
while (arr[l] <= temp && r > l)
{
l++;
}
if (r > l) {
int p = arr[l];
arr[l] = arr[r];
arr[r] = p;
r--;
}
}
quick_sort(arr, left, l-1);
quick_sort(arr, l+1, right);
}
int main() {
int a[MAX];
srand(time(0));//获得100个随机数并存入数组
for (int i = 0; i < MAX; i++) {
a[i] = rand() % 101;
}
/*for (int i = 1; i <= MAX; i++) {
cout << a[i-1] << " ";
if ((i % 10) == 0) {
cout << endl;
}
}*/
quick_sort(a, 0, MAX-1);
for (int n = 0; n < MAX; n++) {
cout << a[n] << " ";
}
cout << endl;
return 0;
}
使用C++编写快速排序
最新推荐文章于 2021-06-15 17:04:59 发布