第二章 Big O notation 進階課程,bignotation

來源:互聯網
上載者:User

第二章 Big O notation 進階課程,bignotation

第二章 

Big O notation 進階課程


在這一章中,我們將會瞭解到演算法的基礎----Counting primitive operation,什麼是primitive operation:

The following are all primitive operations:

1. Assigning a value to a variable

2. Calling another algorithm (function)

3. Performing an arithmetic operation (adding, etc)

4. Indexing into an array

5. Comparing two values (includes all logical and relational

6. operators)

7. Following (dereferencing) an object reference (pointer)

8. Returning from a method (function, procedure)


舉一個例子:

currentMax <-A[0]     2 ops
i <-1                                    1 op
while (i < n) do      1 op
if ( currentMax < A[i] )      2 ops
then currentMax Ω A[i]        2 ops
i <- i + 1                              2 ops
done
return currentMax               1 op

 

Loop Body:

Single loop body iteration: 5 or 7 operations

Loop body repeated n -1 times.

So: between 5(n -1) and 7(n-1) operations, when n > 1
Best case: t = 2 + 1 + 5(n - 1) + 1 = 5n -1
Worst case: t = 2 + 1 + 7(n - 1) + 1 = 7n -3

舉一個我們學校的例子吧

在這裡我們可以看到在這兩個方程中,想得到big O 的話,我們需要將內外兩個loop的big O相乘

 而第一個例子會變成從一到n-1的累加,而結果是n(n-1)/2


在這裡我放上一個講解比較優秀的網站連結,大家如果感興趣可以看一下的:

http://rob-bell.net/2009/06/a-beginners-guide-to-big-o-notation/


另外給大家幾道試題做,大家試試看,下期我會公布答案,並且詳細的解答。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.