612.1.002 ALGS4 | Analysis of Algorithms

我们生活在大数的时代
培养数量级的敏感!
Tip:见招拆招

  • 作为工程师,你先要能实现出来。
  • 充实基础,没有什么不好意思
  • 哪怕不完美。但是有时候完成比完美更重要。
  • 之后再去想优化

P.S.作者Robert Sedgewick的导师是Knuth(高德纳!)

Conclusion First

1.Running Time

1143923-20190129143751869-312860822.png

  • Operation table

1143923-20190129143756162-541288907.png

2.Memory

1 SOP - Analysis

1143923-20190129143921386-1744016212.png

2 Observations

  • Measuring the running time - automatic

public class Stopwatch(part of stdlib.jar/algs4.course)


    public static void main(String[] args) {
        In in = new In(args[0]);
        int[] a = in.readAllInts();

        Stopwatch timer = new Stopwatch();
        int count = count(a);
        StdOut.println("elapsed time = " + timer.elapsedTime());//time since creation (in seconds)
        StdOut.println(count);
    }


3 Mathematical Model - Knuth(高德纳!)

Simplification 1: cost model

1143923-20190129143934219-2119635967.png

Simplification 2: tilde notation

approximate

工程近似

1143923-20190129144045253-345964195.png

1143923-20190129144050571-861134697.png

  • Bottom line. Use cost model and tilde notation to simplify counts.

4 Order of growth

1143923-20190129144122337-455903612.png

  • Operation table

1143923-20190129144125590-1741177838.png

5 Binary Search - code 二分搜索

不看代码自己写
1143923-20190129144312945-1534820471.png

public static int binarySearch(int[]a,int key)
        {
        int lo=0;
        int hi=a.length-1;
        while(lo<=hi){
            int mid=lo+(hi-lo)/2;
            if(key>a[mid])lo=mid+1;
            else if(key<a[mid])hi=mid-1;
            else return mid;
        }
        return-1;
        }

6 Memory

1143923-20190129144226099-1885483066.png

Typical memory usage of Java

Object overhead - 对象开销
1143923-20190129144232078-24028413.png

QuickUnion

1143923-20190129144239263-373938154.png

转载于:https://www.cnblogs.com/Neo007/p/10333985.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值