zoukankan      html  css  js  c++  java
  • 几种常见的排序算法集锦

    排序算法有很多,下面只是列举了几种常见的.

    首先定义一个用于交换数组中2个值的方法

    1 //用于交换的方法
    2 public static void swap(Integer[] arr,int a,int b){
    3      int temp;
    4      temp = arr[a];
    5      arr[a] = arr[b];
    6      arr[b] = temp;
    7 }

    冒泡排序   O(n^2)

     1     public static void sort(Integer[] a){
     2         for (int i=0;i<a.length;i++) {
     3 
     4             Boolean flag = true;
     5 
     6             for (int j=a.length-2;j>=i;j--){
     7 
     8                 if (a[j]>a[j+1]){
     9                     swap(a,j,j+1);
    10                     flag = false;
    11                 }
    12             }
    13 
    14             if (flag){
    15                 //数组已经有序
    16                 return ;
    17             }
    18         }
    19     }

    简单选择排序  O(n^2)

     1     public static void sort(Integer[] arr){
     2 
     3         for (int i=0;i<arr.length;i++){
     4             int min = i;
     5             for (int j=i+1;j<arr.length;j++){
     6                 if (arr[j]<arr[min]){
     7                     min = j;
     8                 }
     9             }
    10             if (min != i){
    11                 swap(arr,i,min);
    12             }
    13         }
    14 
    15     }

    直接插入排序  O(n^2)

     1     public static void sort(Integer[] arr){
     2         for(int i=1;i<arr.length;i++){
     3             if (arr[i]<arr[i-1]){
     4                int temp = arr[i];
     5                int j;
     6                 for (j=i-1;j>=0 && arr[j]>temp;j--){
     7                     arr[j+1] = arr[j];
     8                 }
     9                 arr[j+1] = temp;
    10             }
    11         }
    12     }

    希尔排序   O(nlog(n))~ O(n^2)   不稳定

     1     public static void sort(Integer arr[]){
     2         //增量,每次分组的间隔
     3         int increment = arr.length;
     4         //临时变量
     5         int temp;
     6         int j;
     7         while (increment>1){
     8             increment = increment / 3 +1;
     9             for (int i= 0;i<arr.length;i=i+increment){
    10                 temp = arr[i];
    11                 for (j=i-increment;j>=0&&arr[j]>temp;j=j-increment){
    12                     arr[j+increment] = arr[j];
    13                 }
    14                 arr[j+increment] = temp;
    15             }
    16         }
    17     }

    快速排序    O(nlog(n))~ O(n^2)   不稳定

     1     public static void sort(Integer[] arr,int left,int right){
     2         int i,j,temp,t;
     3         if(left>right){
     4             return ;
     5         }
     6         i=left;
     7         j=right;
     8         //temp就是基准位
     9         temp = arr[left];
    10         while (i<j) {
    11             //先看右边,依次往左递减
    12             while (temp<=arr[j]&&i<j) {
    13                 j--;
    14             }
    15             //再看左边,依次往右递增
    16             while (temp>=arr[i]&&i<j) {
    17                 i++;
    18             }
    19             //如果满足条件则交换
    20             if (i<j) {
    21                 t = arr[j];
    22                 arr[j] = arr[i];
    23                 arr[i] = t;
    24             }
    25         }
    26         //最后将基准为与i和j相等位置的数字交换
    27         arr[left] = arr[i];
    28         arr[i] = temp;
    29         //递归调用左半数组
    30         sort(arr, left, j-1);
    31         //递归调用右半数组
    32         sort(arr, j+1, right);
    33     }

    用下面这段代码做测试,每个算法的大概时间(因个人电脑差异,数据可能有差异)

     1         Integer arr[] = new Integer[10000];
     2         for (int i =0;i<arr.length;i++){
     3             arr[i]= new Random().nextInt();
     4         }
     5         Long start = System.currentTimeMillis();
     6         sort(arr,0,arr.length-1);
     7         Long end = System.currentTimeMillis();
     8         System.out.println(start);
     9         System.out.println(end);
    10         System.out.println("运行时间为----"+(end-start));
    11         for (Integer i:arr) {
    12             System.out.print(i+" ");
    13         }

    冒泡算法:430~550ms

    简单选择排序:105~180ms

    直接插入排序:55~80ms

    希尔排序:40~60ms

    快速排序:5~10ms

  • 相关阅读:
    使用CustomValidate自定义验证控件
    C#中金额的大小写转换
    Andriod出错之Unable to build: the file dx.jar was not loaded from the SDK folder!
    VC 编写的打字练习
    机房工作笔记Ping只有单向通
    web服务协同学习笔记(1)
    Dll 学习3 将MDI子窗口封装在DLL中
    机房工作学习文件共享
    Andriod出错之Failed to find an AVD compatible with target 'Android 2.2'
    Andriod出错之wrapper was not properly loaded first
  • 原文地址:https://www.cnblogs.com/shi-zhe/p/11910082.html
Copyright © 2011-2022 走看看