Pagini recente » Cod sursa (job #61217) | Cod sursa (job #1325647) | Cod sursa (job #1166910) | Cod sursa (job #1587126) | Cod sursa (job #966346)
Cod sursa(job #966346)
//mergesort, silly implemetation
#include <fstream>
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
const int Nmax = 500009;
int N; int A[Nmax];
void Read(){
fin >> N;
for(int i = 1; i <= N; ++i)
fin >> A[i];
}
void Combine(int A[], int Left, int Mid, int Right){
int L = Left; int B[Nmax]; int M = Mid;
for(int i = L; i <= Right; ++i){
if(A[Left] >= A[Mid] || Left == M)
B[i] = A[Mid++];
else B[i] = A[Left++];
}
for(int i = L; i <= Right; ++i) A[i] = B[i];
}
void Divide(int A[], int Left, int Right){
int Mid = ((Left + Right) >> 1);
if(Right <= Left) return ;
Divide (A, Left, Mid);
Divide (A, Mid + 1, Right);
Combine (A, Left, Mid + 1, Right);
}
void Print (){
for(int i = 1; i <= N; ++i)
fout << A[i] <<" ";
}
int main(){
Read ();
Divide (A, 1, N);
Print ();
return 0;
}