数论入门符号_大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.
您可能正在在線學習編碼教程,或者在新兵訓練營中努力地工作,或者您剛剛開始研究如何過渡到軟件工程,并且一切都已經開始。
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和文本編輯器之間的區別嗎? 檢查一下 您是否可以使用自己選擇的語言來構建CRUD應用程序 ? 檢查一下 您可以相對于算法輸入確定算法的運行時間嗎?
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.
嗯,快點 您所說的運行時是什么意思? 又是什么算法呢? 汗流Google背,您可以使用Google“運行時分析”,這就是Big 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.
在編程中,我們通過專注于最終目標來開始學習之旅。 “如果可行,我很高興”,這是許多初級開發人員常說的一句話。 但是,當您處理更大,更復雜的項目時,了解代碼規模如何對確保您針對遇到的問題設計優雅而有效的解決方案至關重要。
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發揮作用的地方。 大O表示法使我們能夠以代數方式描述代碼的復雜性,在這種方式下,我們可以估計當置于大量數據的壓力下時,代碼將以多快或慢的速度運行。
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.
聽起來復雜嗎? 是的,但這是程序員的一項基本技能,不僅在實踐中,而且在評估我們的能力方面(是的,這將在面試中提出)。 因此-讓我們深吸一口氣,擺脫掉您在PreCalc中度過的剩余創傷,然后將腳趾浸入時空復雜性世界。
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不會為您或您的團隊提供一段代碼要花多長時間的確切答案。 離得很遠。 正如我前面提到的,大O允許我們用代數討論我們的代碼,以獲得它可能如何Swift的大型數據集的壓力下操作感 。 幸運的是,這使我們的新手得到了一些簡化,因為它使我們可以將重點從運行時(以毫秒為單位)轉移到討論與代碼中操作的數量和復雜性有關的運行時。
For example, let’s say we have the following piece of code:
例如,假設我們有以下代碼:
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?
在這里,我們僅循環遍歷提供的數組以找到特定的鍵。 如果找不到該密鑰,那么我們也一定要突出顯示該密鑰。 如果我們使用包含五個項目的數組,總體上來說這是一個相當快的操作,但是如果我們使用此方法在一個數組中搜索關鍵字,則該關鍵字長為1000個項目,或者甚至是100,000個項目呢?
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表示法,我們可以查看我們的算法,然后看它將花費O(n)時間來運行。 與其他可能的解決方案相比,Big O表示法(寫為O(blank))向我們展示了我們的代碼將運行多少個操作以及其運行時間如何增長。
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)上運行,因為在最壞的情況下,如果第一次運行我們的代碼時找不到我們的密鑰,則可能必須繼續循環遍歷整個數組,直到我們確定密鑰是否存在為止并且其運行時間將繼續以恒定速度增長。
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.
為什么我要強調最壞的情況 ? 我們始終希望將精力集中在一個過程上,以了解我們的代碼的外部限制,以避免錯誤的解決方案所花費的最長時間。 它向我們保證,解決方案所用的時間不會比我們預期的長,并且使我們有信心,我們的代碼將來不會導致運行時問題。
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)的運行時與其他運行時相比如何?對于不同類型的解決方案,我們還能期待其他哪些運行時? 嗯,每種算法都有自己的運行時,但是您可以期望看到以下運行相當普遍:
- O(log n) O(log n)
- O(n) 上)
- O(n * log n) O(n *對數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:
目前,您不必擔心如何計算這些(我們目前只是采取一些小步驟)。 您現在只需要知道,上面的列表是按效率排序的-以O(log n)運行的算法比以O(n!)運行的算法運行快得多。 您可以在下面找到一個演示此圖的圖形:
Graph of Possible Runtimes (source: https://bit.ly/31XKRXO)可能的運行時間圖(來源: 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.
最終,這只是一個簡化,事情很少如此整潔。 但是,通過實踐和進一步的研究,您將開始更好地理解如何確定特定算法的Big O運行時間,以及它們如何與其他可能的解決方案進行比較。
結論 (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的快速入門,以及為什么它對軟件工程師如此重要。 如果您只記得本文中的一些內容,應該是:
- Algorithm speed isn’t discussed in actual runtime but Big O Notation. 在實際運行時中并未討論算法速度,而是使用Big 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符號很重要,因為它使我們能夠討論代碼的效率并比較解決方案,而不會陷入復雜的計算中。
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表示法時,我們始終專注于最壞的情況 ,以確保我們選擇最有效的解決方案。
- 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. 比例會顯著影響速度,盡管起初O(n)解決方案可能比O(log n)解決方案快,但隨著向方程中添加更多數據,其變化很快。
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.
希望您能擺脫本文,感到自己有能力自行解決此問題。 正如我前面提到的,“大O符號”和“時空復雜性”是軟件工程真正必不可少的主題,您對它們的理解可能會導致您獲得理想的工作。
Good luck!
祝好運!
翻譯自: https://medium.com/dataseries/a-quick-primer-on-big-o-notation-c99ccc7ddbae
數論入門符號
總結
以上是生活随笔為你收集整理的数论入门符号_大o符号快速入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 期末复习-选择题整理(湖南大学操作系统课
- 下一篇: 利用Google Earth Engin