import java.util.Scanner;
public class Main{
static int a[] = new int[100];
static int b[] = new int[100];
static int n;
public static void merge(int low, int mid, int high){
int h=low, i=low, j=mid+1;
while(h<=mid&&j<=high){
if(a[h]<a[j]) b[i++]=a[h++];
else b[i++]=a[j++];
}
if(h>mid){
for(int k=j;k<=high;k++)
b[i++]=a[k];
}
else{
for(int k=h;k<=mid;k++)
b[i++]=a[k];
}
for(int k=low;k<=high;k++) a[k]=b[k];
}
public static void merge_sort(int low, int high){
if(low<high){
int mid = (low+high)/2;
merge_sort(low,mid);
merge_sort(mid+1,high);
merge(low,mid,high);
}
}
public static void main(String args[]){
Scanner cin = new Scanner(System.in);
while(cin.hasNext()){
n = cin.nextInt();
for(int i=1;i<=n;i++) a[i] = cin.nextInt();
merge_sort(0,n);
for(int i=1;i<=n;i++)
System.out.println(a[i]);
}
}
}
Merge Sort
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment