Upgrade to Pro — share decks privately, control downloads, hide ads and more …

归并

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
Avatar for bin Chou bin Chou
December 18, 2024
10

 归并

Avatar for bin Chou

bin Chou

December 18, 2024

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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.