Mai intai trebuie sa te autentifici.
Cod sursa(job #1660367)
Utilizator | Data | 23 martie 2016 00:12:02 | |
---|---|---|---|
Problema | Sortare prin comparare | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 0.88 kb |
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
ifstream in("algsort.in");
ofstream out("algsort.out");
int *a;
int n;
void quicksort(int x,int y)
{
if(x<y)
{
int i =x,j=y,pivot = a[(x+y)/2];
while(i<j)
{
while(a[i] < pivot)
i++;
while(a[j] > pivot)
j--;
if(i<=j)
{
swap(a[i],a[j]);
i++;
j--;
}
}
if(x < j)
quicksort(x,j);
if(i<y)
quicksort(i,y);
}
}
int main()
{
in>>n;
a = new int[n+1];
for(int i=1;i<=n;i++)
in>>a[i];
in.close();
///
quicksort(1,n);
///
for(int i=1;i<=n;i++)
out<<a[i]<<" ";
delete[] a;
out.close();
return 0;
}