-
Notifications
You must be signed in to change notification settings - Fork 0
/
age sort.cpp
61 lines (58 loc) · 1.08 KB
/
age sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include<bits/stdc++.h>
using namespace std;
void marge(int a[],int low,int mid,int high);
void margesort(int a[],int low,int high)
{
int mid;
if(low<high)
{
mid=(low+high)/2;
margesort(a,low,mid);
margesort(a,mid+1,high);
marge(a,low,mid,high);
}
}
void marge(int a[],int low,int mid,int high)
{
int n=high-low+1;
int c[n],index=0,l=low,r=mid+1;
for(;index<n && l<=mid && r<=high;index++)
{
c[index]=(a[l]<a[r]?a[l++]:a[r++]);
}
if(l<=mid)
{
while(l<=mid)
c[index++]=a[l++];
}
if(r<=high)
{
while(r<=high)
c[index++]=a[r++];
}
l=low;
for(index=0;index<n;index++)
{
a[low++]=c[index];
}
}
int main()
{
int n;
while(cin>>n,n)
{
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
margesort(a,0,n-1);
bool first=false;
for(int i=0;i<n;i++)
{
if(first)
cout<<' ';
first=true;
cout<<a[i];
}
cout<<endl;
}
}