Pagini recente » Cod sursa (job #2965207) | Cod sursa (job #362199) | Cod sursa (job #1586457) | Cod sursa (job #421553) | Cod sursa (job #1984239)
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
ifstream in("algsort.in");
ofstream out("algsort.out");
#define ll long long
#define pb push_back
const int NMax = 5e5 + 5;
const int inf = 1e9 + 5;
int N;
int v[NMax];
void qSort(int,int);
int part(int,int);
int main() {
in>>N;
for (int i=1;i <= N;++i) {
in>>v[i];
}
srand(time(0));
qSort(1,N);
for (int i=1;i <= N;++i) {
out<<v[i]<<' ';
}
out<<'\n';
in.close();out.close();
return 0;
}
void qSort(int st,int dr) {
if (st >= dr) {
return;
}
int idx = part(st,dr);
qSort(st,idx-1);
qSort(idx+1,dr);
}
int part(int st,int dr) {
int idx = rand()%(dr-st+1) + st;
swap(v[st],v[idx]);
int i = st-1;
for (int j=st;j <= dr;++j) {
if (v[j] <= v[st]) {
swap(v[++i],v[j]);
}
}
swap(v[st],v[i]);
return i;
}