程序員必知的8大排序(四)-------歸併排序,基數排序(java實現)

http://blog.csdn.net/pzhtpf/article/details/7560312

 

7、歸併排序

 

(1)基本排序:歸併(Merge)排序法是將兩個(或兩個以上)有序表合併成一個新的有序表,即把待排序序列分爲若干個子序列,每個子序列是有序的。然後再把有序子序列合併爲整體有序序列。

(2)實例:

(3)用java實現

  1. import java.util.Arrays;  
  2.   
  3. public class mergingSort {  
  4. int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};  
  5. public  mergingSort(){  
  6.     sort(a,0,a.length-1);  
  7.     for(int i=0;i<a.length;i++)  
  8.         System.out.println(a[i]);  
  9. }  
  10. public void sort(int[] data, int left, int right) {  
  11.     // TODO Auto-generated method stub  
  12.     if(left<right){  
  13.         //找出中間索引  
  14.         int center=(left+right)/2;  
  15.         //對左邊數組進行遞歸  
  16.         sort(data,left,center);  
  17.         //對右邊數組進行遞歸  
  18.         sort(data,center+1,right);  
  19.         //合併  
  20.         merge(data,left,center,right);  
  21.           
  22.     }  
  23. }  
  24. public void merge(int[] data, int left, int center, int right) {  
  25.     // TODO Auto-generated method stub  
  26.     int [] tmpArr=new int[data.length];  
  27.     int mid=center+1;  
  28.     //third記錄中間數組的索引  
  29.     int third=left;  
  30.     int tmp=left;  
  31.     while(left<=center&&mid<=right){  
  32.         //從兩個數組中取出最小的放入中間數組  
  33.         if(data[left]<=data[mid]){  
  34.             tmpArr[third++]=data[left++];  
  35.         }else{  
  36.             tmpArr[third++]=data[mid++];  
  37.         }  
  38.     }  
  39.     //剩餘部分依次放入中間數組  
  40.     while(mid<=right){  
  41.         tmpArr[third++]=data[mid++];  
  42.     }  
  43.     while(left<=center){  
  44.         tmpArr[third++]=data[left++];  
  45.     }  
  46.     //將中間數組中的內容複製回原數組  
  47.     while(tmp<=right){  
  48.         data[tmp]=tmpArr[tmp++];  
  49.     }  
  50.     System.out.println(Arrays.toString(data));  
  51. }  
  52.   
  53. }  
import java.util.Arrays;

public class mergingSort {
int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
public	mergingSort(){
	sort(a,0,a.length-1);
	for(int i=0;i<a.length;i++)
		System.out.println(a[i]);
}
public void sort(int[] data, int left, int right) {
    // TODO Auto-generated method stub
    if(left<right){
        //找出中間索引
        int center=(left+right)/2;
        //對左邊數組進行遞歸
        sort(data,left,center);
        //對右邊數組進行遞歸
        sort(data,center+1,right);
        //合併
        merge(data,left,center,right);
        
    }
}
public void merge(int[] data, int left, int center, int right) {
    // TODO Auto-generated method stub
    int [] tmpArr=new int[data.length];
    int mid=center+1;
    //third記錄中間數組的索引
    int third=left;
    int tmp=left;
    while(left<=center&&mid<=right){
        //從兩個數組中取出最小的放入中間數組
        if(data[left]<=data[mid]){
            tmpArr[third++]=data[left++];
        }else{
            tmpArr[third++]=data[mid++];
        }
    }
    //剩餘部分依次放入中間數組
    while(mid<=right){
        tmpArr[third++]=data[mid++];
    }
    while(left<=center){
        tmpArr[third++]=data[left++];
    }
    //將中間數組中的內容複製回原數組
    while(tmp<=right){
        data[tmp]=tmpArr[tmp++];
    }
    System.out.println(Arrays.toString(data));
}

}

 

</pre><div class="dp-highlighter bg_plain"><div class="bar"><div class="tools"><strong>[plain]</strong> <a target=_blank title="view plain" class="ViewSource" href="http://blog.csdn.net/pzhtpf/article/details/7560312#">view plain</a><span data-mod="popu_168"> <a target=_blank title="copy" class="CopyToClipboard" href="http://blog.csdn.net/pzhtpf/article/details/7560312#">copy</a></span><span data-mod="popu_169"> <a target=_blank title="print" class="PrintSource" href="http://blog.csdn.net/pzhtpf/article/details/7560312#">print</a></span><a target=_blank title="?" class="About" href="http://blog.csdn.net/pzhtpf/article/details/7560312#">?</a></div></div><ol><li class="alt"><span><span>  </span></span></li></ol><div class="save_code tracking-ad" style="display: none;" data-mod="popu_249"><a target=_blank href="javascript:;" target="_blank"><img src="http://static.blog.csdn.net/images/save_snippets_01.png" alt="" /></a></div></div><pre class="plain" style="display: none;" name="code">
8、基數排序
 
(1)基本思想:將所有待比較數值(正整數)統一爲同樣的數位長度,數位較短的數前面補零。然後,從最低位開始,依次進行一次排序。這樣從最低位排序一直到最高位排序完成以後,數列就變成一個有序序列。
(2)實例:

3)用java實現
  1. import java.util.ArrayList;  
  2.   
  3. import java.util.List;  
  4.   
  5.    
  6.   
  7. public class radixSort {  
  8.   
  9.          int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,101,56,17,18,23,34,15,35,25,53,51};  
  10.   
  11. public radixSort(){  
  12.   
  13.          sort(a);  
  14.   
  15.          for(int i=0;i<a.length;i++)  
  16.   
  17.                    System.out.println(a[i]);  
  18.   
  19. }  
  20.   
  21. public  void sort(int[] array){     
  22.   
  23.                       
  24.   
  25.                  //首先確定排序的趟數;     
  26.   
  27.         int max=array[0];     
  28.   
  29.         for(int i=1;i<array.length;i++){     
  30.   
  31.                     if(array[i]>max){     
  32.   
  33.                max=array[i];     
  34.   
  35.                     }     
  36.   
  37.                  }     
  38.   
  39.                       
  40.   
  41.         int time=0;     
  42.   
  43.                 //判斷位數;     
  44.   
  45.                  while(max>0){     
  46.   
  47.                     max/=10;     
  48.   
  49.                      time++;     
  50.   
  51.                  }     
  52.   
  53.                       
  54.   
  55.         //建立10個隊列;     
  56.   
  57.                  List<ArrayList> queue=new ArrayList<ArrayList>();     
  58.   
  59.                  for(int i=0;i<10;i++){     
  60.   
  61.                           ArrayList<Integer> queue1=new ArrayList<Integer>();   
  62.   
  63.                      queue.add(queue1);     
  64.   
  65.         }     
  66.   
  67.                      
  68.   
  69.                  //進行time次分配和收集;     
  70.   
  71.                  for(int i=0;i<time;i++){     
  72.   
  73.                           
  74.   
  75.                      //分配數組元素;     
  76.   
  77.                     for(int j=0;j<array.length;j++){     
  78.   
  79.                          //得到數字的第time+1位數;   
  80.   
  81.                              int x=array[j]%(int)Math.pow(10, i+1)/(int)Math.pow(10, i);  
  82.   
  83.                              ArrayList<Integer> queue2=queue.get(x);  
  84.   
  85.                              queue2.add(array[j]);  
  86.   
  87.                              queue.set(x, queue2);  
  88.   
  89.             }     
  90.   
  91.                      int count=0;//元素計數器;     
  92.   
  93.             //收集隊列元素;     
  94.   
  95.                      for(int k=0;k<10;k++){   
  96.   
  97.                 while(queue.get(k).size()>0){  
  98.   
  99.                          ArrayList<Integer> queue3=queue.get(k);  
  100.   
  101.                              array[count]=queue3.get(0);     
  102.   
  103.                              queue3.remove(0);  
  104.   
  105.                     count++;  
  106.   
  107.               }     
  108.   
  109.             }     
  110.   
  111.                }     
  112.   
  113.                       
  114.   
  115.    }    
  116.   
  117.    
  118.   
  119. }  
import java.util.ArrayList;

import java.util.List;

 

public class radixSort {

         int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,101,56,17,18,23,34,15,35,25,53,51};

public radixSort(){

         sort(a);

         for(int i=0;i<a.length;i++)

                   System.out.println(a[i]);

}

public  void sort(int[] array){   

                    

                 //首先確定排序的趟數;   

        int max=array[0];   

        for(int i=1;i<array.length;i++){   

                    if(array[i]>max){   

               max=array[i];   

                    }   

                 }   

                    

        int time=0;   

                //判斷位數;   

                 while(max>0){   

                    max/=10;   

                     time++;   

                 }   

                    

        //建立10個隊列;   

                 List<ArrayList> queue=new ArrayList<ArrayList>();   

                 for(int i=0;i<10;i++){   

                          ArrayList<Integer> queue1=new ArrayList<Integer>(); 

                     queue.add(queue1);   

        }   

                   

                 //進行time次分配和收集;   

                 for(int i=0;i<time;i++){   

                        

                     //分配數組元素;   

                    for(int j=0;j<array.length;j++){   

                         //得到數字的第time+1位數; 

                             int x=array[j]%(int)Math.pow(10, i+1)/(int)Math.pow(10, i);

                             ArrayList<Integer> queue2=queue.get(x);

                             queue2.add(array[j]);

                             queue.set(x, queue2);

            }   

                     int count=0;//元素計數器;   

            //收集隊列元素;   

                     for(int k=0;k<10;k++){ 

                while(queue.get(k).size()>0){

                         ArrayList<Integer> queue3=queue.get(k);

                             array[count]=queue3.get(0);   

                             queue3.remove(0);

                    count++;

              }   

            }   

               }   

                    

   }  

 

}

 

 

 

 
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章