Pseudocode for Merge Sort in Python (Example)


In this tutorial, you’ll learn how to write a pseudocode for the merge sort algorithm using the Python programming language. Merge sort is an algorithm to sort lists of all types of objects and functions by splitting the big and hard-to-sort lists into smaller lists, then sorting each and merging the results.

The table of content is structured as follows:

Let’s get to the pseudocode!


Example Pseudocode for Merge Sort in Python

The code snippet below is a pseudocode, “a notation resembling a simplified programming language, used in program design”, as stated in Oxford Languages, so it is not a Python code that can be executed in a Python interpreter.

def merge_sort(my_list):
    if len(my_list) < 2: # return if size is less than 2
    mid = len(my_list)//2 # setting the middle index of the list
    left_lst = "Elements with indices less than mid"
    right_lst = "Elements with indices higher than mid"
    merge_sort(left_lst) # sorting the left sublist
    merge_sort(right_lst) # sorting the right sublist
    i = 0
    j = 0
    x = 0
    while i < len(left_lst) and j < len(right_lst):
        if left_lst[i] < right_lst[j]:
            my_list[x] = left_lst[i] # insert left list element
            i = i + 1
            my_list[x] = right_lst[i] # insert right list element
            j = j + 1
        x += 1
    "Fill rest of my_list with the remaining elements of either left_lst or right_lst"

The pseudocode for the merge sort algorithm flow can be summarized as the following:

  • Check if the list has 2 or more elements; if not, return control to the parent execution.
  • Separate the list into two sublists and call the function for both.
  • Once the function calls are executed, sort the two sublists.
  • Finally, merge the two sorted sublists and repeat the procedure until the entire list is sorted.

And that sums up the pseudocode. I hope it was helpful to you.


Video, Further Resources & Summary

Do you need more explanations on how to implement the pseudocode of merge sort? Then you should look at the following YouTube video of the Statistics Globe YouTube channel.


The YouTube video will be added soon.


Furthermore, you could have a look at some of the other tutorials on Statistics Globe:

This post has shown how to compose a pseudocode for merge sort. In case you have further questions, you may leave a comment below.


Ömer Ekiz Informatics Expert

This page was created in collaboration with Ömer Ekiz. You may have a look at Ömer’s author page to read more about his academic background and the other articles he has written for Statistics Globe.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.