Bubble Sort Merge Sort Bubble Sort Sorting Sorting
![Bubble Sort Merge Sort Bubble Sort Merge Sort](https://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-1.jpg)
Bubble Sort Merge Sort
![Bubble Sort Bubble Sort](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-2.jpg)
Bubble Sort
![Sorting • Sorting takes an unordered collection and makes it an ordered one. 1 Sorting • Sorting takes an unordered collection and makes it an ordered one. 1](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-3.jpg)
Sorting • Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 77 42 35 12 101 5 1 2 3 4 5 6 5 12 35 42 77 101
!["Bubbling Up" the Largest Element • Traverse a collection of elements – Move from "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-4.jpg)
"Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 77 42 35 12 101 5
!["Bubbling Up" the Largest Element • Traverse a collection of elements – Move from "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-5.jpg)
"Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 42 Swap 77 42 77 35 12 101 5
!["Bubbling Up" the Largest Element • Traverse a collection of elements – Move from "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-6.jpg)
"Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 42 2 3 35 Swap 35 77 77 4 5 6 12 101 5
!["Bubbling Up" the Largest Element • Traverse a collection of elements – Move from "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-7.jpg)
"Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 42 35 3 4 12 Swap 12 77 77 5 6 101 5
!["Bubbling Up" the Largest Element • Traverse a collection of elements – Move from "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-8.jpg)
"Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 42 35 12 77 101 5 No need to swap
!["Bubbling Up" the Largest Element • Traverse a collection of elements – Move from "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-9.jpg)
"Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 42 35 12 77 5 6 5 Swap 101 5
!["Bubbling Up" the Largest Element • Traverse a collection of elements – Move from "Bubbling Up" the Largest Element • Traverse a collection of elements – Move from](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-10.jpg)
"Bubbling Up" the Largest Element • Traverse a collection of elements – Move from the front to the end – “Bubble” the largest value to the end using pair-wise comparisons and swapping 1 2 3 4 5 6 42 35 12 77 5 101 Largest value correctly placed
![The “Bubble Up” Algorithm index <- 1 last_compare_at <- n – 1 loop exitif(index The “Bubble Up” Algorithm index <- 1 last_compare_at <- n – 1 loop exitif(index](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-11.jpg)
The “Bubble Up” Algorithm index <- 1 last_compare_at <- n – 1 loop exitif(index > last_compare_at) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop
![LB No, Swap isn’t built in. Procedure Swap(a, b isoftype in/out Num) t isoftype LB No, Swap isn’t built in. Procedure Swap(a, b isoftype in/out Num) t isoftype](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-12.jpg)
LB No, Swap isn’t built in. Procedure Swap(a, b isoftype in/out Num) t isoftype Num t <- a a <- b b <- t endprocedure // Swap
![Items of Interest • Notice that only the largest value is correctly placed • Items of Interest • Notice that only the largest value is correctly placed •](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-13.jpg)
Items of Interest • Notice that only the largest value is correctly placed • All other values are still out of order • So we need to repeat this process 1 2 3 4 5 6 42 35 12 77 5 101 Largest value correctly placed
![Repeat “Bubble Up” How Many Times? • If we have N elements… • And Repeat “Bubble Up” How Many Times? • If we have N elements… • And](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-14.jpg)
Repeat “Bubble Up” How Many Times? • If we have N elements… • And if each time we bubble an element, we place it in its correct location… • Then we repeat the “bubble up” process N – 1 times. • This guarantees we’ll correctly place all N elements.
![N-1 “Bubbling” All the Elements 1 42 2 35 3 12 4 77 5 N-1 “Bubbling” All the Elements 1 42 2 35 3 12 4 77 5](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-15.jpg)
N-1 “Bubbling” All the Elements 1 42 2 35 3 12 4 77 5 5 6 101 1 35 2 12 3 42 4 5 5 77 6 101 1 12 3 5 3 35 4 42 5 77 6 101 1 12 2 35 2 5 4 42 5 77 6 101 1 5 2 12 3 35 4 42 5 77 6 101
![Reducing the Number of Comparisons 1 77 1 42 2 35 3 12 4 Reducing the Number of Comparisons 1 77 1 42 2 35 3 12 4](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-16.jpg)
Reducing the Number of Comparisons 1 77 1 42 2 35 3 12 4 77 5 101 5 5 6 101 1 35 2 12 3 42 4 5 5 77 6 101 1 12 2 35 3 5 4 42 5 77 6 101 1 12 2 5 3 35 4 42 5 77 6 101
![Reducing the Number of Comparisons • On the Nth “bubble up”, we only need Reducing the Number of Comparisons • On the Nth “bubble up”, we only need](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-17.jpg)
Reducing the Number of Comparisons • On the Nth “bubble up”, we only need to do MAX-N comparisons. • For example: – This is the 4 th “bubble up” – MAX is 6 – Thus we have 2 comparisons to do 1 12 2 35 3 5 4 42 5 77 6 101
![Putting It All Together Putting It All Together](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-18.jpg)
Putting It All Together
![N is … // Size of Array Arr_Type definesa Array[1. . N] of Num N is … // Size of Array Arr_Type definesa Array[1. . N] of Num](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-19.jpg)
N is … // Size of Array Arr_Type definesa Array[1. . N] of Num Procedure Swap(n 1, n 2 isoftype in/out Num) temp isoftype Num temp <- n 1 <- n 2 <- temp endprocedure // Swap
![Outer loop exitif(to_do = 0) index <- 1 loop exitif(index > to_do) if(A[index] > Outer loop exitif(to_do = 0) index <- 1 loop exitif(index > to_do) if(A[index] >](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-20.jpg)
Outer loop exitif(to_do = 0) index <- 1 loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) endif index <- index + 1 endloop to_do <- to_do - 1 endloop endprocedure // Bubblesort Inner loop procedure Bubblesort(A isoftype in/out Arr_Type) to_do, index isoftype Num to_do <- N – 1
![Already Sorted Collections? • What if the collection was already sorted? • What if Already Sorted Collections? • What if the collection was already sorted? • What if](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-21.jpg)
Already Sorted Collections? • What if the collection was already sorted? • What if only a few elements were out of place and after a couple of “bubble ups, ” the collection was sorted? • We want to be able to detect this and “stop early”! 1 5 2 12 3 35 4 42 5 77 6 101
![Using a Boolean “Flag” • We can use a boolean variable to determine if Using a Boolean “Flag” • We can use a boolean variable to determine if](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-22.jpg)
Using a Boolean “Flag” • We can use a boolean variable to determine if any swapping occurred during the “bubble up. ” • If no swapping occurred, then we know that the collection is already sorted! • This boolean “flag” needs to be reset after each “bubble up. ”
![did_swap isoftype Boolean did_swap <- true loop exitif ((to_do = 0) OR NOT(did_swap)) index did_swap isoftype Boolean did_swap <- true loop exitif ((to_do = 0) OR NOT(did_swap)) index](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-23.jpg)
did_swap isoftype Boolean did_swap <- true loop exitif ((to_do = 0) OR NOT(did_swap)) index <- 1 did_swap <- false loop exitif(index > to_do) if(A[index] > A[index + 1]) then Swap(A[index], A[index + 1]) did_swap <- true endif index <- index + 1 endloop to_do <- to_do - 1 endloop
![An Animated Example N 8 to_do 7 did_swap true index 98 23 45 14 An Animated Example N 8 to_do 7 did_swap true index 98 23 45 14](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-24.jpg)
An Animated Example N 8 to_do 7 did_swap true index 98 23 45 14 1 2 3 4 6 67 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 1 did_swap false 98 23 45 An Animated Example N 8 to_do 7 index 1 did_swap false 98 23 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-25.jpg)
An Animated Example N 8 to_do 7 index 1 did_swap false 98 23 45 14 1 2 3 4 6 67 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 1 did_swap false Swap 98 23 An Animated Example N 8 to_do 7 index 1 did_swap false Swap 98 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-26.jpg)
An Animated Example N 8 to_do 7 index 1 did_swap false Swap 98 23 45 14 1 2 3 4 6 67 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 1 did_swap true Swap 23 98 An Animated Example N 8 to_do 7 index 1 did_swap true Swap 23 98](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-27.jpg)
An Animated Example N 8 to_do 7 index 1 did_swap true Swap 23 98 45 14 1 2 3 4 6 67 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 2 did_swap 23 98 45 14 An Animated Example N 8 to_do 7 index 2 did_swap 23 98 45 14](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-28.jpg)
An Animated Example N 8 to_do 7 index 2 did_swap 23 98 45 14 1 2 3 4 true 6 67 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 2 did_swap true Swap 23 98 An Animated Example N 8 to_do 7 index 2 did_swap true Swap 23 98](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-29.jpg)
An Animated Example N 8 to_do 7 index 2 did_swap true Swap 23 98 45 14 1 2 3 4 6 67 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 2 did_swap true Swap 23 45 An Animated Example N 8 to_do 7 index 2 did_swap true Swap 23 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-30.jpg)
An Animated Example N 8 to_do 7 index 2 did_swap true Swap 23 45 98 14 1 2 3 4 6 67 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 3 did_swap 23 45 98 14 An Animated Example N 8 to_do 7 index 3 did_swap 23 45 98 14](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-31.jpg)
An Animated Example N 8 to_do 7 index 3 did_swap 23 45 98 14 1 2 3 4 true 6 67 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 3 did_swap true Swap 23 45 An Animated Example N 8 to_do 7 index 3 did_swap true Swap 23 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-32.jpg)
An Animated Example N 8 to_do 7 index 3 did_swap true Swap 23 45 98 14 1 2 3 4 6 67 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 3 did_swap true Swap 23 45 An Animated Example N 8 to_do 7 index 3 did_swap true Swap 23 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-33.jpg)
An Animated Example N 8 to_do 7 index 3 did_swap true Swap 23 45 14 98 1 2 3 4 6 67 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 4 did_swap 23 45 14 98 An Animated Example N 8 to_do 7 index 4 did_swap 23 45 14 98](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-34.jpg)
An Animated Example N 8 to_do 7 index 4 did_swap 23 45 14 98 1 2 3 4 true 6 67 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 4 did_swap true Swap 23 45 An Animated Example N 8 to_do 7 index 4 did_swap true Swap 23 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-35.jpg)
An Animated Example N 8 to_do 7 index 4 did_swap true Swap 23 45 14 98 1 2 3 4 6 67 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 4 did_swap true Swap 23 45 An Animated Example N 8 to_do 7 index 4 did_swap true Swap 23 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-36.jpg)
An Animated Example N 8 to_do 7 index 4 did_swap true Swap 23 45 14 6 1 2 3 4 98 67 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 5 did_swap 23 45 14 6 An Animated Example N 8 to_do 7 index 5 did_swap 23 45 14 6](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-37.jpg)
An Animated Example N 8 to_do 7 index 5 did_swap 23 45 14 6 1 2 3 4 true 98 67 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 5 did_swap true Swap 23 45 An Animated Example N 8 to_do 7 index 5 did_swap true Swap 23 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-38.jpg)
An Animated Example N 8 to_do 7 index 5 did_swap true Swap 23 45 14 6 1 2 3 4 98 67 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 5 did_swap true Swap 23 45 An Animated Example N 8 to_do 7 index 5 did_swap true Swap 23 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-39.jpg)
An Animated Example N 8 to_do 7 index 5 did_swap true Swap 23 45 14 6 1 2 3 4 67 98 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 6 did_swap 23 45 14 6 An Animated Example N 8 to_do 7 index 6 did_swap 23 45 14 6](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-40.jpg)
An Animated Example N 8 to_do 7 index 6 did_swap 23 45 14 6 1 2 3 4 true 67 98 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 6 did_swap true Swap 23 45 An Animated Example N 8 to_do 7 index 6 did_swap true Swap 23 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-41.jpg)
An Animated Example N 8 to_do 7 index 6 did_swap true Swap 23 45 14 6 1 2 3 4 67 98 33 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 6 did_swap true Swap 23 45 An Animated Example N 8 to_do 7 index 6 did_swap true Swap 23 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-42.jpg)
An Animated Example N 8 to_do 7 index 6 did_swap true Swap 23 45 14 6 1 2 3 4 67 33 98 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 7 did_swap 23 45 14 6 An Animated Example N 8 to_do 7 index 7 did_swap 23 45 14 6](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-43.jpg)
An Animated Example N 8 to_do 7 index 7 did_swap 23 45 14 6 1 2 3 4 true 67 33 98 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 7 did_swap true Swap 23 45 An Animated Example N 8 to_do 7 index 7 did_swap true Swap 23 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-44.jpg)
An Animated Example N 8 to_do 7 index 7 did_swap true Swap 23 45 14 6 1 2 3 4 67 33 98 42 5 6 7 8
![An Animated Example N 8 to_do 7 index 7 did_swap true Swap 23 45 An Animated Example N 8 to_do 7 index 7 did_swap true Swap 23 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-45.jpg)
An Animated Example N 8 to_do 7 index 7 did_swap true Swap 23 45 14 6 1 2 3 4 67 33 42 98 5 6 7 8
![After First Pass of Outer Loop N 8 to_do 7 index 8 did_swap Finished After First Pass of Outer Loop N 8 to_do 7 index 8 did_swap Finished](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-46.jpg)
After First Pass of Outer Loop N 8 to_do 7 index 8 did_swap Finished first “Bubble Up” 23 45 14 6 1 true 2 3 4 67 33 42 98 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 1 did_swap false 23 45 The Second “Bubble Up” N 8 to_do 6 index 1 did_swap false 23 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-47.jpg)
The Second “Bubble Up” N 8 to_do 6 index 1 did_swap false 23 45 14 6 1 2 3 4 67 33 42 98 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 1 did_swap false No Swap The Second “Bubble Up” N 8 to_do 6 index 1 did_swap false No Swap](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-48.jpg)
The Second “Bubble Up” N 8 to_do 6 index 1 did_swap false No Swap 23 45 14 6 1 2 3 4 67 33 42 98 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 2 did_swap false 23 45 The Second “Bubble Up” N 8 to_do 6 index 2 did_swap false 23 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-49.jpg)
The Second “Bubble Up” N 8 to_do 6 index 2 did_swap false 23 45 14 6 1 2 3 4 67 33 42 98 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 2 did_swap false Swap 23 The Second “Bubble Up” N 8 to_do 6 index 2 did_swap false Swap 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-50.jpg)
The Second “Bubble Up” N 8 to_do 6 index 2 did_swap false Swap 23 45 14 6 1 2 3 4 67 33 42 98 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 2 did_swap true Swap 23 The Second “Bubble Up” N 8 to_do 6 index 2 did_swap true Swap 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-51.jpg)
The Second “Bubble Up” N 8 to_do 6 index 2 did_swap true Swap 23 14 45 6 1 2 3 4 67 33 42 98 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 3 did_swap 23 14 45 The Second “Bubble Up” N 8 to_do 6 index 3 did_swap 23 14 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-52.jpg)
The Second “Bubble Up” N 8 to_do 6 index 3 did_swap 23 14 45 6 1 2 3 4 true 67 33 42 98 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 3 did_swap true Swap 23 The Second “Bubble Up” N 8 to_do 6 index 3 did_swap true Swap 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-53.jpg)
The Second “Bubble Up” N 8 to_do 6 index 3 did_swap true Swap 23 14 45 6 1 2 3 4 67 33 42 98 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 3 did_swap true Swap 23 The Second “Bubble Up” N 8 to_do 6 index 3 did_swap true Swap 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-54.jpg)
The Second “Bubble Up” N 8 to_do 6 index 3 did_swap true Swap 23 14 6 1 2 3 45 67 33 42 98 4 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 4 did_swap 23 14 6 The Second “Bubble Up” N 8 to_do 6 index 4 did_swap 23 14 6](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-55.jpg)
The Second “Bubble Up” N 8 to_do 6 index 4 did_swap 23 14 6 1 2 3 true 45 67 33 42 98 4 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 4 did_swap true No Swap The Second “Bubble Up” N 8 to_do 6 index 4 did_swap true No Swap](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-56.jpg)
The Second “Bubble Up” N 8 to_do 6 index 4 did_swap true No Swap 23 14 6 1 2 3 45 67 33 42 98 4 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 5 did_swap 23 14 6 The Second “Bubble Up” N 8 to_do 6 index 5 did_swap 23 14 6](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-57.jpg)
The Second “Bubble Up” N 8 to_do 6 index 5 did_swap 23 14 6 1 2 3 true 45 67 33 42 98 4 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 5 did_swap true Swap 23 The Second “Bubble Up” N 8 to_do 6 index 5 did_swap true Swap 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-58.jpg)
The Second “Bubble Up” N 8 to_do 6 index 5 did_swap true Swap 23 14 6 1 2 3 45 67 33 42 98 4 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 5 did_swap true Swap 23 The Second “Bubble Up” N 8 to_do 6 index 5 did_swap true Swap 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-59.jpg)
The Second “Bubble Up” N 8 to_do 6 index 5 did_swap true Swap 23 14 6 1 2 3 45 33 67 42 98 4 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 6 did_swap 23 14 6 The Second “Bubble Up” N 8 to_do 6 index 6 did_swap 23 14 6](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-60.jpg)
The Second “Bubble Up” N 8 to_do 6 index 6 did_swap 23 14 6 1 2 3 true 45 33 67 42 98 4 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 6 did_swap true Swap 23 The Second “Bubble Up” N 8 to_do 6 index 6 did_swap true Swap 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-61.jpg)
The Second “Bubble Up” N 8 to_do 6 index 6 did_swap true Swap 23 14 6 1 2 3 45 33 67 42 98 4 5 6 7 8
![The Second “Bubble Up” N 8 to_do 6 index 6 did_swap true Swap 23 The Second “Bubble Up” N 8 to_do 6 index 6 did_swap true Swap 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-62.jpg)
The Second “Bubble Up” N 8 to_do 6 index 6 did_swap true Swap 23 14 6 1 2 3 45 33 42 67 98 4 5 6 7 8
![After Second Pass of Outer Loop N 8 to_do 6 index 7 did_swap Finished After Second Pass of Outer Loop N 8 to_do 6 index 7 did_swap Finished](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-63.jpg)
After Second Pass of Outer Loop N 8 to_do 6 index 7 did_swap Finished second “Bubble Up” 23 14 6 1 true 2 3 45 33 42 67 98 4 5 6 7 8
![The Third “Bubble Up” N 8 to_do 5 index 1 did_swap false 23 14 The Third “Bubble Up” N 8 to_do 5 index 1 did_swap false 23 14](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-64.jpg)
The Third “Bubble Up” N 8 to_do 5 index 1 did_swap false 23 14 6 1 2 3 45 33 42 67 98 4 5 6 7 8
![The Third “Bubble Up” N 8 to_do 5 index 1 did_swap false Swap 23 The Third “Bubble Up” N 8 to_do 5 index 1 did_swap false Swap 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-65.jpg)
The Third “Bubble Up” N 8 to_do 5 index 1 did_swap false Swap 23 14 6 1 2 3 45 33 42 67 98 4 5 6 7 8
![The Third “Bubble Up” N 8 to_do 5 index 1 did_swap true Swap 14 The Third “Bubble Up” N 8 to_do 5 index 1 did_swap true Swap 14](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-66.jpg)
The Third “Bubble Up” N 8 to_do 5 index 1 did_swap true Swap 14 23 6 1 2 3 45 33 42 67 98 4 5 6 7 8
![The Third “Bubble Up” N 8 to_do 5 index 2 did_swap 14 23 6 The Third “Bubble Up” N 8 to_do 5 index 2 did_swap 14 23 6](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-67.jpg)
The Third “Bubble Up” N 8 to_do 5 index 2 did_swap 14 23 6 1 2 3 true 45 33 42 67 98 4 5 6 7 8
![The Third “Bubble Up” N 8 to_do 5 index 2 did_swap true Swap 14 The Third “Bubble Up” N 8 to_do 5 index 2 did_swap true Swap 14](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-68.jpg)
The Third “Bubble Up” N 8 to_do 5 index 2 did_swap true Swap 14 23 6 1 2 3 45 33 42 67 98 4 5 6 7 8
![The Third “Bubble Up” N 8 to_do 5 index 2 did_swap true Swap 14 The Third “Bubble Up” N 8 to_do 5 index 2 did_swap true Swap 14](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-69.jpg)
The Third “Bubble Up” N 8 to_do 5 index 2 did_swap true Swap 14 6 1 2 23 45 33 42 67 98 3 4 5 6 7 8
![The Third “Bubble Up” N 8 to_do 5 index 3 did_swap 14 6 1 The Third “Bubble Up” N 8 to_do 5 index 3 did_swap 14 6 1](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-70.jpg)
The Third “Bubble Up” N 8 to_do 5 index 3 did_swap 14 6 1 2 true 23 45 33 42 67 98 3 4 5 6 7 8
![The Third “Bubble Up” N 8 to_do 5 index 3 did_swap true No Swap The Third “Bubble Up” N 8 to_do 5 index 3 did_swap true No Swap](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-71.jpg)
The Third “Bubble Up” N 8 to_do 5 index 3 did_swap true No Swap 14 6 1 2 23 45 33 42 67 98 3 4 5 6 7 8
![The Third “Bubble Up” N 8 to_do 5 index 4 did_swap 14 6 1 The Third “Bubble Up” N 8 to_do 5 index 4 did_swap 14 6 1](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-72.jpg)
The Third “Bubble Up” N 8 to_do 5 index 4 did_swap 14 6 1 2 true 23 45 33 42 67 98 3 4 5 6 7 8
![The Third “Bubble Up” N 8 to_do 5 index 4 did_swap true Swap 14 The Third “Bubble Up” N 8 to_do 5 index 4 did_swap true Swap 14](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-73.jpg)
The Third “Bubble Up” N 8 to_do 5 index 4 did_swap true Swap 14 6 1 2 23 45 33 42 67 98 3 4 5 6 7 8
![The Third “Bubble Up” N 8 to_do 5 index 4 did_swap true Swap 14 The Third “Bubble Up” N 8 to_do 5 index 4 did_swap true Swap 14](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-74.jpg)
The Third “Bubble Up” N 8 to_do 5 index 4 did_swap true Swap 14 6 1 2 23 33 45 42 67 98 3 4 5 6 7 8
![The Third “Bubble Up” N 8 to_do 5 index 5 did_swap 14 6 1 The Third “Bubble Up” N 8 to_do 5 index 5 did_swap 14 6 1](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-75.jpg)
The Third “Bubble Up” N 8 to_do 5 index 5 did_swap 14 6 1 2 true 23 33 45 42 67 98 3 4 5 6 7 8
![The Third “Bubble Up” N 8 to_do 5 index 5 did_swap true Swap 14 The Third “Bubble Up” N 8 to_do 5 index 5 did_swap true Swap 14](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-76.jpg)
The Third “Bubble Up” N 8 to_do 5 index 5 did_swap true Swap 14 6 1 2 23 33 45 42 67 98 3 4 5 6 7 8
![The Third “Bubble Up” N 8 to_do 5 index 5 did_swap true Swap 14 The Third “Bubble Up” N 8 to_do 5 index 5 did_swap true Swap 14](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-77.jpg)
The Third “Bubble Up” N 8 to_do 5 index 5 did_swap true Swap 14 6 1 2 23 33 42 45 67 98 3 4 5 6 7 8
![After Third Pass of Outer Loop N 8 to_do 5 index 6 did_swap Finished After Third Pass of Outer Loop N 8 to_do 5 index 6 did_swap Finished](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-78.jpg)
After Third Pass of Outer Loop N 8 to_do 5 index 6 did_swap Finished third “Bubble Up” 14 6 1 true 2 23 33 42 45 67 98 3 4 5 6 7 8
![The Fourth “Bubble Up” N 8 to_do 4 index 1 did_swap false 14 6 The Fourth “Bubble Up” N 8 to_do 4 index 1 did_swap false 14 6](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-79.jpg)
The Fourth “Bubble Up” N 8 to_do 4 index 1 did_swap false 14 6 1 2 23 33 42 45 67 98 3 4 5 6 7 8
![The Fourth “Bubble Up” N 8 to_do 4 index 1 did_swap false Swap 14 The Fourth “Bubble Up” N 8 to_do 4 index 1 did_swap false Swap 14](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-80.jpg)
The Fourth “Bubble Up” N 8 to_do 4 index 1 did_swap false Swap 14 6 1 2 23 33 42 45 67 98 3 4 5 6 7 8
![The Fourth “Bubble Up” N 8 to_do 4 index 1 did_swap true Swap 6 The Fourth “Bubble Up” N 8 to_do 4 index 1 did_swap true Swap 6](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-81.jpg)
The Fourth “Bubble Up” N 8 to_do 4 index 1 did_swap true Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![The Fourth “Bubble Up” N 8 to_do 4 index 2 6 1 did_swap true The Fourth “Bubble Up” N 8 to_do 4 index 2 6 1 did_swap true](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-82.jpg)
The Fourth “Bubble Up” N 8 to_do 4 index 2 6 1 did_swap true 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![The Fourth “Bubble Up” N 8 to_do 4 index 2 did_swap true No Swap The Fourth “Bubble Up” N 8 to_do 4 index 2 did_swap true No Swap](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-83.jpg)
The Fourth “Bubble Up” N 8 to_do 4 index 2 did_swap true No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![The Fourth “Bubble Up” N 8 to_do 4 index 3 6 1 did_swap true The Fourth “Bubble Up” N 8 to_do 4 index 3 6 1 did_swap true](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-84.jpg)
The Fourth “Bubble Up” N 8 to_do 4 index 3 6 1 did_swap true 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![The Fourth “Bubble Up” N 8 to_do 4 index 3 did_swap true No Swap The Fourth “Bubble Up” N 8 to_do 4 index 3 did_swap true No Swap](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-85.jpg)
The Fourth “Bubble Up” N 8 to_do 4 index 3 did_swap true No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![The Fourth “Bubble Up” N 8 to_do 4 index 4 6 1 did_swap true The Fourth “Bubble Up” N 8 to_do 4 index 4 6 1 did_swap true](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-86.jpg)
The Fourth “Bubble Up” N 8 to_do 4 index 4 6 1 did_swap true 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![The Fourth “Bubble Up” N 8 to_do 4 index 4 did_swap true No Swap The Fourth “Bubble Up” N 8 to_do 4 index 4 did_swap true No Swap](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-87.jpg)
The Fourth “Bubble Up” N 8 to_do 4 index 4 did_swap true No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![After Fourth Pass of Outer Loop N 8 to_do 4 index 5 6 1 After Fourth Pass of Outer Loop N 8 to_do 4 index 5 6 1](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-88.jpg)
After Fourth Pass of Outer Loop N 8 to_do 4 index 5 6 1 did_swap true Finished fourth “Bubble Up” 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![The Fifth “Bubble Up” N 8 to_do 3 index 1 6 1 did_swap false The Fifth “Bubble Up” N 8 to_do 3 index 1 6 1 did_swap false](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-89.jpg)
The Fifth “Bubble Up” N 8 to_do 3 index 1 6 1 did_swap false 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![The Fifth “Bubble Up” N 8 to_do 3 index 1 did_swap false No Swap The Fifth “Bubble Up” N 8 to_do 3 index 1 did_swap false No Swap](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-90.jpg)
The Fifth “Bubble Up” N 8 to_do 3 index 1 did_swap false No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![The Fifth “Bubble Up” N 8 to_do 3 index 2 6 1 did_swap false The Fifth “Bubble Up” N 8 to_do 3 index 2 6 1 did_swap false](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-91.jpg)
The Fifth “Bubble Up” N 8 to_do 3 index 2 6 1 did_swap false 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![The Fifth “Bubble Up” N 8 to_do 3 index 2 did_swap false No Swap The Fifth “Bubble Up” N 8 to_do 3 index 2 did_swap false No Swap](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-92.jpg)
The Fifth “Bubble Up” N 8 to_do 3 index 2 did_swap false No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![The Fifth “Bubble Up” N 8 to_do 3 index 3 6 1 did_swap false The Fifth “Bubble Up” N 8 to_do 3 index 3 6 1 did_swap false](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-93.jpg)
The Fifth “Bubble Up” N 8 to_do 3 index 3 6 1 did_swap false 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![The Fifth “Bubble Up” N 8 to_do 3 index 3 did_swap false No Swap The Fifth “Bubble Up” N 8 to_do 3 index 3 did_swap false No Swap](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-94.jpg)
The Fifth “Bubble Up” N 8 to_do 3 index 3 did_swap false No Swap 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![After Fifth Pass of Outer Loop N 8 to_do 3 index 4 6 1 After Fifth Pass of Outer Loop N 8 to_do 3 index 4 6 1](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-95.jpg)
After Fifth Pass of Outer Loop N 8 to_do 3 index 4 6 1 did_swap false Finished fifth “Bubble Up” 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![Finished “Early” N 8 to_do 3 index 4 did_swap false We didn’t do any Finished “Early” N 8 to_do 3 index 4 did_swap false We didn’t do any](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-96.jpg)
Finished “Early” N 8 to_do 3 index 4 did_swap false We didn’t do any swapping, so all of the other elements must be correctly placed. We can “skip” the last two passes of the outer loop. 6 1 14 23 33 42 45 67 98 2 3 4 5 6 7 8
![Summary • “Bubble Up” algorithm will move largest value to its correct location (to Summary • “Bubble Up” algorithm will move largest value to its correct location (to](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-97.jpg)
Summary • “Bubble Up” algorithm will move largest value to its correct location (to the right) • Repeat “Bubble Up” until all elements are correctly placed: – Maximum of N-1 times – Can finish early if no swapping occurs • We reduce the number of elements we compare each time one is correctly placed
![LB Truth in CS Act • NOBODY EVER USES BUBBLE SORT • NOBODY • LB Truth in CS Act • NOBODY EVER USES BUBBLE SORT • NOBODY •](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-98.jpg)
LB Truth in CS Act • NOBODY EVER USES BUBBLE SORT • NOBODY • NOT EVER • BECAUSE IT IS EXTREMELY INEFFICIENT
![Questions? Questions?](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-99.jpg)
Questions?
![Mergesort Mergesort](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-100.jpg)
Mergesort
![Sorting • Sorting takes an unordered collection and makes it an ordered one. 1 Sorting • Sorting takes an unordered collection and makes it an ordered one. 1](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-101.jpg)
Sorting • Sorting takes an unordered collection and makes it an ordered one. 1 2 3 4 5 6 77 42 35 12 101 5 1 2 3 4 5 6 5 12 35 42 77 101
![Divide and Conquer • Divide and Conquer cuts the problem in half each time, Divide and Conquer • Divide and Conquer cuts the problem in half each time,](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-102.jpg)
Divide and Conquer • Divide and Conquer cuts the problem in half each time, but uses the result of both halves: – cut the problem in half until the problem is trivial – solve for both halves – combine the solutions
![Mergesort • A divide-and-conquer algorithm: • Divide the unsorted array into 2 halves until Mergesort • A divide-and-conquer algorithm: • Divide the unsorted array into 2 halves until](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-103.jpg)
Mergesort • A divide-and-conquer algorithm: • Divide the unsorted array into 2 halves until the sub-arrays only contain one element • Merge the sub-problem solutions together: – Compare the sub-array’s first elements – Remove the smallest element and put it into the result array – Continue the process until all elements have been put into the result array 37 23 6 89 15 12 2 19
![How to Remember Merge Sort? That’s easy. Just remember Mariah Carey. As a singing How to Remember Merge Sort? That’s easy. Just remember Mariah Carey. As a singing](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-104.jpg)
How to Remember Merge Sort? That’s easy. Just remember Mariah Carey. As a singing star, Ms. Carey has perfected the “wax-on” wave motion--a clockwise sweep of her hand used to emphasize lyrics. The Maria “Wax-on” Angle: ¦(q, t) The Siren of Subquadratic Sorts
![How To Remember Merge Sort? ¦(q, t) Just as Mariah recursively moves her hands How To Remember Merge Sort? ¦(q, t) Just as Mariah recursively moves her hands](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-105.jpg)
How To Remember Merge Sort? ¦(q, t) Just as Mariah recursively moves her hands into smaller circles, so too does merge sort recursively split an array into smaller segments. We need two such recursions, one for each half of the split array.
![Algorithm Mergesort(Passed an array) if array size > 1 Divide array in half Call Algorithm Mergesort(Passed an array) if array size > 1 Divide array in half Call](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-106.jpg)
Algorithm Mergesort(Passed an array) if array size > 1 Divide array in half Call Mergesort on first half. Call Mergesort on second half. Merge two halves. Merge(Passed two arrays) Compare leading element in each array Select lower and place in new array. (If one input array is empty then place remainder of other array in output array)
![LB More TRUTH in CS • We don’t really pass in two arrays! • LB More TRUTH in CS • We don’t really pass in two arrays! •](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-107.jpg)
LB More TRUTH in CS • We don’t really pass in two arrays! • We pass in one array with indicator variables which tell us where one set of data starts and finishes and where the other set of data starts and finishes. • Honest. s 1 f 1 s 2 f 2
![LB Algorithm Mergesort(Passed an array) if array size > 1 Divide array in half LB Algorithm Mergesort(Passed an array) if array size > 1 Divide array in half](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-108.jpg)
LB Algorithm Mergesort(Passed an array) if array size > 1 Divide array in half Call Mergesort on first half. Call Mergesort on second half. Merge two halves. Merge(Passed two arrays) Compare leading element in each array Select lower and place in new array. (If one input array is empty then place remainder of other array in output array)
![98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-109.jpg)
98 23 45 14 6 67 33 42
![98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-110.jpg)
98 23 45 14 6 67 33 42
![98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-111.jpg)
98 23 45 14 6 67 33 42
![98 23 45 14 6 67 33 42 98 23 45 14 6 67 33 42](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-112.jpg)
98 23 45 14 6 67 33 42
![98 23 45 14 98 23 Merge 45 14 6 67 33 42 98 23 45 14 98 23 Merge 45 14 6 67 33 42](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-113.jpg)
98 23 45 14 98 23 Merge 45 14 6 67 33 42
![98 23 45 14 98 23 23 Merge 45 14 6 67 33 42 98 23 45 14 98 23 23 Merge 45 14 6 67 33 42](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-114.jpg)
98 23 45 14 98 23 23 Merge 45 14 6 67 33 42
![98 23 45 14 98 23 23 98 Merge 45 14 6 67 33 98 23 45 14 98 23 23 98 Merge 45 14 6 67 33](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-115.jpg)
98 23 45 14 98 23 23 98 Merge 45 14 6 67 33 42
![98 23 45 14 98 23 23 98 45 14 6 67 33 42 98 23 45 14 98 23 23 98 45 14 6 67 33 42](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-116.jpg)
98 23 45 14 98 23 23 98 45 14 6 67 33 42
![98 23 45 14 23 98 Merge 6 67 33 42 98 23 45 14 23 98 Merge 6 67 33 42](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-117.jpg)
98 23 45 14 23 98 Merge 6 67 33 42
![98 23 45 14 98 23 23 98 45 14 14 Merge 6 67 98 23 45 14 98 23 23 98 45 14 14 Merge 6 67](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-118.jpg)
98 23 45 14 98 23 23 98 45 14 14 Merge 6 67 33 42
![98 23 45 14 98 23 23 98 45 14 14 45 Merge 6 98 23 45 14 98 23 23 98 45 14 14 45 Merge 6](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-119.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 Merge 6 67 33 42
![98 23 45 14 98 23 23 98 45 14 14 45 Merge 6 98 23 45 14 98 23 23 98 45 14 14 45 Merge 6](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-120.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 Merge 6 67 33 42
![98 23 45 14 98 23 23 98 45 14 14 45 14 Merge 98 23 45 14 98 23 23 98 45 14 14 45 14 Merge](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-121.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 Merge 6 67 33 42
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-122.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 Merge 6 67 33 42
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-123.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 Merge 6 67 33 42
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-124.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 Merge 6 67 33 42
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-125.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-126.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42
![98 23 45 14 98 23 23 98 45 14 6 67 33 42 98 23 45 14 98 23 23 98 45 14 6 67 33 42](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-127.jpg)
98 23 45 14 98 23 23 98 45 14 6 67 33 42 6 67 14 45 14 23 45 98 Merge 33 42
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-128.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 67 6 Merge 33 42
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-129.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 67 Merge 33 42
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-130.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-131.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 42 6 67 Merge
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-132.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42 33 Merge
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-133.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42 33 42 Merge
![98 23 45 14 98 23 23 98 45 14 14 45 6 67 98 23 45 14 98 23 23 98 45 14 14 45 6 67](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-134.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 6 67 33 42 6 33 67 6 67 42 33 42 14 23 45 98 Merge
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-135.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42 33 42 6 Merge
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-136.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42 33 42 6 33 Merge
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-137.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42 33 42 6 33 42 Merge
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-138.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42 33 42 67 Merge
![98 23 45 14 6 67 33 42 98 23 45 14 98 23 98 23 45 14 6 67 33 42 98 23 45 14 98 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-139.jpg)
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 Merge 42
![98 23 45 14 6 67 33 42 98 23 45 14 98 23 98 23 45 14 6 67 33 42 98 23 45 14 98 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-140.jpg)
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 Merge 42
![98 23 45 14 6 67 33 42 98 23 45 14 98 23 98 23 45 14 6 67 33 42 98 23 45 14 98 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-141.jpg)
98 23 45 14 6 67 33 42 98 23 45 14 98 23 6 67 33 42 45 14 45 23 98 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 14 Merge 42
![98 23 45 14 6 67 33 42 98 23 45 14 98 23 98 23 45 14 6 67 33 42 98 23 45 14 98 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-142.jpg)
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 14 23 Merge 42
![98 23 45 14 6 67 33 42 98 23 45 14 98 23 98 23 45 14 6 67 33 42 98 23 45 14 98 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-143.jpg)
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 14 23 33 Merge 42
![98 23 45 14 6 67 33 42 98 23 45 14 98 23 98 23 45 14 6 67 33 42 98 23 45 14 98 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-144.jpg)
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 14 23 33 42 Merge 42
![98 23 45 14 6 67 33 42 98 23 45 14 98 23 98 23 45 14 6 67 33 42 98 23 45 14 98 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-145.jpg)
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 14 23 33 42 45 Merge 42
![98 23 45 14 6 67 33 42 98 23 45 14 98 23 98 23 45 14 6 67 33 42 98 23 45 14 98 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-146.jpg)
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 14 23 33 42 45 67 Merge 42
![98 23 45 14 6 67 33 42 98 23 45 14 98 23 98 23 45 14 6 67 33 42 98 23 45 14 98 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-147.jpg)
98 23 45 14 6 67 33 42 98 23 45 14 98 23 23 98 6 67 33 42 45 14 6 67 33 42 6 33 14 45 67 6 67 33 42 67 14 23 45 98 6 14 23 33 42 45 67 98 Merge 42
![98 23 45 14 98 23 23 98 45 14 14 45 14 23 98 23 45 14 98 23 23 98 45 14 14 45 14 23](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-148.jpg)
98 23 45 14 98 23 23 98 45 14 14 45 14 23 45 98 6 67 33 42 6 33 67 6 67 42 33 42 67 6 14 23 33 42 45 67 98
![98 23 45 14 6 67 33 42 6 14 23 33 42 45 98 23 45 14 6 67 33 42 6 14 23 33 42 45](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-149.jpg)
98 23 45 14 6 67 33 42 6 14 23 33 42 45 67 98
![Summary • Divide the unsorted collection into two • Until the sub-arrays only contain Summary • Divide the unsorted collection into two • Until the sub-arrays only contain](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-150.jpg)
Summary • Divide the unsorted collection into two • Until the sub-arrays only contain one element • Then merge the sub-problem solutions together
![Questions? Questions?](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-151.jpg)
Questions?
![Review? Review?](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-152.jpg)
Review?
![](http://slidetodoc.com/presentation_image_h/ec20a281ebc695c5ba0c5e38225cf620/image-153.jpg)
- Slides: 153