zoukankan      html  css  js  c++  java
  • 三种基本排序的实现及其效率对比:冒泡排序、选择排序和插入排序

      1 public class ThreeTypesOfBaseSort {
      2   //    ========================== 三种基本排序的效率对比 ============================
      3   public static void main(String[] args) {
      4     ThreeTypesOfBaseSort sort = new ThreeTypesOfBaseSort();
      5 
      6     // 测试百万级别的数组排序,看三种基本排序的的效率差别:
      7     int number = 500000;
      8     int[] array = new int[number];
      9     int[] array1 = new int[number];
     10     int[] array2 = new int[number];
     11     for(int i = 0; i < array.length; i++) {
     12       int temp = new Random().nextInt(number);
     13       array[i] = temp;
     14       array1[i] = temp;
     15       array2[i] = temp;
     16     }
     17     System.out.println("数组准备完毕~");
     18 
     19     long start1 = System.currentTimeMillis();
     20     sort.bubbleSort(array);
     21     long end1 = System.currentTimeMillis();
     22     System.out.println("bubbleSort 用时:" + (end1 - start1));//测试结果:当元素个数为5万时:4157。50万:430255。100万:1644079
     23 
     24     long start2 = System.currentTimeMillis();
     25     sort.selectionSort(array1);
     26     long end2 = System.currentTimeMillis();
     27     System.out.println("selectSort 用时:" + (end2 - start2));//5万:727。50万:74253。100万:281276 ==》选择排序比冒泡快了5.7倍。
     28 
     29     long start3 = System.currentTimeMillis();
     30     sort.insertionSort(array2);
     31     long end3 = System.currentTimeMillis();
     32     System.out.println("insertionSort 用时:" + (end3 - start3));//5万:827。50万:84644。 ==》插入排序比选择排序稍慢一点。
     33   }
     34 
     35   //    ========================== BubbleSort ============================
     36   /**
     37   * 冒泡排序:相邻的两个数进行比较,如果是从小到大排序,则将较大的那个数放后,每次都要交换。
     38 
     39   * 冒泡排序的优化思路:
     40   *       ① 判空;
     41   *       ② 元素的个数很特殊时:个数为0、1时;
     42   *       ③ 数组已经是有序的,或者是数组里所有元素都相同:
     43   */
     44   public void bubbleSort(int[] target){
     45     if(target == null){
     46       return;
     47     }
     48     boolean tag = true;//如果数组是有序的,则不需要再进行交换。
     49     for(int i = 0; i < target.length -1 && tag; i++){//外层循环:控制比较的组数:(元素个数-1)次。
     50       tag = false;
     51       for(int j = 0; j < target.length - i - 1; j++){//内层循环:控制比较的元素:每组比较都从第一个元素开始比较,把每次比较时的max移到后面去,每组的比较次数=target.length-1-i。
     52         if(target[j] > target[j + 1]){
     53           int temp = target[j];
     54           target[j] = target[j + 1];
     55           target[j + 1] = temp;
     56           tag = true;
     57         }
     58       }
     59     }
     60   }
     61 
     62   //    ======================= SelectionSort ============================
     63   /**
     64   * 选择排序:
     65   *   第一趟从n个元素的数据序列中选出关键字最小/大的元素并放在最前位置,
     66   *   下一趟从n-1个元素中选出最小/大的元素并放在未排好序元素的最前位置。以此类推,经过n-1趟完成排序。
     67   */
     68   public void selectionSort(int[] target){
     69     if(target == null){
     70       return;
     71     }
     72     for(int i = 0; i < target.length - 1; i++){
     73       int tempIndex = i;
     74       for(int j = i + 1; j < target.length; j++){
     75         if(target[tempIndex] > target[j]){
     76           tempIndex = j;
     77         }
     78       }
     79     int temp = target[tempIndex];
     80     target[tempIndex] = target[i];
     81     target[i] = temp;
     82     }
     83   }
     84 
     85   //    ===================== insertSort ====================================
     86   /**
     87   * 插入排序:将一个数据插入到已经排好序的有序数据中(一般默认第一个元素是有序的,比较从第二个元素开始),
     88   * 从而得到一个新的、个数加一的有序数据,算法适用于少量数据的排序。
     89   */
     90   public void insertionSort(int[] target){
     91     if(target == null){
     92       return;
     93     }
     94     //外层循环控制比较的组数;
     95     for(int i = 0; i < target.length - 1; i++){
     96       //内层循环负责找出该组比较中,在已排好序数组之后的第一个无序的元素,并通过比较将这个无序元素插入到有序数组中。
     97       for(int j = i + 1; j > 0; j--){
     98         if(target[j - 1] > target[j]){
     99           int temp = target[j];
    100           target[j] = target[j - 1];
    101           target[j - 1] = temp;
    102         }else{
    103           break;
    104         }
    105       }
    106     }
    107   }
    108 }

    测试结果:

      时间单位:毫秒
      1. 冒泡排序:5万个元素的排序:4157。  50万:430255。  100万:1644079
      2. 选择排序:5万:727。                         50万:74253。     100万:281276
      3. 插入排序:5万:827。                         50万:84644。
      4. 快速排序:5万:11。                           50万:66。            100万:136。

    总结:
      选择排序 > 插入排序 > 冒泡排序;
      选择排序比插入排序快了1.1倍,比冒泡排序快了越5.7倍。

    作者:赖皮梅
    声明:
    1.原创博文,欢迎转载、引用;转载、引用请注明作者并附上原文链接,否则保留追究法律责任的权利。
    2.本博文中引用他人的博文内容时均已注明出处,如有侵权,请联系作者删除。
    3.博文内容如有错误、不妥之处,欢迎留言指正,还请不吝赐教 =^_^=
  • 相关阅读:
    【神经网络与深度学习】学习笔记:AlexNet&Imagenet学习笔记
    【神经网络与深度学习】学习笔记:AlexNet&Imagenet学习笔记
    【神经网络与深度学习】如何将别人训练好的model用到自己的数据上
    【神经网络与深度学习】如何将别人训练好的model用到自己的数据上
    【神经网络与深度学习】Caffe使用step by step:使用自己数据对已经训练好的模型进行finetuning
    【神经网络与深度学习】Caffe使用step by step:使用自己数据对已经训练好的模型进行finetuning
    【神经网络与深度学习】用训练好的caffemodel来进行分类
    【神经网络与深度学习】用训练好的caffemodel来进行分类
    【神经网络与深度学习】Caffe部署中的几个train-test-solver-prototxt-deploy等说明
    【神经网络与深度学习】Caffe部署中的几个train-test-solver-prototxt-deploy等说明
  • 原文地址:https://www.cnblogs.com/laipimei/p/11118561.html
Copyright © 2011-2022 走看看