数论入门符号_大o符号快速入门
數(shù)論入門符號(hào)
You might be taking coding tutorials online, diligently working through labs in boot camp, or maybe you’ve just started your research into what a transition into Software Engineering looks like, and everything is starting to click into place.
您可能正在在線學(xué)習(xí)編碼教程,或者在新兵訓(xùn)練營(yíng)中努力地工作,或者您剛剛開始研究如何過(guò)渡到軟件工程,并且一切都已經(jīng)開始。
Know the difference between an IDE and a Text Editor? Check. Are you able to build a CRUD app using your language of choice? Check. Can you pin down the runtime of an algorithm relative to its input?
知道IDE和文本編輯器之間的區(qū)別嗎? 檢查一下 您是否可以使用自己選擇的語(yǔ)言來(lái)構(gòu)建CRUD應(yīng)用程序 ? 檢查一下 您可以相對(duì)于算法輸入確定算法的運(yùn)行時(shí)間嗎?
Uh, hold up. What do you mean by runtime? And what exactly is an algorithm again? In a cold sweat, you Google “Runtime Analysis,” and there it is — Big O Notation.
嗯,快點(diǎn) 您所說(shuō)的運(yùn)行時(shí)是什么意思? 又是什么算法呢? 汗流Google背,您可以使用Google“運(yùn)行時(shí)分析”,這就是Big O符號(hào)。
In programming, we start our learning journey by focusing on the end goal. “If it works, I’m happy,” is a common phrase uttered by many a junior developer. But as you work on larger and more complex projects, understanding how your code scales becomes essential in ensuring that you design elegant and efficient solutions to the problems you face.
在編程中,我們通過(guò)專注于最終目標(biāo)來(lái)開始學(xué)習(xí)之旅。 “如果可行,我很高興”,這是許多初級(jí)開發(fā)人員常說(shuō)的一句話。 但是,當(dāng)您處理更大,更復(fù)雜的項(xiàng)目時(shí),了解代碼規(guī)模如何對(duì)確保您針對(duì)遇到的問(wèn)題設(shè)計(jì)優(yōu)雅而有效的解決方案至關(guān)重要。
This is where Big O Notation comes into play. Big O Notation allows us to describe the complexity of our code algebraically in a way where we can estimate how fast or slow our code will function when placed under the strain of large amounts of data.
這就是Big O Notation發(fā)揮作用的地方。 大O表示法使我們能夠以代數(shù)方式描述代碼的復(fù)雜性,在這種方式下,我們可以估計(jì)當(dāng)置于大量數(shù)據(jù)的壓力下時(shí),代碼將以多快或慢的速度運(yùn)行。
Sound complicated? Well, it kind of is, but it’s an essential skill for programmers not only in practice but in the evaluation of our ability (oh yeah, this is going to come up in interviews). So — let’s take a deep breath, shake off that leftover trauma from your days in PreCalc, and dip our toes into the world of Space and Time Complexity.
聽起來(lái)復(fù)雜嗎? 是的,但這是程序員的一項(xiàng)基本技能,不僅在實(shí)踐中,而且在評(píng)估我們的能力方面(是的,這將在面試中提出)。 因此-讓我們深吸一口氣,擺脫掉您在PreCalc中度過(guò)的剩余創(chuàng)傷,然后將腳趾浸入時(shí)空復(fù)雜性世界。
First, let’s be clear about what Big O isn’t. Big O is not going to give you or your team an exact answer on how long a piece of code will take to run. Far from it. As I mentioned earlier, Big O allows us to discuss our code algebraically to get a sense of how quickly it might operate under the strain of large data sets. Luckily, this simplifies things a bit for us newcomers, as it allows us to move our focus from runtime in terms of milliseconds to discussing runtime in relation to the number, and complexity, of operations in our code.
首先,讓我們弄清楚Big O不是什么。 Big O不會(huì)為您或您的團(tuán)隊(duì)提供一段代碼要花多長(zhǎng)時(shí)間的確切答案。 離得很遠(yuǎn)。 正如我前面提到的,大O允許我們用代數(shù)討論我們的代碼,以獲得它可能如何Swift的大型數(shù)據(jù)集的壓力下操作感 。 幸運(yùn)的是,這使我們的新手得到了一些簡(jiǎn)化,因?yàn)樗刮覀兛梢詫⒅攸c(diǎn)從運(yùn)行時(shí)(以毫秒為單位)轉(zhuǎn)移到討論與代碼中操作的數(shù)量和復(fù)雜性有關(guān)的運(yùn)行時(shí)。
For example, let’s say we have the following piece of code:
例如,假設(shè)我們有以下代碼:
def example_search(key, array)for i in 0...array.length
return "#{key} found!" unless array[i] != key
end
return "#{key} not found :("
end
Here, we’re merely looping through a provided array to find a specific key. If that key isn’t found, we’re sure to highlight that as well. It’s a pretty quick operation overall if we’re using an array with five items, but what if we use this method to search for a key in an array 1,000 items long, or even 100,000 items long?
在這里,我們僅循環(huán)遍歷提供的數(shù)組以找到特定的鍵。 如果找不到該密鑰,那么我們也一定要突出顯示該密鑰。 如果我們使用包含五個(gè)項(xiàng)目的數(shù)組,總體上來(lái)說(shuō)這是一個(gè)相當(dāng)快的操作,但是如果我們使用此方法在一個(gè)數(shù)組中搜索關(guān)鍵字,則該關(guān)鍵字長(zhǎng)為1000個(gè)項(xiàng)目,或者甚至是100,000個(gè)項(xiàng)目呢?
Well, with Big O Notation, we can look at our algorithm and see that it will take O(n) time to run. Big O Notation, written as O(blank), show us how many operations our code will run, and how its runtime grows in comparison to other possible solutions.
好了,使用大O表示法,我們可以查看我們的算法,然后看它將花費(fèi)O(n)時(shí)間來(lái)運(yùn)行。 與其他可能的解決方案相比,Big O表示法(寫為O(blank))向我們展示了我們的代碼將運(yùn)行多少個(gè)操作以及其運(yùn)行時(shí)間如何增長(zhǎng)。
Our example code runs at O(n) because in the worst-case if our key isn’t found the first time our code runs, it might have to continue looping through the entire array until we can determine if the key is there or not and its runtime will continue growing at a constant rate.
我們的示例代碼在O(n)上運(yùn)行,因?yàn)樵谧顗牡那闆r下,如果第一次運(yùn)行我們的代碼時(shí)找不到我們的密鑰,則可能必須繼續(xù)循環(huán)遍歷整個(gè)數(shù)組,直到我們確定密鑰是否存在為止并且其運(yùn)行時(shí)間將繼續(xù)以恒定速度增長(zhǎng)。
Why did I emphasize worst-case? We always want to focus on the maximum amount of time a process could take to understand the outer limits of our code to avoid bad solutions. It reassures us that a solution won’t take longer than we expect, and empowers us to feel confident that our code won’t lead to runtime issues in the future.
為什么我要強(qiáng)調(diào)最壞的情況 ? 我們始終希望將精力集中在一個(gè)過(guò)程上,以了解我們的代碼的外部限制,以避免錯(cuò)誤的解決方案所花費(fèi)的最長(zhǎng)時(shí)間。 它向我們保證,解決方案所用的時(shí)間不會(huì)比我們預(yù)期的長(zhǎng),并且使我們有信心,我們的代碼將來(lái)不會(huì)導(dǎo)致運(yùn)行時(shí)問(wèn)題。
So how does a runtime of O(n) compare to other run times, and what other runtimes can we expect for different types of solutions? Well, every algorithm is going to have its own runtime, but you can expect to see the following run fairly commonly:
那么O(n)的運(yùn)行時(shí)與其他運(yùn)行時(shí)相比如何?對(duì)于不同類型的解決方案,我們還能期待其他哪些運(yùn)行時(shí)? 嗯,每種算法都有自己的運(yùn)行時(shí),但是您可以期望看到以下運(yùn)行相當(dāng)普遍:
- O(log n) O(log n)
- O(n) 上)
- O(n * log n) O(n *對(duì)數(shù)n)
- O(n2) O(n2)
- O(n!) 上!)
For now, don’t worry about how to calculate these yet (we’re just taking baby steps for the moment). All you need to know for now is that the list above is ordered by efficiency — algorithms that run at O(log n) run much faster than algorithms that run at O(n!). You can find a graph demonstrating this below:
目前,您不必?fù)?dān)心如何計(jì)算這些(我們目前只是采取一些小步驟)。 您現(xiàn)在只需要知道,上面的列表是按效率排序的-以O(shè)(log n)運(yùn)行的算法比以O(shè)(n!)運(yùn)行的算法運(yùn)行快得多。 您可以在下面找到一個(gè)演示此圖的圖形:
Graph of Possible Runtimes (source: https://bit.ly/31XKRXO)可能的運(yùn)行時(shí)間圖(來(lái)源: https : //bit.ly/31XKRXO )Ultimately, this is a simplification, and things are rarely so neat. But, with practice and further research, you’ll begin to develop a better understanding of how Big O run time can be determined for specific algorithms, and how they compare to other possible solutions.
最終,這只是一個(gè)簡(jiǎn)化,事情很少如此整潔。 但是,通過(guò)實(shí)踐和進(jìn)一步的研究,您將開始更好地理解如何確定特定算法的Big O運(yùn)行時(shí)間,以及它們?nèi)绾闻c其他可能的解決方案進(jìn)行比較。
結(jié)論 (Conclusion)
And that’s it — a quick primer on what Big O Notation, and why it is so essential for Software Engineers. If you only remember a few things from this article, it should be these:
就是這樣-Big O Notation的快速入門,以及為什么它對(duì)軟件工程師如此重要。 如果您只記得本文中的一些內(nèi)容,應(yīng)該是:
- Algorithm speed isn’t discussed in actual runtime but Big O Notation. 在實(shí)際運(yùn)行時(shí)中并未討論算法速度,而是使用Big O符號(hào)。
- Big O Notation is important because it allows us to discuss the efficiency of our code and compare solutions without getting caught up in complex calculations. 大O符號(hào)很重要,因?yàn)樗刮覀兡軌蛴懻摯a的效率并比較解決方案,而不會(huì)陷入復(fù)雜的計(jì)算中。
We always focus on the worst-case when using Big O Notation to ensure that we choose the most efficient solution at scale.
使用Big O表示法時(shí),我們始終專注于最壞的情況 ,以確保我們選擇最有效的解決方案。
- Scale influences speed significantly, and even though an O(n) solution might be faster than an O(log n) solution at first, that changes quickly as you add more data to the equation. 比例會(huì)顯著影響速度,盡管起初O(n)解決方案可能比O(log n)解決方案快,但隨著向方程中添加更多數(shù)據(jù),其變化很快。
Hopefully, you walk away from this article feeling empowered to tackle this subject on your own. As I mentioned earlier, Big O Notation and Space & Time Complexity are subjects that truly essential for Software Engineering, and your understanding of them could be what leads you to land your dream job.
希望您能擺脫本文,感到自己有能力自行解決此問(wèn)題。 正如我前面提到的,“大O符號(hào)”和“時(shí)空復(fù)雜性”是軟件工程真正必不可少的主題,您對(duì)它們的理解可能會(huì)導(dǎo)致您獲得理想的工作。
Good luck!
祝好運(yùn)!
翻譯自: https://medium.com/dataseries/a-quick-primer-on-big-o-notation-c99ccc7ddbae
數(shù)論入門符號(hào)
總結(jié)
以上是生活随笔為你收集整理的数论入门符号_大o符号快速入门的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 期末复习-选择题整理(湖南大学操作系统课
- 下一篇: 利用Google Earth Engin