歸併排序-JAVA實現

來源:互聯網
上載者:User

標籤:lock   i++   gen   輸出   記憶體   複製   比較   pack   之間   

  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 block101                     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實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.