编程语言
首页 > 编程语言> > 612.1.004 ALGS4 | Elementary Sorts - 基础排序算法

612.1.004 ALGS4 | Elementary Sorts - 基础排序算法

作者:互联网

sublime编辑器写代码,命令行编译
减少对ide的依赖//可以提示缺少什么依赖import
所有示例代码动手敲一遍
Graham's Scan是经典的计算几何算法
shffule 与 map-reduce 有关—— 云计算
知道这些算法在身边切实的应用,对学习动力很有帮助
下一章开始,使用 git进行源代码管理!
先用来做自己的项目管理

Inspiration

计算机思维——不要重复造轮子
零件的通用性——拆分拆分再拆分,专业与分工细致

1.Callback = reference to executable code

核心思想是将函数作为实参传递给其他函数
Interface-Java 使我们能够以类型安全的方式使用为任何类型的数据开发的排序算法

Roadmap

对象需要实现Compareble Interface()
这些数据类型具有重新实现compareTo()方法的实例方法

排序实现里与数据类型无关
具体的比较由Comparable 接口处理

What I can use —— code

点击链接可看动画演示

1 Two useful sorting abstrations

Less


private static boolean less(Comparable v, Comparable w)
{   return v.compareTo(w)< 0 ;}

Exchange

//exchange a[i] & a[j]
private static void exch(Comparable[] a,int i ,int j)
{
    Comparable swap = a[i];
    a[i] = a[j];
    a[j] = swap;
}

2.Selection Sort

select the min



1 rules of the game

sorting problem

real numbers

String

Callbacks

key point :passing function as arguments to other function
核心思想是将函数作为实参传递给其他函数
Interface-Java 使我们能够以类型安全的方式使用为任何类型的数据开发的排序算法

Roadmap | 技术路线图 - Callback

public inter face Comparable<Item>
{
    public int compareTo(Item that); // generics method
}

对象需要实现Compareble Interface()
这些数据类型具有重新实现compareTo()方法的实例方法
56f4240e221fb6c07d008741f0838bb3.png
排序实现里与数据类型无关
具体的比较由Comparable 接口处理

Two useful sorting abstrations

Less


private static boolean less(Comparable v, Comparable w)
{   return v.compareTo(w)< 0 ;}

Exchange

private static void exch(Comparable[] a,int i ,int j)
{
    Comparable swap = a[i];
    a[i] = a[j];
    a[j] = swap;
}

2 Selection Sort | 选择排序

即使数据集已经部分有序,仍需重新遍历一遍
N^2

QuestionDemo-Selection Sort

Inner Loop - Selection sort

public class SelectionSort
{
    public static void sort(Comparable[] a)
    {
        int N = a.length;
        for (i = 0; i < N;i++)
        {
        int min = i;
        for(int j = i+1; j < N;j++)
            if(less(a[j], a[min]))
                min = j;
         exch(a, i, min);
        }
    }

    private static boolean less(Comparable v,Comparable w)
    {
        return v.compareTo(w) < 0;
    }

    private static void exch (Comparable[] a, int i, int j)
    {
        Comparable swap = a[i];
        a[i] = a[j];
        a[j] = swap;
    }

}

3 Insertion Sort | 插入排序

Move one position at one time
如果左边(比自己)更大,则继续向左互换

QuestionDemo-Insertion Sort

Step1

Step2

Inner Loop - Insertion sort


import edu.princeton.cs.algs4.Insertion;
import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom;


public class InsertionSort
{
    public static void sort(Comparable[] a)
    {
        int N = a.length;

        for (int i = 0; i < N; i++)
        {
            for(int j = i; j > 0; j--)
                if(less(a[j],a[j-1]))
                    exch(a,j,j-1);
                else break;
        }
    }

    private static boolean less(Comparable v,Comparable w)
    {
        return v.compareTo(w) < 0;
    }

    private static void exch (Comparable[] a, int i, int j)
    {
        Comparable swap = a[i];
        a[i] = a[j];
        a[j] = swap;
    }

}



public class SortDemo {
    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);
        Double[] a = new Double[N];
        for (int i = 0; i < N; i++) {
            a[i] = StdRandom.uniform();
        }
        Insertion.sort(a);
        for (int i = 0; i < N; i++) {
            StdOut.println(a[i]);
        }

    }
}

4 ShellSort | 希尔排序

Shell - the name of the creator 1959
Move more than one position at one time // compare to the Insertion Sort
一次移动多个位置
希尔排序,也称递减增量排序算法,是插入排序的一种更高效的改进版本。希尔排序是非稳定排序算法。

希尔排序是基于插入排序的以下两点性质而提出改进方法的: 插入排序在对几乎已经排好序的数据操作时,效率高,即可以达到线性排序的效率 但插入排序一般来说是低效的,因为插入排序每次只能将数据移动一位

Photo:

7,3,1 Sort

package edu.princeton.cs.algs4;


public class ShellSort{

    public static void sort(Comparable[] a){
        int n = a.length;
    //// x = 3x+1 increment sequence:  1, 4, 13, 40, 121, 364, 1093, ... 
        int h = 1;
        while (h < n/3) h = 3*h + 1;

        while (h >= 1) {
            //h-sort the array
            // Insertion sort
            for (int i = h; i < n;i++){
                for (j = i; j >= h && less(a[j],a[j-h]); j-= h){
                    exch(a, j, j-h);
                }
            }
            assert isHsorted(a, h); 
            h /=3;//move to next increment,eg. 13,4,1
        }
        assert isHsorted(a, h); 
    }

      /***************************************************************************
    *  Helper sorting functions.
    ***************************************************************************/
    
    // is v < w ?
    private static boolean less(Comparable v, Comparable w) {
        return v.compareTo(w) < 0;
    }
        
    // exchange a[i] and a[j]
    private static void exch(Object[] a, int i, int j) {
        Object swap = a[i];
        a[i] = a[j];
        a[j] = swap;
    }


   /***************************************************************************
    *  Check if array is sorted - useful for debugging.
    ***************************************************************************/
    private static boolean isSorted(Comparable[] a) {
        for (int i = 1; i < a.length; i++)
            if (less(a[i], a[i-1])) return false;
        return true;
    }

    // is the array h-sorted?
    private static boolean isHsorted(Comparable[] a, int h) {
        for (int i = h; i < a.length; i++)
            if (less(a[i], a[i-h])) return false;
        return true;
    }

    // print array to standard output
    private static void show(Comparable[] a) {
        for (int i = 0; i < a.length; i++) {
            StdOut.println(a[i]);
        }
    }

    /**
     * Reads in a sequence of strings from standard input; Shellsorts them; 
     * and prints them to standard output in ascending order. 
     *
     * @param args the command-line arguments
     */
    public static void main(String[] args) {
        String[] a = StdIn.readAllStrings();
        ShellSort.sort(a);
        show(a);
    }


}

5 Shuffle - StdRandom.java

Knuth Shuffle
a uniformly random permutation of the input array in linear time.
洗牌

Goal
from

to

Code


public class StdRandom
{
    public static void shuffle(Object[] a)
    {
        int N = a.length;
        for (int i = 0; i < N; i++);
        {
            int r = StdRandom.uniform(i+1); //StdRandom.uniform 均匀分布  [0,i+1)
                                                        //r = between 0 and i
            exch(a,i,r);
        }
    }
    

}

6 Convex Hull

Graham's Scan
凸包
The convex hull of a set of N points is the smallest perimeter fence enclosing the points

Goal

counterclockwise:逆时针
clockwise:顺时针

mechanical algorithm

Computer application
1.motion planning

2.farthest pair

Fact

P.S. Polar angle

Graham's Scan 葛立恒扫描

没错 ,就是提出葛立恒数的那位葛立恒
accroding the two facts

Demo

challenge

Implementing ccw(counterclockwise)

what is ccw

** math**

according to the Slope (斜率)

** implement**

标签:Comparable,int,void,private,static,ALGS4,004,Elementary,public
来源: https://www.cnblogs.com/Neo007/p/10509568.html