算法的大O表示法

[英]Big O Notation for an Algorithm


I can't solve a problem; can someone help me?

我無法解決問題;有人能幫我嗎?

What is the Big O notation for the below statement:-

以下聲明的Big O表示法是什么: -

for (int i=2;i<=n;i=i*4)
    sum++;

7 个解决方案

#1


12  

Once i grows exponentially, it is O(log(n)).

一旦我以指數方式增長,它就是O(log(n))。

If n is 16 times larger, it is expected to run the loop only two more times than it would.

如果n大16倍,預計循環運行次數只會比它多兩次。

#2


5  

Try counting the number of loops for a few (small) values of n, then graphing the result (n on horizontal axis, loop count on vertical axis). Playing around with a problem is great when you're first learning.

嘗試計算n的幾個(小)值的循環次數,然后繪制結果圖(水平軸上的n,垂直軸上的循環計數)。當你第一次學習時,解決問題是很好的。

Depending on which values you pick for n, you may not see the pattern. For instance, the loop count is the same for n=10 and n=20. Considering when the loop count will change will also reveal a pattern which can tell you the big-O timing.

根據您為n選擇的值,您可能看不到該模式。例如,對於n = 10和n = 20,循環計數是相同的。考慮到循環計數何時改變還會顯示一個可以告訴你大O時序的模式。

Once you have a better understanding of algorithm timing, you won't need to go through this somewhat time-consuming procedure. You'll be able to figure out the big-O timing algebraically through code analysis.

一旦您對算法時序有了更好的理解,您就不需要經歷這個耗時的過程。您將能夠通過代碼分析以代數方式計算出大O時序。

#3


4  

The way I think of big O notation is how long does it take to complete, which is complexity. For example if you have a bubble sort, as you go to items to be sorted it take approximately n*n operations to complete, which is O(N^2).

我認為大O符號的方式是完成需要多長時間,這就是復雜性。例如,如果您有一個冒泡排序,當您轉到要排序的項目時,需要大約n * n次操作才能完成,即O(N ^ 2)。

For binary search, as you increase n in size, you have log2(n) operations to find the value. Since the number of operation is in terms of log then O(log N) for binary search (where log is log of 2).

對於二進制搜索,當您增加n的大小時,您可以使用log2(n)操作來查找該值。由於操作次數是以log為單位,因此O(log N)用於二進制搜索(其中log是2的log)。

For what you have, has increase you have N number of operations (this is even if you offsetting) as it is increasing in a linear fashion, which is O(N). This is the notation for linear search as it may take n/2 options an average to find a value, it is still O(N).

對於你所擁有的,增加你有N次操作(即使你偏移),因為它以線性方式增加,即O(N)。這是線性搜索的符號,因為它可能需要n / 2個選項作為平均值才能找到一個值,它仍然是O(N)。

I would look at Wikipedia on O(N) notation. It has a more technical explanation, and more big O notation information.

我會看維基百科的O(N)表示法。它有更多的技術解釋,以及更多大的O符號信息。

#4


3  

Once you know that the starting point and multiplier for i are > 1, their exact values make no difference in big-O terms (they only translate into constants added to or multiplying the core component, O(log N), and such constants are neglected in big-O reasoning -- that's the core point of big-O reasoning, after all!!!).

一旦你知道i的起點和乘數大於1,它們的精確值就沒有大O項的差別(它們只能轉換為核心組件O或log乘以或者乘以核心組件的常數,並且這些常數是在大O推理中被忽略 - 這畢竟是大O推理的核心點!!!)。

#5


1  

Since you are doing this as a homework exercise, what you really need to do is go back to first principles; i.e. the mathematical definition of O notation. Work out how many simple computation steps there are as 'n' increases, work out the limit algebraically, and then proceed to the answer.

既然你是做家庭作業,那么你真正需要做的就是回到第一原則;即O符號的數學定義。計算出當'n'增加時有多少簡單的計算步驟,以代數方式計算出限制,然后繼續回答。

In practice, most folks estimate the 'O' complexity, based on knowledge of classical examples and a experience. And quite often they get it wrong.

在實踐中,大多數人基於對經典例子和經驗的了解來估計“O”復雜性。他們經常弄錯了。

#6


0  

Assuming that "sum++" is constant which is a pretty reasonable assumption the algorithm is O(log4 n).

假設“sum ++”是常數,這是一個非常合理的假設,算法是O(log4 n)。

Because the loop goes from 2 to n, you know it is at most O(n). However, because your incremented is multiplied by 4 every loop, there is exponentially less time spent in the loop.

因為循環從2到n,你知道它最多是O(n)。但是,因為每個循環的增量乘以4,所以在循環中花費的時間會少得多。

#7


0  

The first 4 values of i are 2, 8, 32, 128 so the formula that shows how many iterations that loops will go through is:

i的前4個值是2,8,32,128,因此顯示循環將經歷多少次迭代的公式為:

((log(n)/log(2))/2)+0.5


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/08/10/5a9e3da5aa5933b6c70595d6ed2045ff.html



 
  © 2014-2022 ITdaan.com 联系我们: