Introduction to algorithm in C++

这是我的第一篇博文,学习C++半学期以来,时常会忘记很多基础语法等,经过思考,我决定以后把自己学到的东西都记录下来。一来,帮助记忆;二来,能检查自己是否真的懂了;三来,和广大网友交流分享,这样自己理解有误的地方还能得到改正,实在是利处多多。


算法是程序设计中重要的一环,曾看到有人说:程序=算法+数据结构。可见算法和数据结构的重要性。学习C++半学期,听到最多关于算法的就是算法的复杂度,经常会听到

O(n), O(1), O(n²), O(logn), 指数增长......

初听起来似乎很复杂,今天看书,由于书上并没有多讲,所以目前看来还不是很复杂,不过分析具体的程序的算法复杂度还是挺麻烦的,因为有很多常数次基本操作都需要计入,最终仅保留最高次。

算法的复杂度包括时间复杂度空间复杂度。

时间复杂度(time complexity):

时间复杂度是对程序运行时间的估计,由于一般情况下,程序运行时间会随着输入规模的增大而增大,所以对时间复杂度的估计依赖于输入规模n,一般,n越大,所消耗的时间就越长,因此,时间复杂度可看作是一个关于n的函数映射,我们记为T(n).​​​​​​​

T(n)描述一个程序的计算效率的高低,不同的规模的输入适应不同的算法,并不是说T(n)最小就是最合适的。

D。Knuth在The Art of Computer Programming中提出几种记法,对时间复杂度的分析似乎很重要,也就是O(Big-O notation),Ω(Big-omega notation), Θ​​(Big-theta notation)

在时间复杂度的分析中,几种常数次基本操作很重要,最终就是通过对他们的次数求和来估算运行时间的。包括:逻辑判断,位运算,加法,右移等

O(n)是对时间复杂度的一个保守估计,也就是T(n)的上界

   定义:当存在常数C,使得对任意n>2,均有T(n)≤Cf(n), 则记T(n)=O(f(n))

Ω​​(n)是对时间复杂度的一个良好估计,也就是T(n)的下界

   定义:当存在常数C,使得对任意n>2,均有T(n)≥​Cf(n), 则记T(n)=Ω​​(f(n))

Θ(n)是T(n)的一个类似极限的概念

   定义:当存在常数C1,C2,使得对任意n>2,均有C1h(n)≤T(n)≤C2h(n),则记为T(n)=Θ(h(n))

例如下面的代码就是O(logn)的复杂度:
代码是用来计算一个无符号整型数字在二进制表示下有多少位数字 1。 代码来源邓俊辉的《数据结构C++语言描述》
int count_1s(unsigned int n){
    int ones=0;
    while(n>0){
        ones +=(1 & n);
        n >>=1;     
    }
}
这串代码,初始化1,while循环1+log2n次,每次循环执行1次位运算,1次赋值运算,1次移位运算,所以T(n)=O(3log2n+4)=O(logn);这里我们省略了log2中的底数2,笼统的表示为一个  对数复杂度。  在程序运行过程中,如果能将复杂度降到 常数次或者对数,那就是非常不错高效的算法了。

空间复杂度(space complexity):

空间复杂度是对代码的存储空间的一个衡量标准,一些基础操作都会占用存储,例如 转储,中转,索引,映射,缓冲等等(这里我也只是把书上抄上来了,马上再去google)空间复杂度的衡量也是和时间复杂度一样用三种符号记,最常用的也同样是O(n).






Robert Sedgewick has thoroughly rewritten and substantially expanded and updated his popular work to provide current and comprehensive coverage of important algorithms and data structures. Christopher Van Wyk and Sedgewick have developed new C++ implementations that both express the methods in a concise and direct manner, and also provide programmers with the practical means to test them on real applications. Many new algorithms are presented, and the explanations of each algorithm are much more detailed than in previous editions. A new text design and detailed, innovative figures, with accompanying commentary, greatly enhance the presentation. The third edition retains the successful blend of theory and practice that has made Sedgewick's work an invaluable resource for more than 250,000 programmers! This particular book, Parts 1n4, represents the essential first half of Sedgewick's complete work. It provides extensive coverage of fundamental data structures and algorithms for sorting, searching, and related applications. Although the substance of the book applies to programming in any language, the implementations by Van Wyk and Sedgewick also exploit the natural match between C++ classes and ADT implementations. Highlights * Expanded coverage of arrays, linked lists, strings, trees, and other basic data structures * Greater emphasis on abstract data types (ADTs), modular programming, object-oriented programming, and C++ classes than in previous editions * Over 100 algorithms for sorting, selection, priority queue ADT implementations, and symbol table ADT (searching) implementations * New implementations of binomial queues, multiway radix sorting, randomized BSTs, splay trees, skip lists, multiway tries, B trees, extendible hashing, and much more * Increased quantitative information about the algorithms, giving you a basis for comparing them * Over 1000 new exercises to help you learn the properties of algorithms Whether you are learning the algorithms for the first time or wish to have up-to-date reference material that incorporates new programming styles with classic and new algorithms, you will find a wealth of useful information in this book.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值