Merge Sort - cdn.cs50.netcdn.cs50.net/2015/fall/sections/4/merge_sort/merge_sort.pdf · Merge Sort...

40
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Merge Sort

O(n log n)W(n log n)