1. 程式人生 > >歸並排序-JAVA實現

歸並排序-JAVA實現

int stub print 並排 n) 部分 操作 ges void

  1 package com.iloveu.xxx;
  2 
  3 public class MergeSort {
  4     
  5     static final int SIZE = 15;
  6     
  7     static void mergeOne(int a[],int b[],int n,int len)
  8     {
  9         int i,j,k,s,e;
 10         s=0;
 11         while(s+len<n){
 12             e = s+2*len-1;
 13             if
(e>=n){//最後一段可能少於len個節點 14 e = n -1; 15 } 16 //相鄰有序段合並 17 k=s; 18 i=s; 19 j=s+len; 20 while(i<s+len && j<=e){//如果兩個有序表都未結束時,循環比較 21 if(a[i]<=a[j]){//如果較小的元素復制到數組b中 22 b[k++]=a[i++];
23 }else{ 24 b[k++]=a[j++]; 25 } 26 } 27 while(i<s+len){//未合並的部分復制到數組b中 28 b[k++]=a[i++]; 29 } 30 while(j<=e){//未合並的部分復制到數組b中 31 b[k++]=a[j++]; 32 33
} 34 s=e+1;//下一對有序段中左段的開始下標 35 } 36 if(s<n){//將剩余的一個有序段從數組a中復制到數組b中 37 for(;s<n;s++){ 38 b[s] = a[s]; 39 } 40 41 } 42 } 43 44 static void mergeSort(int a[],int n)//合並排序 45 { 46 int h,count,len,f; 47 48 count = 0;//排序步驟 49 len = 1;//有序序列的長度 50 f = 0;//變量f作標誌 51 52 int[] p = new int[n]; 53 while(len<n){ 54 if(f==1){//交替在a和p之間合並 55 mergeOne(p,a,n,len);//p合並到a 56 }else{ 57 mergeOne(a,p,n,len);//a合並到p 58 } 59 len = len*2;//增加有序序列長度 60 f=1-f;//使f值在0和1之間切換 61 62 count++; 63 System.out.printf("第"+count+"步排序結果:");//輸出每步排序的結果 64 for(h=0;h<SIZE;h++){ 65 System.out.printf(" "+a[h]); 66 67 } 68 System.out.printf("\n"); 69 } 70 if(f==1){//如果進行了排序 71 for(h=0;h<n;h++){//將內存p中的數據復制回數組a 72 a[h]=p[h]; 73 } 74 } 75 } 76 77 public static void main(String[] args) { 78 // TODO Auto-generated method stub 79 int[] shuzu=new int[SIZE]; 80 int i; 81 82 for(i=0;i<SIZE;i++){ 83 shuzu[i] = (int) (100+Math.random()*(100+1));//初始化數組 84 } 85 86 System.out.print("排序前的數組為:\n");//輸出排序前的數組 87 for(i=0;i<SIZE;i++){ 88 System.out.print(shuzu[i]+" "); 89 } 90 System.out.print("\n"); 91 92 mergeSort(shuzu,SIZE);//排序操作 93 94 System.out.print("排序後的數組為:\n"); 95 for(i=0;i<SIZE;i++){ 96 System.out.print(shuzu[i]+" ");//輸出排序後的數組 97 try { 98 Thread.sleep(1000); 99 } catch (InterruptedException e) { 100 // TODO Auto-generated catch block 101 e.printStackTrace(); 102 } 103 } 104 System.out.print("\n"); 105 } 106 107 108 }

package com.iloveu.xxx;
public class MergeSort {static final int SIZE = 15;static void mergeOne(int a[],int b[],int n,int len){int i,j,k,s,e;s=0;while(s+len<n){e = s+2*len-1;if(e>=n){//最後一段可能少於len個節點e = n -1;}//相鄰有序段合並k=s;i=s;j=s+len;while(i<s+len && j<=e){//如果兩個有序表都未結束時,循環比較if(a[i]<=a[j]){//如果較小的元素復制到數組b中b[k++]=a[i++];}else{b[k++]=a[j++];}}while(i<s+len){//未合並的部分復制到數組b中b[k++]=a[i++];}while(j<=e){//未合並的部分復制到數組b中b[k++]=a[j++];}s=e+1;//下一對有序段中左段的開始下標}if(s<n){//將剩余的一個有序段從數組a中復制到數組b中for(;s<n;s++){b[s] = a[s];}}}static void mergeSort(int a[],int n)//合並排序{int h,count,len,f;count = 0;//排序步驟len = 1;//有序序列的長度f = 0;//變量f作標誌int[] p = new int[n];while(len<n){if(f==1){//交替在a和p之間合並mergeOne(p,a,n,len);//p合並到a}else{mergeOne(a,p,n,len);//a合並到p}len = len*2;//增加有序序列長度f=1-f;//使f值在0和1之間切換count++;System.out.printf("第"+count+"步排序結果:");//輸出每步排序的結果for(h=0;h<SIZE;h++){System.out.printf(" "+a[h]);}System.out.printf("\n");}if(f==1){//如果進行了排序for(h=0;h<n;h++){//將內存p中的數據復制回數組aa[h]=p[h];}}}
public static void main(String[] args) {// TODO Auto-generated method stubint[] shuzu=new int[SIZE];int i;for(i=0;i<SIZE;i++){shuzu[i] = (int) (100+Math.random()*(100+1));//初始化數組}System.out.print("排序前的數組為:\n");//輸出排序前的數組for(i=0;i<SIZE;i++){System.out.print(shuzu[i]+" ");}System.out.print("\n");mergeSort(shuzu,SIZE);//排序操作System.out.print("排序後的數組為:\n");for(i=0;i<SIZE;i++){System.out.print(shuzu[i]+" ");//輸出排序後的數組try {Thread.sleep(1000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}System.out.print("\n");}

}

歸並排序-JAVA實現