Nu aveti permisiuni pentru a descarca fisierul grader_test1.in
Cod sursa(job #1218530)
| Utilizator | Data | 11 august 2014 16:52:00 | |
|---|---|---|---|
| Problema | Sortare prin comparare | Scor | 0 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 0.6 kb |
#include <iostream>
#include <fstream>
using namespace std;
ifstream inFile("algsort.in");
ofstream outFile("algsort.out");
int n, A[500003];
int partition(int *A, int st, int dr)
{
int i=st, j=dr;
int pivot=A[(i+j)/2];
while(i<j){
while(A[i]<pivot) i++;
while(A[j]>pivot) j--;
if(i<j) swap(A[i],A[j]);
}
return i;
}
void q_sort(int *A, int st, int dr)
{
if(st<dr){
int p=partition(A,st,dr);
q_sort(A,st,p-1);
q_sort(A,p+1,dr);
}
}
int main()
{
inFile >> n;
for(int i=0;i<n;i++) inFile >> A[i];
q_sort(A,0,n-1);
for(int i=0;i<n;i++) outFile << A[i] << " ";
}
