




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
1、2022-3-2212022-3-222Review of last classbDivide and Conquer techniquebThe merge sort algorithm and its best-case, worst-case and average-case analysis2022-3-223Divide And Conquer(II)Chapter 42022-3-224Goals of the LecturebAt the end of this lecture, you should Master the idea of quick sort algorithm
2、 Master the best-case, worst-case and average-case analysis of quick sort algorithm Understand the difference between merge sort and quick sort2022-3-225QuicksortbAnother sort algorithm example to reveal the essence of divide-and-conquer techniquebIts idea can be described as follows: Divide: Partit
3、ion the array Ap.r into two sub arrays Ap.q and Aq+1.r Invariant: All elements in Ap.q are less than all elements in Aq+1.r Conquer: Sort the two sub arrays recursively Merge: Unlike merge sort, no combining step, two sub arrays form an already-sorted array2022-3-226Quicksort AlgorithmALGORITHM Quic
4、kSort(A, l, r)/Sorts a subarray by quicksort/Input: A subarray Alr of A0n-1, defined by its/ left and right indices l and r/Output: Subarray Alr sorted in nondecreasing order if l r s Partition(A, l, r) /s is a split position QuickSort(A, l, s-1) QuickSort(A, s+1, r) end if2022-3-227bClearly, all th
5、e action takes place in the divide step should be the followings: Select a pivot and rearranges the array in place End result: Two sub arrays been separated by the pivotThe elements in one sub array are smaller than the pivotThe elements in another are larger than the pivot Returns the index of the
6、“pivot” element separating the two sub arraysbHow do you suppose we implement this?Partition2022-3-228Partition In WordsbGiven an array A0,n-1, we can partition the array like these: (1) Initial: select an element to act as the “pivot” (which?), let i and j indicate the index of second left and righ
7、t elements which will be used to compare with the pivot. (2) Scan from left: Increase i until Ai greater than and equal to the pivot. (3) Scan from right: Decrease j until Aj smaller than and equal to the pivot. (4) Swap Ai and Aj (5) Repeat (2),(3) and (4) until ji. (6) Swap Ai and Aj, swap A0 and
8、Aj, return j.2022-3-229Partition example Initial list6, 7, 5, 2, 5, 8ji6, 7, 5, 2, 5, 8ji6, 5, 5, 2, 7, 8ji6, 5, 5, 2, 7, 8jiDone!Quciksort is not stableQuciksort is not stable6, 7, 5, 2, 5, 82, 5, 5 6 7, 82022-3-2210Partition Algorithm(I)ALGORITHM Partition1(A, l, r) /Input: A subarray Alr of A0n-1
9、, defined by its left / and right indices l and r (lr) /Output: A partition of Alr, with the split position / returned as this functions valuep Al i l; j r + 1 repeat repeat i i + 1 until Ai p repeat j j - 1 until Aj p swap(Ai, Aj); until i j swap(Ai, Aj) / undo last s i j swap(Al, Ajreturn j / j is
10、 the final index of pivot Any Problem ?2022-3-2211Partition Algorithm(II)ALGORITHM Partition2(A, l, r) /Input: A subarray Alr of A0n-1, defined by its left / and right indices l and r (lr) /Output: A partition of Alr, with the split position / returned as this functions valuep Al; j l for i l +1 to
11、r do if Ai p j j + 1 if j i swap(Aj, Ai) end if end forswap(Al, Aj)return j / j is the final index of pivot Why not “ ?2022-3-2212Quicksort Example5 8 4 9 3 6 7 2 initial array 2 4 3 5 8 6 7 9 the first partition 2 4 3 5 8 6 7 9 the second partition 2 3 4 5 8 6 7 9 the third partition 2 3 4 5 8 6 7
12、9 the fourth partition 2 3 4 5 7 6 8 9 the fifth partition 2 3 4 5 6 7 8 9 the sixth partition 2 3 4 5 6 7 8 9 the seventh partition 2 3 4 5 6 7 8 9 the eighth partition 2022-3-2213Analyzing QuicksortbWhat will be the best case for the algorithm? Partition is perfectly balanced, this means that the
13、array is divided into two equal length subarrays.bIn the best case:Tbest(1) = 0Tbest(n) = 2Tbest(n/2) + n-1bWhat does the recurrence relation work out to?T(n) = (nlogn) 2022-3-2214Analyzing QuicksortbWhat will be the worst case for the algorithm? Partition is always unbalancedbWill any particular in
14、put elicit the worst case? Yes: Already-sorted inputbIn the worst case, Partition will do n-1 comparisons, but create one partition that has n-1 elements and the other will have no elementsbBecause we wind up just reducing the partition by one element each time, worst case is given by:T(1) = 0T(n) =
15、 T(n - 1) + n - 1bThe recurrence relation works out to T(n) = (n2)2022-3-2215 Improving QuicksortbThe real liability of quicksort is that it runs in O(n2) on already-sorted inputbDiscusses two solutions: Randomize the input array, OR Pick a random pivot elementbHow will these solve the problem? By i
16、nsuring that no particular input can be chosen to make quicksort run in O(n2) time2022-3-2216Analyzing Quicksort: Average CasebAssuming random input, average-case running time is much closer to (nlogn) than O(n2)bFirst, a more intuitive explanation/example: Suppose that partition always produces a 9
17、-to-1 split. This looks quite unbalanced! The recurrence is thus:T(n) = T(9n/10) + T(n/10) + n-1How deep will the recursion go? (draw it)2022-3-2217Analyzing Quicksort: Average CasebIntuitively, a real-life run of quicksort will produce a mix of “bad” and “good” splits Randomly distributed among the
18、 recursion tree Pretend for intuition that they alternate between best-case (n/2 : n/2) and worst-case (n-1 : 0) What happens if we bad-split root node, then good-split the resulting size (n-1) node?2022-3-2218Analyzing Quicksort: Average CasebIntuitively, a real-life run of quicksort will produce a
19、 mix of “bad” and “good” splits Randomly distributed among the recursion tree Pretend for intuition that they alternate between best-case (n/2 : n/2) and worst-case (n-1 : 0) What happens if we bad-split root node, then good-split the resulting size (n-1) node? We end up with three subarrays, size 0
20、, (n-1)/2, (n-1)/2 Combined cost of splits = n-1 + n -2 = 2n -3 = O(n) No worse than if we had good-split the root node!2022-3-2219Analyzing Quicksort: Average CasebIntuitively, the O(n) cost of a bad split (or 2 or 3 bad splits) can be absorbed into the O(n) cost of each good splitbThus running tim
21、e of alternating bad and good splits is still O(nlogn), with slightly higher constantsbHow can we be more rigorous?2022-3-2220Analyzing Quicksort: Average CasebFor simplicity, assume: All inputs distinct (no repeats) Slightly different partition procedurepartition around a random element, which is n
22、ot included in subarraysall splits (0:n-1, 1:n-2, 2:n-3, , n-1:0) equally likelybWhat is the probability of a particular split happening?bAnswer: 1/n2022-3-2221Analyzing Quicksort: Average CasebSo partition generates splits (0:n-1, 1:n-2, 2:n-3, , n-2:1, n-1:0) each with probability 1/nbIf T(n) is t
23、he expected running time,bWhat is each term under the summation for?bWhat is the (n) term for? 1011( )nkT nT kT nknn 2022-3-2222Analyzing Quicksort: Average CasebSo 101011121nknkT nT kT nknnT knn 2022-3-2223Analyzing Quicksort: Average CasebWe can solve this recurrence using the dreaded substitution
24、 method Guess the answer Assume that the inductive hypothesis holds Substitute it in for some value n Prove that it follows for n2022-3-2224Analyzing Quicksort: Average CasebWe can solve this recurrence using the dreaded substitution method Guess the answerWhats the answer? Assume that the inductive
25、 hypothesis holds Substitute it in for some value n Prove that it follows for n2022-3-2225Analyzing Quicksort: Average CasebWe can solve this recurrence using the dreaded substitution method Guess the answerT(n) = (nlogn) Assume that the inductive hypothesis holds Substitute it in for some value n P
26、rove that it follows for n2022-3-2226Analyzing Quicksort: Average CasebWe can solve this recurrence using the dreaded substitution method Guess the answerT(n) = (nlogn) Assume that the inductive hypothesis holdsWhats the inductive hypothesis? Substitute it in for some value n Prove that it follows f
27、or n2022-3-2227Analyzing Quicksort: Average CasebWe can solve this recurrence using the dreaded substitution method Guess the answerT(n) = (nlogn) Assume that the inductive hypothesis holdsT(n) anlogn + b for some constants a and b Substitute it in for some value n Prove that it follows for n2022-3-
28、2228Analyzing Quicksort: Average CasebWe can solve this recurrence using the dreaded substitution method Guess the answerT(n) = (nlogn) Assume that the inductive hypothesis holdsT(n) anlogn + b for some constants a and b Substitute it in for some value nWhat value? Prove that it follows for n2022-3-
29、2229Analyzing Quicksort: Average CasebWe can solve this recurrence using the dreaded substitution method Guess the answerT(n) = (nlogn) Assume that the inductive hypothesis holdsT(n) anlogn + b for some constants a and b Substitute it in for some value nThe value k in the recurrence Prove that it fo
30、llows for n2022-3-2230What are we doing here?Analyzing Quicksort: Average Case 101011111122log2log22log2lognknknknknkT nT knnakkbnnbakkbnnbakkbnnnakkbnn The recurrence to be solvedWhat are we doing here?What are we doing here?Plug in inductive hypothesisExpand out the k=0 case2b/n is just a constant
31、, so fold it into (n)2022-3-2231What are we doing here?What are we doing here?Evaluate the summation: b+b+b = b (n-1)The recurrence to be solvedSince n-1n, 2b(n-1)/n 2bAnalyzing Quicksort: Average Case 11111111112log22log22log(1)2log2nknnkknknkT nakkbnnakkbnnnabkknnnnakkbnn What are we doing here?Di
32、stribute the summationThis summation gets its own set of slides later2022-3-2232How did we do this?Pick a large enough thatan/4 dominates (n)+b What are we doing here?Remember, our goal is to get T(n) an log n + bWhat the hell?Well prove this laterWhat are we doing here?Distribute the (2a/n) termThe
33、 recurrence to be solvedAnalyzing Quicksort: Average Case 11222log2211log228log24log4lognkaT nkkbnnannnbnnaannnbnaannbnbnannb 2022-3-2233Analyzing Quicksort: Average CasebSo T(n) an log n + b for certain a and b Thus the induction holds Thus T(n) = (nlogn) Thus quicksort runs in (nlogn) time on aver
34、age (phew!)bOh yeah, the summation 2022-3-2234What are we doing here?The log k in the second term is bounded by log nTightly Bounding of the Key Summation21111122111221112logloglogloglogloglognnnkkknnnkknnnkknkkkkkkkkknkknkWhat are we doing here?Move the log n outside the summationWhat are we doing
35、here?Split the summation for a tighter bound2022-3-2235The summation bound so farTightly Bounding of the Key Summation2111112211122111221112loglogloglog2loglog1loglog1lognnnkkknnnkknnnkknnnkknkkkknkknnkknnknknkWhat are we doing here?The log k in the first term is bounded by log n/2What are we doing here?log n/2 = log n - 1What are we doing here?Move (log n - 1) outside the summation2022-3-2236The
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 山西職業(yè)技術(shù)學院《化工廠設(shè)計基礎(chǔ)》2023-2024學年第二學期期末試卷
- 南京信息職業(yè)技術(shù)學院《世界少數(shù)族裔文學》2023-2024學年第二學期期末試卷
- 湖南商務(wù)職業(yè)技術(shù)學院《電子設(shè)計制造與測試一》2023-2024學年第二學期期末試卷
- 南陽醫(yī)學高等??茖W校《鏡頭語言與導演基礎(chǔ)》2023-2024學年第二學期期末試卷
- 廣東農(nóng)工商職業(yè)技術(shù)學院《工程招投標》2023-2024學年第二學期期末試卷
- 貴州民族大學《建筑荷載》2023-2024學年第二學期期末試卷
- 四川民族學院《BIM造價管理應(yīng)用》2023-2024學年第二學期期末試卷
- 玉溪職業(yè)技術(shù)學院《圖像采集與處理》2023-2024學年第二學期期末試卷
- 湖南有色金屬職業(yè)技術(shù)學院《安全心理學》2023-2024學年第二學期期末試卷
- 廈門理工學院《醫(yī)學影像設(shè)備學》2023-2024學年第二學期期末試卷
- GB/T 45501-2025工業(yè)機器人三維視覺引導系統(tǒng)通用技術(shù)要求
- 2025年武漢數(shù)學四調(diào)試題及答案
- 2024年全國高中數(shù)學聯(lián)賽北京賽區(qū)預賽一試試題(解析版)
- 技能大師工作室成員協(xié)議范本書
- PICC??谱o士進修學習匯報
- 工廠如何消除靜電與防止靜電實踐篇
- 我學會了洗碗作文
- 武漢市住宅專項維修資金使用申請表
- 牛津譯林版英語八年級下冊8B——單詞默寫(表格版)
- 霍尼韋爾x溫控儀中文說明書——有程序設(shè)定篇
- 人們通過合作取得更大的成功辯論稿
評論
0/150
提交評論