zoukankan      html  css  js  c++  java
  • 算法第四版学习笔记之初级排序算法

    软件:DrJava

    参考书:算法(第四版)

    章节:2.1(以下截图是算法配套视频所讲内容截图)

    1:rules of the game(游戏规则)

    2:selection sort(选择排序)

    3:insertion sort(插入排序)

    4:shellsort(希尔排序)

    5:shuffling(洗牌)

    6:convex hull(凸包算法)

    1:rules of the game(游戏规则)

    排序算法的目标就是降所有元素的主键按照某种方式排列。在Java中,元素通常都是对象,对主键的抽象描述则是通过一种内置的机制(Comparable接口)来完成的。

    2:selection sort(选择排序)

    选择排序的两个鲜明的特点:1:运行时间和输入无关。2:数据移动是最少的。

      1 /*************************************************************************
      2  *  Compilation:  javac Selection.java
      3  *  Execution:    java  Selection < input.txt
      4  *  Dependencies: StdOut.java StdIn.java
      5  *  Data files:   http://algs4.cs.princeton.edu/21sort/tiny.txt
      6  *                http://algs4.cs.princeton.edu/21sort/words3.txt
      7  *   
      8  *  Sorts a sequence of strings from standard input using selection sort.
      9  *   
     10  *  % more tiny.txt
     11  *  S O R T E X A M P L E
     12  *
     13  *  % java Selection < tiny.txt
     14  *  S O R T E X A M P L E A               [ one string per line ]
     15  *    
     16  *  % more words3.txt
     17  *  bed bug dad yes zoo ... all bad yet
     18  *  
     19  *  % java Selection < words3.txt
     20  *  all bad bed bug dad ... yes yet zoo    [ one string per line ]
     21  *
     22  *************************************************************************/
     23 
     24 import edu.princeton.cs.algs4.*;
     25 import java.util.Comparator;
     26 
     27 public class Selection {
     28 
     29     // selection sort
     30     public static void sort(Comparable[] a) {
     31         int N = a.length;
     32         for (int i = 0; i < N; i++) {
     33             int min = i;
     34             for (int j = i+1; j < N; j++) {
     35                 if (less(a[j], a[min])) min = j;
     36             }
     37             exch(a, i, min);
     38             assert isSorted(a, 0, i);
     39         }
     40         assert isSorted(a);
     41     }
     42 
     43     // use a custom order and Comparator interface - see Section 3.5
     44     public static void sort(Object[] a, Comparator c) {
     45         int N = a.length;
     46         for (int i = 0; i < N; i++) {
     47             int min = i;
     48             for (int j = i+1; j < N; j++) {
     49                 if (less(c, a[j], a[min])) min = j;
     50             }
     51             exch(a, i, min);
     52             assert isSorted(a, c, 0, i);
     53         }
     54         assert isSorted(a, c);
     55     }
     56 
     57 
     58    /***********************************************************************
     59     *  Helper sorting functions
     60     ***********************************************************************/
     61     
     62     // is v < w ?
     63     private static boolean less(Comparable v, Comparable w) {
     64         return (v.compareTo(w) < 0);
     65     }
     66 
     67     // is v < w ?
     68     private static boolean less(Comparator c, Object v, Object w) {
     69         return (c.compare(v, w) < 0);
     70     }
     71         
     72         
     73     // exchange a[i] and a[j]
     74     private static void exch(Object[] a, int i, int j) {
     75         Object swap = a[i];
     76         a[i] = a[j];
     77         a[j] = swap;
     78     }
     79 
     80 
     81    /***********************************************************************
     82     *  Check if array is sorted - useful for debugging
     83     ***********************************************************************/
     84 
     85     // is the array a[] sorted?
     86     private static boolean isSorted(Comparable[] a) {
     87         return isSorted(a, 0, a.length - 1);
     88     }
     89         
     90     // is the array sorted from a[lo] to a[hi]
     91     private static boolean isSorted(Comparable[] a, int lo, int hi) {
     92         for (int i = lo + 1; i <= hi; i++)
     93             if (less(a[i], a[i-1])) return false;
     94         return true;
     95     }
     96 
     97     // is the array a[] sorted?
     98     private static boolean isSorted(Object[] a, Comparator c) {
     99         return isSorted(a, c, 0, a.length - 1);
    100     }
    101 
    102     // is the array sorted from a[lo] to a[hi]
    103     private static boolean isSorted(Object[] a, Comparator c, int lo, int hi) {
    104         for (int i = lo + 1; i <= hi; i++)
    105             if (less(c, a[i], a[i-1])) return false;
    106         return true;
    107     }
    108 
    109 
    110 
    111     // print array to standard output
    112     private static void show(Comparable[] a) {
    113         for (int i = 0; i < a.length; i++) {
    114             StdOut.println(a[i]);
    115         }
    116     }
    117 
    118     // Read strings from standard input, sort them, and print.
    119     public static void main(String[] args) {
    120         String[] a = StdIn.readStrings();
    121         Selection.sort(a);
    122         show(a);
    123     }
    124 }

    3:insertion sort(插入排序)

    插入排序:只需比较前后两个值,之后后边值比前边小,就交换位置。

      1 /*************************************************************************
      2  *  Compilation:  javac Insertion.java
      3  *  Execution:    java Insertion < input.txt
      4  *  Dependencies: StdOut.java StdIn.java
      5  *  Data files:   http://algs4.cs.princeton.edu/21sort/tiny.txt
      6  *                http://algs4.cs.princeton.edu/21sort/words3.txt
      7  *  
      8  *  Sorts a sequence of strings from standard input using insertion sort.
      9  *
     10  *  % more tiny.txt
     11  *  S O R T E X A M P L E
     12  *
     13  *  % java Insertion < tiny.txt
     14  *  S O R T E X A M P L E A               [ one string per line ]
     15  *
     16  *  % more words3.txt
     17  *  bed bug dad yes zoo ... all bad yet
     18  *
     19  *  % java Insertion < words3.txt
     20  *  all bad bed bug dad ... yes yet zoo   [ one string per line ]
     21  *
     22  *************************************************************************/
     23 import edu.princeton.cs.algs4.*;
     24 import java.util.Comparator;
     25 
     26 public class Insertion {
     27 
     28     // use natural order and Comparable interface
     29     public static void sort(Comparable[] a) {
     30         //将a[]按升序排列
     31         int N = a.length;
     32         for (int i = 0; i < N; i++) {
     33             //将a[i]插入到a[i-1]、a[i-2]、a[i-3]...之中
     34             for (int j = i; j > 0 && less(a[j], a[j-1]); j--) {
     35                 exch(a, j, j-1);
     36             }
     37             assert isSorted(a, 0, i);
     38         }
     39         assert isSorted(a);
     40     }
     41 
     42     // use a custom order and Comparator interface - see Section 3.5
     43     public static void sort(Object[] a, Comparator c) {
     44         int N = a.length;
     45         for (int i = 0; i < N; i++) {
     46             for (int j = i; j > 0 && less(c, a[j], a[j-1]); j--) {
     47                 exch(a, j, j-1);
     48             }
     49             assert isSorted(a, c, 0, i);
     50         }
     51         assert isSorted(a, c);
     52     }
     53 
     54     // return a permutation that gives the elements in a[] in ascending order
     55     // do not change the original array a[]
     56     public static int[] indexSort(Comparable[] a) {
     57         int N = a.length;
     58         int[] index = new int[N];
     59         for (int i = 0; i < N; i++)
     60             index[i] = i;
     61 
     62         for (int i = 0; i < N; i++)
     63             for (int j = i; j > 0 && less(a[index[j]], a[index[j-1]]); j--)
     64                 exch(index, j, j-1);
     65 
     66         return index;
     67     }
     68 
     69    /***********************************************************************
     70     *  Helper sorting functions
     71     ***********************************************************************/
     72     
     73     // is v < w ?
     74     private static boolean less(Comparable v, Comparable w) {
     75         return (v.compareTo(w) < 0);
     76     }
     77 
     78     // is v < w ?
     79     private static boolean less(Comparator c, Object v, Object w) {
     80         return (c.compare(v, w) < 0);
     81     }
     82         
     83     // exchange a[i] and a[j]
     84     private static void exch(Object[] a, int i, int j) {
     85         Object swap = a[i];
     86         a[i] = a[j];
     87         a[j] = swap;
     88     }
     89 
     90     // exchange a[i] and a[j]  (for indirect sort)
     91     private static void exch(int[] a, int i, int j) {
     92         int swap = a[i];
     93         a[i] = a[j];
     94         a[j] = swap;
     95     }
     96 
     97    /***********************************************************************
     98     *  Check if array is sorted - useful for debugging
     99     ***********************************************************************/
    100     private static boolean isSorted(Comparable[] a) {
    101         return isSorted(a, 0, a.length - 1);
    102     }
    103 
    104     // is the array sorted from a[lo] to a[hi]
    105     private static boolean isSorted(Comparable[] a, int lo, int hi) {
    106         for (int i = lo + 1; i <= hi; i++)
    107             if (less(a[i], a[i-1])) return false;
    108         return true;
    109     }
    110 
    111     private static boolean isSorted(Object[] a, Comparator c) {
    112         return isSorted(a, c, 0, a.length - 1);
    113     }
    114 
    115     // is the array sorted from a[lo] to a[hi]
    116     private static boolean isSorted(Object[] a, Comparator c, int lo, int hi) {
    117         for (int i = lo + 1; i <= hi; i++)
    118             if (less(c, a[i], a[i-1])) return false;
    119         return true;
    120     }
    121 
    122    // print array to standard output
    123     private static void show(Comparable[] a) {
    124         for (int i = 0; i < a.length; i++) {
    125             StdOut.println(a[i]);
    126         }
    127     }
    128 
    129     // Read strings from standard input, sort them, and print.
    130     public static void main(String[] args) {
    131         String[] a = StdIn.readStrings();
    132         Insertion.sort(a);
    133         show(a);
    134     }
    135 }

    比较两种排序算法:对于随机排序的无重复主键的数组,插入排序和选择排序的运行时间是平方级别的,两者之比应该是一个较小的常数。

    4:shellsort(希尔排序)

    希尔排序的思想是使数组中任意间隔为h的元素都是有序的。

      1 /*************************************************************************
      2  *  Compilation:  javac Shell.java
      3  *  Execution:    java Shell < input.txt
      4  *  Dependencies: StdOut.java StdIn.java
      5  *  Data files:   http://algs4.cs.princeton.edu/21sort/tiny.txt
      6  *                http://algs4.cs.princeton.edu/21sort/words3.txt
      7  *   
      8  *  Sorts a sequence of strings from standard input using shellsort.
      9  *
     10  *  Uses increment sequence proposed by Sedgewick and Incerpi.
     11  *  The nth element of the sequence is the smallest integer >= 2.5^n
     12  *  that is relatively prime to all previous terms in the sequence.
     13  *  For example, incs[4] is 41 because 2.5^4 = 39.0625 and 41 is
     14  *  the next integer that is relatively prime to 3, 7, and 16.
     15  *   
     16  *  % more tiny.txt
     17  *  S O R T E X A M P L E
     18  *
     19  *  % java Shell < tiny.txt
     20  *  S O R T E X A M P L E A               [ one string per line ]
     21  *    
     22  *  % more words3.txt
     23  *  bed bug dad yes zoo ... all bad yet
     24  *  
     25  *  % java Shell < words3.txt
     26  *  all bad bed bug dad ... yes yet zoo    [ one string per line ]
     27  *
     28  *
     29  *************************************************************************/
     30 import edu.princeton.cs.algs4.*;
     31 public class Shell {
     32 
     33     // sort the array a[] in ascending order using Shellsort
     34     public static void sort(Comparable[] a) {
     35         //将a[]按升序排列
     36         int N = a.length;
     37 
     38         // 3x+1 increment sequence:  1, 4, 13, 40, 121, 364, 1093, ... 
     39         int h = 1;
     40         while (h < N/3) h = 3*h + 1; 
     41 
     42         while (h >= 1) {
     43             // h-sort the array 将数组变为h有序
     44             for (int i = h; i < N; i++) {
     45                 //将a[i]插入到a[i-h]、a[i-2*h]。。。之中               
     46                 for (int j = i; j >= h && less(a[j], a[j-h]); j -= h) {
     47                     exch(a, j, j-h);
     48                 }
     49             }
     50             assert isHsorted(a, h); 
     51             h /= 3;
     52         }
     53         //查看是否已经排序成功,如果没成功,发出异常报警
     54         assert isSorted(a);
     55     }
     56 
     57 
     58 
     59    /***********************************************************************
     60     *  Helper sorting functions
     61     ***********************************************************************/
     62     
     63     // is v < w ?
     64     private static boolean less(Comparable v, Comparable w) {
     65         return (v.compareTo(w) < 0);
     66     }
     67         
     68     // exchange a[i] and a[j]
     69     private static void exch(Object[] a, int i, int j) {
     70         Object swap = a[i];
     71         a[i] = a[j];
     72         a[j] = swap;
     73     }
     74 
     75 
     76    /***********************************************************************
     77     *  Check if array is sorted - useful for debugging
     78     ***********************************************************************/
     79     private static boolean isSorted(Comparable[] a) {
     80         for (int i = 1; i < a.length; i++)
     81             if (less(a[i], a[i-1])) return false;
     82         return true;
     83     }
     84 
     85     // is the array h-sorted?
     86     private static boolean isHsorted(Comparable[] a, int h) {
     87         for (int i = h; i < a.length; i++)
     88             if (less(a[i], a[i-h])) return false;
     89         return true;
     90     }
     91 
     92     // print array to standard output
     93     private static void show(Comparable[] a) {
     94         for (int i = 0; i < a.length; i++) {
     95             StdOut.println(a[i]);
     96         }
     97     }
     98 
     99     // Read strings from standard input, sort them, and print.
    100     public static void main(String[] args) {
    101         String[] a = StdIn.readStrings();
    102         Shell.sort(a);
    103         show(a);
    104     }
    105 
    106 }

    希尔排序比插入排序以及选择排序快得多。

    通过提升速度来解决其他方式无法解决的问题是研究算法的设计和性能的主要原因之一。

  • 相关阅读:
    PHP Framework
    PHP Framework
    PHP Framework
    PHP Framework
    Coursera:一流大学免费在线课程平台
    在线编译器Coding Ground
    朱子家训
    [转]3天搞定的小型B/S内部管理类软件定制开发项目【软件开发实战10步骤详解】
    [转]Android 如何监听返回键,弹出一个退出对话框
    [转]Android 完美退出 App (Exit)
  • 原文地址:https://www.cnblogs.com/mrchige/p/5999962.html
Copyright © 2011-2022 走看看