#include <iostream>
#include <random>
#include <vector>
#include <algorithm>
#include <ctime>
using namespace std;
void rand(vector<int> &temp, int Num) {
srand(time(nullptr));//设置随机数种子
while (Num--) {
temp.push_back(rand() % 10000);
}
shuffle(temp.begin(), temp.end(), std::mt19937(std::random_device()()));
}
void Qsort(vector<int> &A, int L, int R) {
if (L >= R)return;
int static n = R - L;
int i = L, j = R;
int p = A[L];
swap(A[L],A[(R-L)/2+L]);
while (i < j) {
while (i < j && p < A[j]) j--;
while (i < j && p >= A[i]) i++;
swap(A[i], A[j]);
}
swap(A[L], A[i]);
if (n / 2 <= i - 1) Qsort(A, L, i - 1);
if (n / 2 >= i + 1) Qsort(A, i + 1, R);
}
int main() {
vector<int> A;
rand(A, 9999);
int sum = 0;
Qsort(A, 0, A.size() - 1);
int c = 0;
for (int i : A) {
sum += i;
c++;
cout << i << " ";
if (c == (A.size())/2) cout << endl;
}
cout << endl << sum;
}
递归快排 2016年408数据结构算法题
最新推荐文章于 2024-08-15 16:09:23 发布