Python Example – Write a Python program to sort a list of elements using the merge sort algorithm

(Python Example for Citizen Data Scientist & Business Analyst)


Write a Python program to sort a list of elements using the merge sort algorithm.


Note: According to Wikipedia “Merge sort (also commonly spelled mergesort) is an O (n log n) comparison-based sorting algorithm. Most implementations produce a stable sort, which means that the implementation preserves the input order of equal elements in the sorted output.”


Conceptually, a merge sort works as follows :

  • Divide the unsorted list into n sublists, each containing 1 element (a list of 1 element is considered sorted).
  • Repeatedly merge sublists to produce new sorted sublists until there is only 1 sublist remaining. This will be the sorted list.

An example of merge sort:

Merge Sort animation

Sample Solution:

Python Code:

def mergeSort(nlist):
    print("Splitting ",nlist)
    if len(nlist)>1:
        mid = len(nlist)//2
        lefthalf = nlist[:mid]
        righthalf = nlist[mid:]

        while i < len(lefthalf) and j < len(righthalf):
            if lefthalf[i] < righthalf[j]:

        while i < len(lefthalf):

        while j < len(righthalf):
    print("Merging ",nlist)

nlist = [14,46,43,27,57,41,45,21,70]

Sample Output:

Splitting  [14, 46, 43, 27, 57, 41, 45, 21, 70]                                                               
Splitting  [14, 46, 43, 27]                                                                                   
Splitting  [14, 46]                                                                                           
Splitting  [14]                                                                                               
Merging  [14]                                                                                                 
Splitting  [46]                         
Merging  [14, 21, 27, 41, 43, 45, 46, 57, 70]                                                                 
[14, 21, 27, 41, 43, 45, 46, 57, 70]


Write a Python program to sort a list of elements using the merge sort algorithm

Sign up to get end-to-end “Learn By Coding” example.

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Leave a Reply

Your email address will not be published. Required fields are marked *