Table of Contents
Merge Sort
Python program for Merge sort, this will help student and beginners to understand the working of merge sort. We request you yo read comments.
You can also check following sort
""" Author : ITVoyagers (itvoyagers.in) Date :27th October 2019 Description : Program to merge sorted lists in python """ def mergeSortedLists( collection1, collection2 ) : #Create the new list and initialize the list markers. new_list = list() a = 0 b = 0 # Merge the two lists together until one is empty. while a < len( collection1 ) and b < len( collection2 ) : if collection1[a] < collection2[b] : new_list.append( collection1[a] ) a += 1 else : new_list.append( collection2[b] ) b += 1 # If collection1 contains more items, append them to new_list. while a < len( collection1 ) : new_list.append( collection1[a] ) a += 1 # Or if collection2 contains more items, append them to new_list. while b < len( collection2 ) : new_list.append( collection2[b] ) b += 1 return new_list a=mergeSortedLists([1,2,3],[8,9,10]) print (a)
Output :
[1, 2, 3, 8, 9, 10]
Note : Please note that above program is compatible with Python 3 or higher version.
Please check out other practical related to Data structures
2 thoughts on “Python program for Merge sort”