Merge sort b

download Merge sort b

If you can't read please download the document

  • date post

    22-Nov-2014
  • Category

    Education

  • view

    284
  • download

    1

Embed Size (px)

description

 

Transcript of Merge sort b

  • 1. Merge sort
  • 2. Lets start with an unsorted list
  • 3. Divide the list into two lists
  • 4. Divide that list up again
  • 5. Divide that list up again
  • 6. And again...
  • 7. A single item list must be sorted
  • 8. go to the next list, a single item list must be sorted
  • 9. jj ii KK Now take that pair and merge
  • 10. jj ii KK Look down both lists taking the smallest
  • 11. jj ii KK When both lists run out you have a sorted list
  • 12. jj ii Now this pair is sorted
  • 13. Do the same with the next pair
  • 14. Each is a list of one item (so sorted)
  • 15. jj ii KK We merge again
  • 16. jj ii KK Creating a new sorted list
  • 17. Now we have two lists of pairs...
  • 18. So we merge them
  • 19. jj ii KK Look down both lists taking the smallest
  • 20. jj ii KK
  • 21. jj ii KK Look down both lists taking the smallest
  • 22. jj ii KK Look down both lists taking the smallest
  • 23. jj ii KK Look down both lists taking the smallest
  • 24. jj ii KK Look down both lists taking the smallest
  • 25. the list is sorted and you can begin the next quarter
  • 26. A single item list must be sorted
  • 27. A single item list must be sorted
  • 28. jj ii KK Merge this pair
  • 29. jj ii KK Pick the smaller item
  • 30. jj ii KK When both lists run out you have a sorted list
  • 31. Sort the next pair
  • 32. A single item list must be sorted
  • 33. A single item list must be sorted
  • 34. Merge the single item lists
  • 35. jj ii KK Merge this pair
  • 36. jj ii KK Pick the smaller item
  • 37. jj ii KK Pick the smaller item
  • 38. Now we have two lists of pairs...
  • 39. ii jj KK So we merge them
  • 40. ii jj KK Pick the smaller item
  • 41. ii jj KK Pick the smaller item
  • 42. ii jj KK Pick the smaller item
  • 43. ii jj KK Pick the smaller item
  • 44. the pairs lists are now merged
  • 45. ii jj KK Now we merge the quarters
  • 46. ii jj KK Pick the smaller item
  • 47. ii jj KK and move on
  • 48. ii jj KK Pick the smaller item
  • 49. ii jj KK and move on
  • 50. ii jj KK Pick the smaller item
  • 51. ii jj KK and move on
  • 52. ii jj KK Pick the smaller item
  • 53. ii jj KK and move on
  • 54. The first half is now sorted
  • 55. Quickly we do the other half
  • 56. Divide...
  • 57. and Divide...
  • 58. and Divide until the lists can be no smaller
  • 59. Merge pairs...
  • 60. Merge quaters
  • 61. Finally the second half is sorted
  • 62. A final merge of both halves makes the list sorted
  • 63. ii jj KK Merging lists takes as long as the list (order(N)) You do this log(N) times Making algorithm Order (N log(N))
  • 64. ii jj KK Merging lists takes as long as the list (order(N)) You do this log(N) times Making algorithm Order (N log(N)) Merging lists takes as long as the list (order(N)) You do this log(N) times Making algorithm Order (N log(N)) Pity you need extra space to merge into otherwise it would be pretty cool