知识点6:计算的复杂度(computational complexity)

If we have a really efficient algorithm, we can minimize the amount of resources we have available to deal with it.

If we have an algorithm that is going to take a lot of work to process a really large set of data, it's going to require more and more resources, which is money, RAM, all that kind of stuff.


When we talk about the complexity of an algorithm -- which sometimes you'll hear it referred to as time complexity or space complexity but we're just going to call complexity -- we're generally talking about the worst-case scenario.

We generally call the worst-case runtime of an algorithm big-O.


Sometimes, though, we do care about the best-case scenario. If the data is everything we wanted it to be and it was absolutely perfect and we were sending this perfect set of data through our algorithm.

How would it handle in that situation?

We sometimes refer to that as big-Omega, so in contrast with big-O, we have big-Omega.


Big-Omega for the best-case scenario.

Big-O for the worst-case scenario.

Generally, when we talk about the complexity of an algorithm, we're talking about the worst-case scenario.


Well, what is a data set?

It means whatever makes the most sense in context, to be honest(在程序里有用的那些都是数据集). If we have an algorithm, the Processes Strings -- we're probably talking about the size of the string. That's the data set -- the size, the number of characters that make up the string.

If we're talking about an algorithm that processes files, we might be talking about how many kilobytes comprise that file. And that's the data set.

If we're talking about an algorithm that handles arrays more generally, such as sorting algorithms or searching algorithms, we're probably talking about the number of elements that comprise an array.


Now, we can measure an algorithm-- in particular, when I say we can measure an algorithm, I mean we can measure how many resources(内存或时间) it takes up. Whether those resources are, how many bytes of RAM-- or megabytes of RAM it uses. Or how much time it takes to run.

we can call this measure, arbitrarily, f of n. Where n is the number of elements in the data set. And f of n is how many units of resources does it require to process that data.


Now, we actually don't care about what f of n is exactly. We're just going to talk about what f of n is approximately or what it tends to.

And the tendency of an algorithm is dictated by its highest order term.

And we can see what I mean by that by takinga look at a more concrete example:

So let's say that we have three different algorithms.

Now again, we really aren't going to get into this level of detail. I'm really just have these up here as an illustration of a point that I'm going to be making in a second, which is that we only really care about the tendency of things as the data sets get bigger.

So if the data set is small, there's actually a pretty big difference in these algorithms. The third algorithm there takes 13 times longer, 13 times the amount of resources to run relative to the first one.

If our data set is size 10, which is bigger, but not necessarily huge, we can see that there's actually a bit of a difference. The third algorithm becomes more efficient. It's about actually 40%-- or 60% more efficient. It takes 40% the amount of time. It can run-- it can take 400 units of resources to process a data set of size 10.

Whereas the first algorithm, by contrast, takes 1,000 units of resources to process a data set of size 10. But look what happens as our numbers get even bigger.


Now, the difference between these algorithms start to become a little less apparent(指第三行:n=1000). And the fact that there are lower-order terms -- or rather, terms with lower exponents -- start to become irrelevant.

If a data set is of size 1,000 and the first algorithm runs in a billion steps. And the second algorithm runs in a billion and a million steps. And the third algorithm runs in just shy of a billion steps. It's pretty much a billion steps.

Those lower-order terms start to become really irrelevant.

And just to really hammer home the point -- if the data input is of size a million -- all three of these pretty much take one quintillion -- if my math is correct -- steps to process a data input of size a million. That's a lot of steps.

And the fact that one of them might take a couple 100,000, or a couple 100 million even less when we're talking about a number that big-- it's kind of irrelevant.

They all tend to take approximately n cubed, and so we would actually refer to all of these algorithms as being on the order of n cubed or big-O of n cubed.


Here's a list of some of the more common computational complexity classes that we'll encounter in algorithms, generally.

These are ordered from generally fastest at the top, to generally slowest at the bottom.


So constant time algorithms tend to be the fastest, regardless of the size of the data input you pass in. They always take one operation or one unit of resources to deal with. It might be 2, it might be 3, it might be 4. But it's a constant number. It doesn't vary.

Logarithmic time algorithms are slightly better. And a really good example of a logarithmic time algorithm you've surely seen by now is the tearing apart of the phone book to find Mike Smith in the phone book.

We cut the problem in half. And so as n gets larger and larger and larger -- in fact, every time you double n, it only takes one more step.

So that's a lot better than, say, linear time. Which is if you double n, it takes double the number of steps. If you triple n, it takes triple the number of steps. One step per unit.

Then things get a little more-- little less great from there. You have linear rhythmic time, sometimes called log linear time or just n log n. And we'll an example of an algorithm that runs in n log n, which is still better than quadratic time-- n squared. Or polynomial time, n two any number greater than two. Or exponential time, which is even worse-- C to the n.

So some constant number raised to the power of the size of the input. So if there's 1,000-- if the data input is of size 1,000, it would take C to the 1,000th power. It's a lot worse than polynomial time.

Factorial time is even worse. And in fact, there really do exist infinite time algorithms, such as, so-called stupid sort-- whose job is to randomly shuffle an array and then check to see whether it's sorted.
And if it's not, randomly shuffle the array again and check to see whether it's sorted. And as you can probably imagine-- you can imagine a situation where in the worst-case, that will never actually start with the array. That algorithm would run forever. And so that would be an infinite time algorithm.


some examples:

一个for循环是O(n),而for内嵌一个for循环是n乘以n,即O(n*2)。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 203,547评论 6 477
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,399评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,428评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,599评论 1 274
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,612评论 5 365
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,577评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,941评论 3 395
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,603评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,852评论 1 297
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,605评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,693评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,375评论 4 318
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,955评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,936评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,172评论 1 259
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 43,970评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,414评论 2 342

推荐阅读更多精彩内容

  • 假如你想要得到一件东西 那么就请就放它走 它如若够能回来找你 那么就将会永远地属于你 但它如若不回来 那根本就不...
    墨樱雪阅读 209评论 0 1
  • 怀念小学一二年级的时候,那时家乡特别爱下大雨,大雨积在路边低洼处可以形成很深的水沟。那时的我最怕下大雨,因为没有防...
    朵朵咩咩阅读 248评论 0 0
  • 1、根据“定期不定额”的投资方法,将上一课中所列的定投计划进行优化。并从以下三种情况任选其一,做个计划: 如果你已...
    缘是你的俊阅读 177评论 0 0
  • 哪个行业的老板不是起早睡晚的, 一个成功的人要会利用所有的资源。 在成功路上,爱你的人很多,决定你成功的是你能听懂...
    乔峰amway阅读 2,028评论 0 0
  • 每周都会和家人一起去看场电影,尤其喜欢接近午夜场的,因为票钱便宜。上周有人推荐《黑处有什么》,就在团购网站上搜索。...
    李大仙儿的精神世界阅读 356评论 0 0