




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、Chapter 2.Getting StartedOutlineFamiliarize you with the framework to think about the design and analysis of algorithmsIntroduce two sorting algorithms: insertion sort and merge sort Start to understand how to analyze the efficiency of algorithmsWe mainly concern about running time, or speed. Other
2、issues could also affect efficiency, e.g. memory, storage.Example: Sorting ProblemInput: A sequence of n numbersOutput: A permutation (reordering) of the input sequence such that PseudocodeDescribe algorithms as programs written in a pseudo codeEmploy whatever expressive method is most clear and con
3、cise to specify a given algorithmNot concerned with issues of software engineering, such as data abstraction, modularity and error handling Insertion SortIt is an efficient algorithm for sorting a small number of elementsIt works the way many people sort a hand of playing cardsIn insertion sort, the
4、 input numbers are sorted in place: the number are rearranged within the arrayInsertion SortFind an appropriate position to insert the new card into sorted cards in handsComparing and exchange in reverse orderProve the CorrectnessDesign, Prove and AnalyzeOften Use a loop invariantHow to define loop
5、invariant is importantE.g. for insertion sort:Loop invariant: At the start of each iteration of the “outer” for loop (line1-8)- the loop indexed by j - the sub-array A1 . . j-1 consists of the elements originally in A1 . j-1 but in sorted order.Loop InvariantTo use a loop invariant to prove correctn
6、ess, show three things about it:Initialization: It is true prior to the first iteration of the loop.Maintenance: If it is true before an iteration of the loop, it remains true before the next iteration.Termination: When the loop terminates, the invariantusually along with the reason that the loop te
7、rminatedgives us a useful property that helps show that the algorithm is correct.Analyzing algorithmsRandom-access machine(RAM) modelHow do we analyze an algorithms running time?The time taken by an algorithm depends on the input itself (e.g. already sorted)Input size: depends on the problem being s
8、tudied. (parameterize in input size)Want upper bound (guarantee to users)Running time: on a particular input, it is the number of primitive operations(steps) executed.RAM MODEL Do not model the memory hierarchyKinds of Analysis Worst-case (Usually) T(n)=max time on any input of size n Average-case (
9、Sometimes) T(n)=expected time over all input of size n How do we know the probability of every particular input is? I do not know. Make assumption of statistical distribution of inputs (what is the common assumption? ) Best-Case (bogus) Some slow algorithms work well on some input , cheating. Detail
10、ed Analysis of Algorithmn : the number of inputst j : the # of times the while loop test is executed.The loop test is executed one time more than the loop body.Thus, the running time of the above Insertion-Sort algorithm is:T(n) = c1n + c2(n-1) + c4(n-1) + c5 j=2 . n tj+ c6 j=2 . n(tj -1) + c7 j=2 .
11、 n(tj -1) + c8(n-1) = (c5/2 + c6/2 + c7/2) n2 + (c1 + c2 + c4 + c5/2 - c6/2 - c7/2 + c8) n (c2+c4+c5+c8).t j : the # of times the while loop test is executed.The loop test is executed one time more than the loop body.Thus, the running time of the above Insertion-Sort algorithm is:T(n) = c1n + c2(n-1
12、) + c4(n-1) + c5 j=2 . n tj+ c6 j=2 . n(tj -1) + c7 j=2 . n(tj -1) + c8(n-1)Detailed Analysis of Running timeThis worst-case running time can be expressed as an2+bn+c , it is thus a quadratic functionAnalysis of Insertion SortThe running time of the algorithm is: (cost of statement) x ( # of times s
13、tatement is executed) all statementstj = # of times that while loop test is executed for that value of j.Best case: the array is already sorted (all tj = 1)Worst case: the array is in reverse order (tj = j).The worst case running time gives a guaranteed upper bound on the running time for any input.
14、Average case: On average, the key in A j is less than half the elements in A1 . j-1 and its greater than the other half. (tj = j /2).n(n-1)/2Order of Growth The abstraction to ease analysis and focus on the important features. Look only at the leading term of the formula for running time.Drop lower-
15、order terms.Ignore the constant coefficient in the leading term. Example: an + bn + c = (n)Drop lower-order terms anIgnore constant coefficient nThe worst case running time T(n) grows like n; it does not equal n.The running time is (n) to capture the notion that the order of growth is n.Order of gro
16、wth (2)We usually consider one algorithm to be more efficient than another if its worst-case running time has a lower order of growthDue to constant factors and lower-order terms, this evaluation may be error for small inputs but for large enough inputs, it is true.Designing algorithmsDivide and Con
17、querDivide the problem into a number of subproblems.Conquer the subproblems by solving them recursively.Base case: If the subproblems are small enough, just solve them.Combine the subproblem solutions to give a solution to the original problem.Cf.) Incremental method insertion sort.Merge SortA sorti
18、ng algorithm based on divide and conquer. The worst-case running time: Tmerge_sort the size of a subproblem.Use this technique when we analyze recursive algorithmLine 1-3 and 8-11 takes constant time and for loop takes (n1+n2) timeLoop Invariant of MERGELoop Invariant At the start of each iteration
19、of the for loop of lines 12-17, the subarray Apk-1 contains k-p elements of L1.n1+1 and R1. n2+1,in sorted order. Moreover, Li and Rj are the smallest elements of their arrays that have not been copied back into A.Show correctness using loop invariantWe show this loop invariant holds priori to the first iteration of the for loop and each iteration maintains the invariant and the invariant show correctness when the loop terminates.Loop InvariantInitialization Priori to the first iteration of the loop, we have k=p. Ap.k-1 is
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 軟件技術轉讓及服務合同
- 選民行為及其影響試題及答案
- 基礎網絡安全防護措施試題及答案
- 軟件設計師考試2025年常見問題解析試題及答案
- 軟件設計師考試的深刻見解試題與答案
- 比較西方政治制度下的中央與地方關系試題及答案
- 西方國家的國際援助政策試題及答案
- 智能科技對公共政策的變革性影響試題及答案
- 公共政策執行中的溝通策略試題及答案
- 西方政治文化的構成要素試題及答案
- 2023年廣西高考歷史真題(含答案)
- 四川建筑施工資料表格(施工單位用表)全套
- 工作作風不嚴謹的表現及改進措施范文(通用5篇)
- 過濾器檢驗報告
- DB11-T 675-2014 清潔生產評價指標體系 醫藥制造業
- 2023家具采購合同范本專業版-1
- GB/T 29319-2012光伏發電系統接入配電網技術規定
- GB/T 11264-2012熱軋輕軌
- 國學導論·巴蜀文化課件
- 《事業單位人事管理條例》及其配套制度講義課件
- 試卷交接簽字單
評論
0/150
提交評論