感觉还是很假的java版本的归并排序
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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | import java.util.*; /** * * @author yanglingfeng */ public class Main { /** * @param args the command line arguments */ private static void add(int[] a,int l1,int h1,int l2,int h2) { int[] t; int i,j,k; i=l1; j=l2; k=0; t=new int[h2-l1+1]; while(i<=h1&&j<=h2) { if(a[i]<a[j]) { t[k++]=a[i++]; }else { t[k++]=a[j++]; } } while(i<=h1) { t[k++]=a[i++]; } while(j<=h2) t[k++]=a[j++]; for(i=l1,j=0;i<=h2;i++,j++) { a[i]=t[j]; } return ; } private static void ms(int[] a,int low,int high) { int mid=(low+high)/2; if(low<high) { ms(a,low,mid); ms(a,mid+1,high); add(a,low,mid,mid+1,high); } return ; } public static void main(String[] args) { // TODO code application logic here int k; Scanner read=new Scanner(System.in); while(read.hasNextInt()) { k=read.nextInt(); int[] w=new int[k]; for(int i=0;i<k;i++) { w[i]=read.nextInt(); } ms(w,0,k-1); for(int i=0;i<k;i++) { System.out.print(w[i]+""); } System.out.println(); } } } |
