Pagini recente » Cod sursa (job #2787939) | Cod sursa (job #508151) | Cod sursa (job #2677365) | Cod sursa (job #1016111) | Cod sursa (job #2197334)
#include <bits/stdc++.h>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
const int NMax = 500001;
int a[NMax];
int n;
int myPartition(int l, int r){
int y = rand();
y = y * rand();
int position = y % (r - l + 1) + l;
swap(a[position],a[r]);
int i = l - 1;
int j = l;
for(j = l; j < r; ++j){
if(a[j] <= a[r]){
swap(a[j],a[i + 1]);
i += 1;
}
}
swap(a[i + 1], a[r]);
return i + 1;
}
void myQuickSort(int l, int r){
if(l >= r){
return;
}
int position = myPartition(l,r);
myQuickSort(l, position - 1);
myQuickSort(position + 1, r);
}
int main()
{
f >> n;
srand(time(NULL));
for(int i = 1; i <= n; ++i){
f >> a[i];
}
myQuickSort(1, n);
for(int i = 1; i <= n; ++i){
g << a[i] << ' ';
}
return 0;
}