Advertisement
karlakmkj

Merge sort sample

Jan 7th, 2021
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.47 KB | None | 0 0
  1. '''
  2. This version prints indicators of its progress as it runs. To make this work, temporary variables are created for unsorted lists so they can be printed.
  3. '''
  4. def mergesort(lst):
  5.    
  6.     #Then, what does it do? mergesort should recursively run mergesort on the left and right sides of lst until it's given a list of only one item. So, if lst has only one item, we should just return that one-item list.
  7.    
  8.     if len(lst) <= 1:
  9.         print("Returning one-item list:", lst)
  10.         return lst
  11.    
  12.     #Otherwise, we should call mergesort separately on the left and right sides. Since each successive call to mergesort sends half as many items, we're guaranteed to eventually send it a list with only one item, at which point we'll stop calling mergesort again.
  13.     else:
  14.         #Floor division on the length of the list will find us the index of the middle value.
  15.         midpoint = len(lst) // 2
  16.  
  17.         #lst[:midpoint] will get the left side of the list based on list slicing syntax. So, we want to sort the left side of the list alone and assign the result to the new smaller list left.      
  18.         unsortedleft = lst[:midpoint]
  19.         unsortedright = lst[midpoint:]
  20.        
  21.         print("Sorting left:", unsortedleft)
  22.         left = mergesort(lst[:midpoint])
  23.         print("Left sorted:", left)
  24.  
  25.         #And same for the right side.
  26.         print("Sorting right:", unsortedright)
  27.         right = mergesort(lst[midpoint:])
  28.         print("Right sorted:", right)
  29.  
  30. '''
  31. So, left and right now hold sorted lists of each half of the original list. They might each have only one item, or they could each have several items. Now we want to compare the first items in each list one-by-one, adding the smaller to our new result list until one list is completely empty.
  32. '''
  33.         print("Merging", left, "and", right)
  34.         newlist = []
  35.         while len(left) and len(right) > 0:
  36.  
  37.             #If the first number in left is lower, add it to the new list and remove it from left
  38.             if left[0] < right[0]:
  39.                 newlist.append(left[0])
  40.                 del left[0]
  41.  
  42.             #Otherwise, add the first number from right to the new list and remove it from right
  43.             else:
  44.                 newlist.append(right[0])
  45.                 del right[0]
  46.  
  47.         #When the while loop above is done, it means one of the two lists is empty. Because both lists were sorted, we can now add the remainder of each list to the new list. The empty list will have no items to add, and the non-empty list will add its items in order.
  48.  
  49.         newlist.extend(left)
  50.         newlist.extend(right)
  51.  
  52.         #newlist is now the sorted version of lst! So, we can return it. If this was a recursive call to mergesort, then this sends a sorted half- list up the ladder. If this was the original call, then this is the final sorted list.
  53.  
  54.         print("Returning merged list:", newlist)
  55.         return newlist
  56.  
  57. print(mergesort([2, 5, 3, 8, 6, 9, 1, 4, 7]))
  58.  
  59. #Total iterations = 8 (see Returning merged list)
  60. '''
  61. Output will show:
  62. Sorting left: [2, 5, 3, 8]
  63. Sorting left: [2, 5]
  64. Sorting left: [2]
  65. Returning one-item list: [2]
  66. Left sorted: [2]
  67. Sorting right: [5]
  68. Returning one-item list: [5]
  69. Right sorted: [5]
  70. Merging [2] and [5]
  71. Returning merged list: [2, 5]
  72. Left sorted: [2, 5]
  73. Sorting right: [3, 8]
  74. Sorting left: [3]
  75. Returning one-item list: [3]
  76. Left sorted: [3]
  77. Sorting right: [8]
  78. Returning one-item list: [8]
  79. Right sorted: [8]
  80. Merging [3] and [8]
  81. Returning merged list: [3, 8]
  82. Right sorted: [3, 8]
  83. Merging [2, 5] and [3, 8]
  84. Returning merged list: [2, 3, 5, 8]
  85. Left sorted: [2, 3, 5, 8]
  86. Sorting right: [6, 9, 1, 4, 7]
  87. Sorting left: [6, 9]
  88. Sorting left: [6]
  89. Returning one-item list: [6]
  90. Left sorted: [6]
  91. Sorting right: [9]
  92. Returning one-item list: [9]
  93. Right sorted: [9]
  94. Merging [6] and [9]
  95. Returning merged list: [6, 9]
  96. Left sorted: [6, 9]
  97. Sorting right: [1, 4, 7]
  98. Sorting left: [1]
  99. Returning one-item list: [1]
  100. Left sorted: [1]
  101. Sorting right: [4, 7]
  102. Sorting left: [4]
  103. Returning one-item list: [4]
  104. Left sorted: [4]
  105. Sorting right: [7]
  106. Returning one-item list: [7]
  107. Right sorted: [7]
  108. Merging [4] and [7]
  109. Returning merged list: [4, 7]
  110. Right sorted: [4, 7]
  111. Merging [1] and [4, 7]
  112. Returning merged list: [1, 4, 7]
  113. Right sorted: [1, 4, 7]
  114. Merging [6, 9] and [1, 4, 7]
  115. Returning merged list: [1, 4, 6, 7, 9]
  116. Right sorted: [1, 4, 6, 7, 9]
  117. Merging [2, 3, 5, 8] and [1, 4, 6, 7, 9]
  118. Returning merged list: [1, 2, 3, 4, 5, 6, 7, 8, 9]
  119. [1, 2, 3, 4, 5, 6, 7, 8, 9]
  120. '''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement