分享

几种常用排序算法

 桑枯海 2013-04-18
最近刚上班,在公司每天除了学习业务以外就是打打酱油,利用空余时间整理了几个排序算法
Java代码  收藏代码
  1. /** 
  2.  * @author txin0814 E-mail:txin0814@sina.com 
  3.  * @version 1.0 
  4.  * @date Apr 1, 2011 2:28:06 PM 
  5.  * @description 排序类的 基类 
  6.  */  
  7.   
  8. public abstract class BaseSorter<E extends Comparable<E>> {  
  9.       
  10.     public abstract void sort(E[] array,int from,int len);  
  11.       
  12.     public final void sort(E[] array){  
  13.         sort(array,0,array.length);  
  14.     }  
  15.       
  16.     protected final void swap(E[] array,int from,int to){  
  17.         E temp = array[from];  
  18.         array[from] = array[to];  
  19.         array[to] = temp;  
  20.     }  
  21. }  
  22.   
  23. /** 
  24.  * @author txin0814 E-mail:txin0814@sina.com 
  25.  * @version 1.0 
  26.  * @date Apr 1, 2011 2:34:47 PM 
  27.  * @description 插入排序 该算法在数据规模小的时候十分高效, 
  28.  *              该算法每次插入第K+1到前K个有序数组中一个合适位置, 
  29.  *              K从0开始到N-1,从而完成排序 
  30.  */  
  31.   
  32. public class InsertSorter<E extends Comparable<E>> extends BaseSorter<E>{  
  33.       
  34.     //当SORT_TYPE为false时按降序排列 为TRUE时按升序排列  
  35.     public static boolean SORT_TYPE = false;   
  36.   
  37.     @Override  
  38.     public void sort(E[] array, int from, int len) {  
  39.         E tmp=null;  
  40.         for(int i=from+1;i<from+len;i++){  
  41.             tmp=array[i];  
  42.             int j=i;  
  43.             for(;j>from;j--){  
  44.                 if(SORT_TYPE){  
  45.                     if(tmp.compareTo(array[j-1])<0){  
  46.                         array[j]=array[j-1];  
  47.                     }else break;  
  48.                 }else{  
  49.                     if(tmp.compareTo(array[j-1])>0){  
  50.                         array[j]=array[j-1];  
  51.                     }else break;  
  52.                 }  
  53.             }  
  54.             array[j]=tmp;  
  55.         }  
  56.           
  57.         /*for (E e : array) { 
  58.             System.out.println(e); 
  59.         }*/  
  60.   
  61.     }  
  62.   
  63.     public static void main(String[] args) {  
  64.         Integer[] elem = {32431354419};  
  65.         InsertSorter<Integer> insertsort = new InsertSorter<Integer>();  
  66.         //InsertSorter.SORT_TYPE = true;  
  67.         insertsort.sort(elem);  
  68.         for (Integer integer : elem) {  
  69.             System.out.println(integer);  
  70.         }  
  71.     }  
  72. }  
  73.   
  74.   
  75. /** 
  76.  * @author txin0814 E-mail:txin0814@sina.com 
  77.  * @version 1.0 
  78.  * @date Apr 1, 2011 2:53:29 PM 
  79.  * @description 冒泡排序 算法思想是每次从数组末端开始比较相邻两元素,把第i小的冒泡到数组的第i个位置。 
  80.  *              i从0一直到N-1从而完成排序。(当然也可以从数组开始端开始比较相邻两元素, 
  81.  *              把第i大的冒泡到数组的第N-i个位置。i从0一直到N-1从而完成排序。) 
  82.  */  
  83.   
  84. public class BubbleSorter<E extends Comparable<E>> extends BaseSorter<E> {  
  85.   
  86.     //当SORT_TYPE为false时按降序排列 为TRUE时按升序排列  
  87.     public static boolean SORT_TYPE = false;   
  88.   
  89.     public final void bubble_down(E[] array, int from, int len) {  
  90.         for (int i = from; i < from + len; i++) {  
  91.             for (int j = from + len - 1; j > i; j--) {  
  92.                 if (array[j].compareTo(array[j - 1]) > 0) {  
  93.                     swap(array, j - 1, j);  
  94.                 }  
  95.             }  
  96.         }  
  97.     }  
  98.   
  99.     public final void bubble_up(E[] array, int from, int len) {  
  100.         for (int i = from + len - 1; i >= from; i--) {  
  101.             for (int j = from; j < i; j++) {  
  102.                 if (array[j].compareTo(array[j + 1]) > 0) {  
  103.                     swap(array, j + 1, j );  
  104.                 }  
  105.             }  
  106.         }  
  107.     }  
  108.   
  109.     @Override  
  110.     public void sort(E[] array, int from, int len) {  
  111.         if (SORT_TYPE) {  
  112.             bubble_up(array, from, len);  
  113.         } else {  
  114.             bubble_down(array, from, len);  
  115.         }  
  116.     }  
  117.       
  118.     public static void main(String[] args) {  
  119.         Integer[] elem = {32431354419};  
  120.         BubbleSorter<Integer> bsort = new BubbleSorter<Integer>();  
  121.         //BubbleSorter.DWON = true;  
  122.         //bsort.sort(elem);  
  123.         //BubbleSorter.SORT_TYPE = true;  
  124.         bsort.sort(elem, 0, elem.length);  
  125.         for (Integer integer : elem) {  
  126.             System.out.println(integer);  
  127.         }  
  128.     }  
  129.   
  130. }  
  131.   
  132. /** 
  133.  * @author txin0814 E-mail:txin0814@sina.com 
  134.  * @version 1.0 
  135.  * @date Apr 1, 2011 3:17:42 PM 
  136.  * @description 选择排序 选择排序相对于冒泡来说,它不是每次发现逆序都交换, 
  137.  *              而是在找到全局第i小的时候记下该元素位置,最后跟第i个元素交换,从而保证数组最终的有序。 
  138.  *              相对与插入排序来说,选择排序每次选出的都是全局第i小的,不会调整前i个元素了 
  139.  */  
  140.   
  141. public class SelectSorter<E extends Comparable<E>> extends BaseSorter<E> {  
  142.   
  143.     @Override  
  144.     public void sort(E[] array, int from, int len) {  
  145.         for (int i = 0; i < len; i++){  
  146.             int smallest = i;  
  147.             int j = i + from;  
  148.             for(;j < from + len ; j++){  
  149.                 if(array[j].compareTo(array[smallest]) < 0){  
  150.                     smallest = j;  
  151.                 }  
  152.             }  
  153.             swap(array,i,smallest);  
  154.         }  
  155.     }  
  156.       
  157.     public static void main(String[] args){  
  158.         Integer[] elem = {12,20,2,5,1,25,55,15};  
  159.         SelectSorter<Integer> sort = new SelectSorter<Integer>();  
  160.         sort.sort(elem);  
  161.         for (Integer integer : elem) {  
  162.             System.out.println(integer);  
  163.         }  
  164.     }  
  165.   
  166. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多