归并排序,基数排序

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

 

7、归并排序

 

(1)基本排序:归并(Merge)排序法是将两个(或两个以上)有序表合并成一个新的有序表,即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列。

(2)实例:

(3)用java实现

[plain]  view plain  copy
 
  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. }  
[plain]  view plain  copy
 
  1.   
[plain]  view plain  copy
 
  1.   
8、基数排序
 
(1)基本思想:将所有待比较数值(正整数)统一为同样的数位长度,数位较短的数前面补零。然后,从最低位开始,依次进行一次排序。这样从最低位排序一直到最高位排序完成以后,数列就变成一个有序序列。
(2)实例:

3)用java实现
[plain]  view plain  copy
 
  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. }  

 

猜你喜欢

转载自1028826685.iteye.com/blog/2309237