inf 2b sorting mergesort and divide and conquer
play

Inf 2B: Sorting, MergeSort and Divide-and-Conquer Lecture 7 of ADS - PowerPoint PPT Presentation

Inf 2B: Sorting, MergeSort and Divide-and-Conquer Lecture 7 of ADS thread Kyriakos Kalorkoti School of Informatics University of Edinburgh The Sorting Problem Input: Array A of items with comparable keys . Task: Sort the items in A by


  1. Inf 2B: Sorting, MergeSort and Divide-and-Conquer Lecture 7 of ADS thread Kyriakos Kalorkoti School of Informatics University of Edinburgh

  2. The Sorting Problem Input: Array A of items with comparable keys . Task: Sort the items in A by increasing keys. The number of items to be sorted is usually denoted by n .

  3. What is important? Worst-case running-time: What are the bounds on T Sort ( n ) for our Sorting Algorithm Sort. In-place or not?: A sorting algorithm is in-place if it can be (simply) implemented on the input array, with only O ( 1 ) extra space (extra variables). Stable or not?: A sorting algorithm is stable if for every pair of indices with A [ i ] . key = A [ j ] . key and i < j , the entry A [ i ] comes before A [ j ] in the output array.

  4. Insertion Sort Algorithm insertionSort ( A ) 1. for j ← 1 to A . length − 1 do 2. a ← A [ j ] 3. i ← j − 1 4. while i ≥ 0 and A [ i ] . key > a . key do 5. A [ i + 1 ] ← A [ i ] 6. i ← i − 1 7. A [ i + 1 ] ← a ◮ Asymptotic worst-case running time: Θ( n 2 ) . ◮ The worst-case (which gives Ω( n 2 ) ) is � n , n − 1 , . . . , 1 � . ◮ Both stable and in-place.

  5. 2nd sorting algorithm - Merge Sort 12 6 4 9 13 8 5 split in Divide the middle 13 12 6 4 9 8 5 sort & recursively 4 6 9 12 5 8 13 merge solutions together Conquer 4 5 8 9 12 13 6

  6. Merge Sort - recursive structure Algorithm mergeSort ( A , i , j ) 1. if i < j then mid ← ⌊ i + j 2. 2 ⌋ 3. mergeSort ( A , i , mid ) 4. mergeSort ( A , mid + 1 , j ) 5. merge ( A , i , mid , j ) Running Time: � Θ( 1 ) , for n ≤ 1 ; T ( n ) = T ( ⌈ n / 2 ⌉ ) + T ( ⌊ n / 2 ⌋ ) + T merge ( n ) + Θ( 1 ) , for n ≥ 2 . How do we perform the merging?

  7. Merging the two subarrays A 8 11 12 4 9 21 4 B m k=i l=mid+1 A 8 11 12 4 9 21 4 8 B k l 4 8 9 B A 8 11 12 4 9 21 l k A 8 11 12 4 9 21 k l New array B for output. Θ( j − i + 1 ) time (linear time) always (best and worst cases).

  8. Merge pseudocode Algorithm merge ( A , i , mid , j ) 13. while k ≤ mid do 1. new array B of length j − i + 1 B [ m ] ← A [ k ] 14. 2. k ← i 3. ℓ ← mid + 1 k ← k + 1 15. 4. m ← 0 16. m ← m + 1 5. while k ≤ mid and ℓ ≤ j do 17. while ℓ ≤ j do 6. if A [ k ] . key < = A [ ℓ ] . key then 18. B [ m ] ← A [ ℓ ] 7. B [ m ] ← A [ k ] 19. ℓ ← ℓ + 1 8. k ← k + 1 9. else 20. m ← m + 1 10. B [ m ] ← A [ ℓ ] 21. for m = 0 to j − i do 11. ℓ ← ℓ + 1 22. A [ m + i ] ← B [ m ] 12. m ← m + 1

  9. Question on mergeSort What is the status of mergeSort in regard to stability and in-place sorting ? 1. Both stable and in-place. 2. Stable but not in-place. 3. Not stable, but is in-place. 4. Neither stable nor in-place. Answer: not in-place but it is stable. If line 6 reads < instead of < = , we have sorting but NOT Stability.

  10. Analysis of Mergesort ◮ merge T merge ( n ) = Θ( n ) ◮ mergeSort � Θ( 1 ) , for n ≤ 1 ; T ( n ) = T ( ⌈ n 2 ⌉ ) + T ( ⌊ n 2 ⌋ ) + T merge ( n ) + Θ( 1 ) , for n ≥ 2 . � Θ( 1 ) , for n ≤ 1 ; = T ( ⌈ n 2 ⌉ ) + T ( ⌊ n 2 ⌋ ) + Θ( n ) , for n ≥ 2 . Solution to recurrence: T ( n ) = Θ( n lg n ) .

  11. Solving the mergeSort recurrence Write with constants c , d : � c , for n ≤ 1 ; T ( n ) = T ( ⌈ n 2 ⌉ ) + T ( ⌊ n 2 ⌋ ) + dn , for n ≥ 2 . Suppose n = 2 k for some k . Then no floors/ceilings. � c , for n = 1 ; T ( n ) = 2 T ( n 2 ) + dn , for n ≥ 2 .

  12. Solving the mergeSort recurrence Put ℓ = lg n (hence 2 ℓ = n ). T ( n ) = 2 T ( n / 2 ) + dn 2 T ( n / 2 2 ) + d ( n / 2 ) � � = + dn 2 2 2 T ( n / 2 2 ) + 2 dn = 2 2 � 2 T ( n / 2 3 ) + d ( n / 2 2 ) � = + 2 dn 2 3 T ( n / 2 3 ) + 3 dn = . . . 2 k T ( n / 2 k ) + kdn = 2 ℓ T ( n / 2 ℓ ) + ℓ dn = = nT ( 1 ) + ℓ dn = cn + dn lg ( n ) = Θ( n lg ( n )) . Can extend to n not a power of 2 (see notes).

  13. Merge Sort vs. Insertion Sort ◮ Merge Sort is much more efficient But: ◮ If the array is “almost” sorted, Insertion Sort only needs “almost” linear time, while Merge Sort needs time Θ( n lg ( n )) even in the best case. ◮ For very small arrays, Insertion Sort is better because Merge Sort has overhead from the recursive calls. ◮ Insertion Sort sorts in place , mergeSort does not (needs Ω( n ) additional memory cells).

  14. Divide-and-Conquer Algorithms ◮ Divide the input instance into several instances P 1 , P 2 , . . . P a of the same problem of smaller size - “setting-up". ◮ Recursively solve the problem on these smaller instances. ◮ Solve small enough instances directly. ◮ Combine the solutions for the smaller instances P 1 , P 2 , . . . P a to a solution for the original instance. Do some “extra work" for this.

  15. Analysing Divide-and-Conquer Algorithms Analysis of divide-and-conquer algorithms yields recurrences like this: � Θ( 1 ) , if n < n 0 ; T ( n ) = T ( n 1 ) + . . . + T ( n a ) + f ( n ) , if n ≥ n 0 . f ( n ) is the time for “setting-up" and “extra work." Usually recurrences can be simplified: � Θ( 1 ) , if n < n 0 ; T ( n ) = aT ( n / b ) + Θ( n k ) , if n ≥ n 0 , where n 0 , a , k ∈ N , b ∈ R with n 0 > 0, a > 0 and b > 1 are constants. (Disregarding floors and ceilings.)

  16. The Master Theorem Theorem: Let n 0 ∈ N , k ∈ N 0 and a , b ∈ R with a > 0 and b > 1, and let T : N → R satisfy the following recurrence: � Θ( 1 ) , if n < n 0 ; T ( n ) = aT ( n / b ) + Θ( n k ) , if n ≥ n 0 . Let e = log b ( a ) ; we call e the critical exponent . Then Θ( n e ) ,  if k < e (I) ;  Θ( n e lg ( n )) , T ( n ) = if k = e (II) ; Θ( n k ) , if k > e (III) .  ◮ Theorem still true if we replace aT ( n / b ) by a 1 T ( ⌊ n / b ⌋ ) + a 2 T ( ⌈ n / b ⌉ ) for a 1 , a 2 ≥ 0 with a 1 + a 2 = a .

  17. Master Theorem in use Example 1: We can “read off” the recurrence for mergeSort: � Θ( 1 ) , n ≤ 1 ; T mergeSort ( n ) = T mergeSort ( ⌈ n 2 ⌉ ) + T mergeSort ( ⌊ n 2 ⌋ ) + Θ( n ) , n ≥ 2 . In Master Theorem terms, we have n 0 = 2 , k = 1 , a = 2 , b = 2 . Thus e = log b ( a ) = log 2 ( 2 ) = 1 . Hence T mergeSort ( n ) = Θ( n lg ( n )) by case (II).

  18. . . . Master Theorem Example 2: Let T be a function satisfying � Θ( 1 ) , if n ≤ 1 ; T ( n ) = 7 T ( n / 2 ) + Θ( n 4 ) , if n ≥ 2 . e = log b ( a ) = log 2 ( 7 ) < 3 So T ( n ) = Θ( n 4 ) by case (III) .

  19. Further Reading ◮ If you have [GT], the “Sorting Sets and Selection" chapter has a section on mergeSort ( . ) ◮ If you have [CLRS], there is an entire chapter on recurrences.

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend