Pagini recente » Cod sursa (job #2232186) | Cod sursa (job #1391298) | Cod sursa (job #2307468) | Cod sursa (job #10641) | Cod sursa (job #514420)
Cod sursa(job #514420)
#include <fstream>
#include <cstdlib>
#include <algorithm>
#include <ctime>
using namespace std;
int a[500001];
int Partition(int st, int dr){
int p=rand()&(dr-st);
p+=st;
//int p = ( st + dr ) / 2;
swap(a[st],a[p]);
p=a[st];
int i, j;
i=st, j=dr;
while(i<j){
if ( a[i]>p ){
swap(a[i],a[j]);
j--;
}
else ++ i;
}
if (a[i]>p){
--j, -- i;
}
swap(a[st],a[i]);
return i;
}
void quick(int l, int r){
if(r - l > 0){
int q = Partition(l, r);
quick(l, q-1);
quick(q+1, r);
}
}
int main(){
ios_base::sync_with_stdio(false);
srand(time(NULL));
ifstream f("algsort.in");
ofstream g("algsort.out");
int i = 0, n;
f >> n;
for(i = 0; i < n; i++)
f >> a [ i ];
quick(0, n-1);
for(i = 0; i < n; i++)
g<< a [ i ] << " ";
g << '\n';
return 0;
}