def merge_sort(unsorted_list):
         if len(unsorted_list) <= 1:
            return unsorted_list
      # Find the middle point and devide it
         middle = len(unsorted_list) // 2
         left_list = unsorted_list[:middle]
         right_list = unsorted_list[middle:]
         left_list = merge_sort(left_list)
         right_list = merge_sort(right_list)
         return list(merge(left_list, right_list))
      # Merge the sorted halves
      def merge(left_half,right_half):
         res = []
         while len(left_half) != 0 and len(right_half) != 0:
            if left_half[0] < right_half[0]:
               res.append(left_half[0])
               left_half.remove(left_half[0])
            else:
               res.append(right_half[0])
               right_half.remove(right_half[0])
         if len(left_half) == 0:
            res = res + right_half
         else:
            res = res + left_half
         return res
      arr= [45,89,56,78,90,12,10,2,34,1]
      print(merge_sort(arr))
      

      Python language logoMerge Sort

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.