Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort...
Transcript of Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort...
![Page 1: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/1.jpg)
Merge Sort
![Page 2: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/2.jpg)
Merge Sort
• In merge sort, the idea of the algorithm is to sort smaller arrays and then combine those arrays together (merge them) in sorted order.
• Merge sort leverages something called recursion, which we’ll touch on in more detail in a future video.
In pseudocode:• Sort the left half of the array (assuming n > 1)• Sort the right half of the array (assuming n > 1)• Merge the two halves together
![Page 3: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/3.jpg)
Merge Sort
5 2 1 3 6 4
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 4: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/4.jpg)
Merge Sort
5 2 1 3 6 4
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 5: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/5.jpg)
Merge Sort
5 2 1 3 6 4
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 6: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/6.jpg)
Merge Sort
5 2 1 3 6 4
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 7: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/7.jpg)
Merge Sort
2 1 3 6 4
5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 8: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/8.jpg)
Merge Sort
2 1 3 6 4
5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 9: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/9.jpg)
Merge Sort
2 1 3 6 4
5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 10: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/10.jpg)
Merge Sort
1 3 6 4
2
5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 11: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/11.jpg)
Merge Sort
1 3 6 4
2
5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 12: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/12.jpg)
Merge Sort
3 6 4
2 1
5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 13: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/13.jpg)
Merge Sort
3 6 4
2
5 1 2
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 14: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/14.jpg)
Merge Sort
3 6 4
5 1 2
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 15: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/15.jpg)
Merge Sort
3 6 4
5 1 2
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 16: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/16.jpg)
Merge Sort
3 6 4
5 2
1
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 17: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/17.jpg)
Merge Sort
3 6 4
5
1 2
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 18: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/18.jpg)
Merge Sort
3 6 4
1 2 5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 19: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/19.jpg)
Merge Sort
3 6 4
3
1 2 5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 20: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/20.jpg)
Merge Sort
3 6 4
1 2 5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 21: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/21.jpg)
Merge Sort
6 4
3
1 2 5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 22: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/22.jpg)
Merge Sort
6 4
3
1 2 5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 23: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/23.jpg)
Merge Sort
6 4
3
1 2 5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 24: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/24.jpg)
Merge Sort
4
6
3
1 2 5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 25: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/25.jpg)
Merge Sort
4
6
3
1 2 5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 26: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/26.jpg)
Merge Sort
6 4
3
1 2 5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 27: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/27.jpg)
Merge Sort
6
3 4
1 2 5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 28: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/28.jpg)
Merge Sort
3 4 6
1 2 5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 29: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/29.jpg)
Merge Sort
3 4 6
1 2 5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 30: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/30.jpg)
Merge Sort
4 6
1 2 5 3
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 31: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/31.jpg)
Merge Sort
1 2 5 3 4 6
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 32: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/32.jpg)
Merge Sort
1 2 5 3 4 6
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 33: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/33.jpg)
Merge Sort
2 5 3 4 6
1
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 34: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/34.jpg)
Merge Sort
5 3 4 6
1 2
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 35: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/35.jpg)
Merge Sort
5 4 6
1 2 3
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 36: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/36.jpg)
Merge Sort
5 6
1 2 3 4
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 37: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/37.jpg)
Merge Sort
6
1 2 3 4 5
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 38: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/38.jpg)
Merge Sort
1 2 3 4 5 6
In pseudocode:Sort the left half of the array (assuming n > 1)Sort the right half of the array (assuming n > 1)Merge the two halves together
![Page 39: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/39.jpg)
Merge Sort
• Worst-case scenario: We have to split n elements up and then recombine them, effectively doubling the sorted subarrays as we build them up. (combining sorted 1-element arrays into 2-element arrays, combining sorted 2-element arrays into 4-element arrays…)
• Best-case scenario: The array is already perfectly sorted. But we still have to split and recombine it back together with this algorithm.
![Page 40: Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort •In merge sort, the idea of the algorithm is to sort smaller arrays and then combine](https://reader031.fdocuments.us/reader031/viewer/2022021703/5e7202bd0619503631084b62/html5/thumbnails/40.jpg)
Merge Sort
O(n log n)W(n log n)