|
| 1 | +""" |
| 2 | +Merge Sort |
| 3 | + We have 2 unsorted arrays, we've to sort them and merge them in a way that merged array is also a sorted array |
| 4 | + Traditional Method |
| 5 | + Compare first element of each array |
| 6 | + Insert the smallest element into sorted array |
| 7 | + Compare greater element from previous array and 2nd element from 2nd array |
| 8 | + Insert the smallest element into sorted array |
| 9 | + Repeat until array is sorted |
| 10 | +
|
| 11 | +
|
| 12 | + 9 - 2 - 1 - 7 - 5 - 3 - 4 - 6 |
| 13 | + 9 - 2 - 1 - 7 5 - 3 - 4 - 6 |
| 14 | + 9 - 2 1 - 7 5 - 3 4 - 6 |
| 15 | + 2 - 9 1 - 7 3 - 5 4 - 6 |
| 16 | + 1 - 2 - 7 - 9 3 - 4 - 5 - 6 |
| 17 | + 1 - 2 - 7 - 9 3 - 4 - 5 - 6 |
| 18 | + 1 - 2 - 3 - 4 - 5 - 6 - 7 - 9 |
| 19 | +
|
| 20 | +
|
| 21 | +Time Complexity: O(n log n) |
| 22 | +
|
| 23 | +
|
| 24 | +""" |
| 25 | + |
| 26 | +def Merge2SortedList(arr1, arr2): |
| 27 | + """Input: 2 sorted lists""" |
| 28 | + sortedList = [] # created an empty sorted list |
| 29 | + len_arr1 = len(arr1) # length of array1 |
| 30 | + len_arr2 = len(arr2) # length of array2 |
| 31 | + i = j = 0 # pointers initialised |
| 32 | + # iterate through both lists until end of any of them is reached |
| 33 | + while i < len_arr1 and j < len_arr2: |
| 34 | + if arr1[i] <= arr2[j]: # compare elements between both the lists |
| 35 | + sortedList.append(arr1[i]) # append to sorted list |
| 36 | + i+=1 # i pointer is increased by 1 and j pointer is static |
| 37 | + else: |
| 38 | + sortedList.append(arr2[j]) |
| 39 | + j+=1 |
| 40 | + while i < len_arr1: |
| 41 | + sortedList.append(arr1[i]) |
| 42 | + i+=1 |
| 43 | + while j < len_arr2: |
| 44 | + sortedList.append(arr2[j]) |
| 45 | + j += 1 |
| 46 | + return sortedList # sorted list returned |
| 47 | + |
| 48 | + |
| 49 | +def MergeSort(arr): |
| 50 | + # if list is empty or contains 1 element only: return list |
| 51 | + if len(arr) <= 1: |
| 52 | + return arr |
| 53 | + mid = len(arr) // 2 # get middle index of array to divide it into 2 parts: left and right |
| 54 | + left = arr[:mid] # dividing list into 2 parts: left and right |
| 55 | + right = arr[mid:] # dividing list into 2 parts: left and right |
| 56 | + |
| 57 | + left = MergeSort(left) # recursively call MergeSort function to sort left array |
| 58 | + right = MergeSort(right) # recursively call MergeSort function to sort right array |
| 59 | + # MergeSort(left) # recursively call MergeSort function to sort left array |
| 60 | + # MergeSort(right) # recursively call MergeSort function to sort right array |
| 61 | + |
| 62 | + return Merge2SortedList(left, right) # Merge Sort both sorted array |
| 63 | + # Merge2SortedList(left, right) # Merge Sort both sorted array |
| 64 | + |
| 65 | + |
| 66 | +if __name__ == '__main__': |
| 67 | + |
| 68 | + """arr1 = [2, 4, 6, 8] |
| 69 | + arr2 = [3, 6, 9, 12] |
| 70 | + print(Merge2SortedList(arr1, arr2))""" |
| 71 | + arr = [10, 3, 15, 7, 8, 23, 98, 29] |
| 72 | + print(MergeSort(arr)) |
| 73 | + |
| 74 | + # test cases |
| 75 | + |
| 76 | + tests = [ |
| 77 | + [20, 13, 5, 37, 8, 32, 98, 29], # unsorted array |
| 78 | + [], # empty array |
| 79 | + [5], # array with single element |
| 80 | + [10, 8, 6, 4, 2], # reverse array |
| 81 | + [1, 2, 3, 4, 5] # sorted array |
| 82 | + ] |
| 83 | + for elements in tests: |
| 84 | + print(MergeSort(elements)) |
0 commit comments