#include <iostream>
#include <vector>
#include <ctime>
using namespace std;
int get_mid(vector<int> &b, int left, int right)
{
int pivot = b[left];
while (left < right)
{
while (b[right] >= pivot && left < right)
{
right--;
}
b[left] = b[right];
while (b[left] <= pivot && left < right)
{
left++;
}
b[right] = b[left];
}
b[left] = pivot;
return left;
}
void quick_sort(vector<int> &b, int left, int right)
{
if (left < right)
{
int mid = get_mid(b, left, right);
quick_sort(b, left, mid - 1);
quick_sort(b, mid + 1, right);
}
}
int main()
{
srand((unsigned int)time(NULL));
vector<int> d;
int n = rand() % 101 + 2;
for (int i = 0; i < n; i++)
{
int score = rand() % 201 + 60;
// cout << score << " "; // 60 ~ 100
d.push_back(score);
}
//int c[10] = {5, 4, 7, 2, 9, 5, 1, 77, 33, 12};
quick_sort(d, 0, n - 1);
for (vector<int>::iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
return 0;
}
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交