Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | None | 0 0
  1. def merge(array1, array2):
  2.     i = 0
  3.     j = 0
  4.     result = []
  5.     while i < len(array1) and j < len(array2):
  6.         if array1[i] <= array2[j]:
  7.             result.append(array1[i])
  8.             i += 1
  9.         else:
  10.             result.append(array2[j])
  11.             j += 1 
  12.     return result + array1[i:] + array2[j:]
  13.  
  14. def merge_sort(array):
  15.     if len(array) == 1:
  16.         return array
  17.     else:
  18.         l = array[:len(array)//2]
  19.         r = array[len(array)//2:]
  20.         return merge(merge_sort(l), merge_sort(r))
  21.  
  22. print(merge_sort([9, 8, 1, 2]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement