MergeSort: Divide and Conquer Routine for Sorting in Python

In Algorithmic thinking, Divide and Conquer Paradigm is widely applicable to problem solving. As the name says, Divide and Conquer approach works by dividing a large problem recursively into small tractable sub-problems which can be solved with ease and then the solutions are then combined to the actual problem at hand. Enough verbosity, in this post we will explain this approach by working on Sorting Problem using the popular MergeSort Algorithm. We will also get our hands dirty by coding this up in our favorite programming language, Python.

The Sorting Problem:

In algorithmic thinking, we recommend to think in terms of input and output. For the sorting problem, input is array of unsorted numbers, [45, 23, 109,  41,  21, 12] for example and the output is the same array in sorted order [12, 21, 23, 41, 45, 109], order being the numbers in increasing order.

Though there are many algorithms to solve this efficiently (time and space complexity), We will solve this problem using MergeSort Algorithm.

MergeSort and Divide and Conquer:

The three main routines of MergeSort Algorithm are:

  1. Divide the input array into two sub-arrays [Divide]
  2. Recursively sort the left sub-array and the right sub-array [Conquer]
  3. Merge two sub-arrays into one array [Solution]

Refer to the following figure to see the approach in action:

Python Code for MergeSort:

The mergesort function in the code below takes array as input. If the array size is 1 (base case), it returns the array. Else it recursively calls mergesort() function on the left sub-array and right sub-array.  The array is sub-divided till its size reduces to 1 ( the base case ) and then only the merge() sub-routine kicks in. The merge() sub-routine merges the two sub-arrays into a single array.

Conclusion:

Divide and Conquer Approach is powerful method for problem solving. It works when the sub-problems have the same structure. In MergeSort, the sub-problems are the same type as the original problem, sorting problem. There are other techniques like Greedy Algorithms, Dynamic Programming for solving problems.

Get this running today and see output for yourself.

Tags: Merge Sort in Python, Sorting algorithm in python, ds programs in python, python data structure programs, merge sort program, latest interview ds questions,

Add Comment

%d bloggers like this: