Explore Courses
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Birla Institute of Management Technology Birla Institute of Management Technology Post Graduate Diploma in Management (BIMTECH)
  • 24 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Popular
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science & AI (Executive)
  • 12 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
University of MarylandIIIT BangalorePost Graduate Certificate in Data Science & AI (Executive)
  • 8-8.5 Months
upGradupGradData Science Bootcamp with AI
  • 6 months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
OP Jindal Global UniversityOP Jindal Global UniversityMaster of Design in User Experience Design
  • 12 Months
Popular
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Rushford, GenevaRushford Business SchoolDBA Doctorate in Technology (Computer Science)
  • 36 Months
IIIT BangaloreIIIT BangaloreCloud Computing and DevOps Program (Executive)
  • 8 Months
New
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Popular
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
Golden Gate University Golden Gate University Doctor of Business Administration in Digital Leadership
  • 36 Months
New
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
Popular
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
Bestseller
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
IIIT BangaloreIIIT BangalorePost Graduate Certificate in Machine Learning & Deep Learning (Executive)
  • 8 Months
Bestseller
Jindal Global UniversityJindal Global UniversityMaster of Design in User Experience
  • 12 Months
New
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in AI and Emerging Technologies (Blended Learning Program)
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
ESGCI, ParisESGCI, ParisDoctorate of Business Administration (DBA) from ESGCI, Paris
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration From Golden Gate University, San Francisco
  • 36 Months
Rushford Business SchoolRushford Business SchoolDoctor of Business Administration from Rushford Business School, Switzerland)
  • 36 Months
Edgewood CollegeEdgewood CollegeDoctorate of Business Administration from Edgewood College
  • 24 Months
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with Concentration in Generative AI
  • 36 Months
Golden Gate University Golden Gate University DBA in Digital Leadership from Golden Gate University, San Francisco
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA by Liverpool Business School
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA (Master of Business Administration)
  • 15 Months
Popular
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Business Administration (MBA)
  • 12 Months
New
Deakin Business School and Institute of Management Technology, GhaziabadDeakin Business School and IMT, GhaziabadMBA (Master of Business Administration)
  • 12 Months
Liverpool John Moores UniversityLiverpool John Moores UniversityMS in Data Science
  • 18 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityMaster of Science in Artificial Intelligence and Data Science
  • 12 Months
Bestseller
IIIT BangaloreIIIT BangalorePost Graduate Programme in Data Science (Executive)
  • 12 Months
Bestseller
O.P.Jindal Global UniversityO.P.Jindal Global UniversityO.P.Jindal Global University
  • 12 Months
WoolfWoolfMaster of Science in Computer Science
  • 18 Months
New
Liverpool John Moores University Liverpool John Moores University MS in Machine Learning & AI
  • 18 Months
Popular
Golden Gate UniversityGolden Gate UniversityDBA in Emerging Technologies with concentration in Generative AI
  • 3 Years
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (AI/ML)
  • 36 Months
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDBA Specialisation in AI & ML
  • 36 Months
Golden Gate University Golden Gate University Doctor of Business Administration (DBA)
  • 36 Months
Bestseller
Ecole Supérieure de Gestion et Commerce International ParisEcole Supérieure de Gestion et Commerce International ParisDoctorate of Business Administration (DBA)
  • 36 Months
Rushford, GenevaRushford Business SchoolDoctorate of Business Administration (DBA)
  • 36 Months
Liverpool Business SchoolLiverpool Business SchoolMBA with Marketing Concentration
  • 18 Months
Bestseller
Golden Gate UniversityGolden Gate UniversityMBA with Marketing Concentration
  • 15 Months
Popular
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Corporate & Financial Law
  • 12 Months
Bestseller
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Intellectual Property & Technology Law
  • 12 Months
Jindal Global Law SchoolJindal Global Law SchoolLL.M. in Dispute Resolution
  • 12 Months
IIITBIIITBExecutive Program in Generative AI for Leaders
  • 4 Months
New
IIIT BangaloreIIIT BangaloreExecutive Post Graduate Programme in Machine Learning & AI
  • 13 Months
Bestseller
upGradupGradData Science Bootcamp with AI
  • 6 Months
New
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
KnowledgeHut upGradKnowledgeHut upGradSAFe® 6.0 Certified ScrumMaster (SSM) Training
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutCertified ScrumMaster®(CSM) Training
  • 16 Hours
upGrad KnowledgeHutupGrad KnowledgeHutLeading SAFe® 6.0 Certification
  • 16 Hours
KnowledgeHut upGradKnowledgeHut upGradPMP® certification
  • Self-Paced
upGrad KnowledgeHutupGrad KnowledgeHutAWS Solutions Architect Certification
  • 32 Hours
upGrad KnowledgeHutupGrad KnowledgeHutAzure Administrator Certification (AZ-104)
  • 24 Hours
KnowledgeHut upGradKnowledgeHut upGradAWS Cloud Practioner Essentials Certification
  • 1 Week
KnowledgeHut upGradKnowledgeHut upGradAzure Data Engineering Training (DP-203)
  • 1 Week
MICAMICAAdvanced Certificate in Digital Marketing and Communication
  • 6 Months
Bestseller
MICAMICAAdvanced Certificate in Brand Communication Management
  • 5 Months
Popular
IIM KozhikodeIIM KozhikodeProfessional Certification in HR Management and Analytics
  • 6 Months
Bestseller
Duke CEDuke CEPost Graduate Certificate in Product Management
  • 4-8 Months
Bestseller
Loyola Institute of Business Administration (LIBA)Loyola Institute of Business Administration (LIBA)Executive PG Programme in Human Resource Management
  • 11 Months
Popular
Goa Institute of ManagementGoa Institute of ManagementExecutive PG Program in Healthcare Management
  • 11 Months
IMT GhaziabadIMT GhaziabadAdvanced General Management Program
  • 11 Months
Golden Gate UniversityGolden Gate UniversityProfessional Certificate in Global Business Management
  • 6-8 Months
upGradupGradContract Law Certificate Program
  • Self paced
New
IU, GermanyIU, GermanyMaster of Business Administration (90 ECTS)
  • 18 Months
Bestseller
IU, GermanyIU, GermanyMaster in International Management (120 ECTS)
  • 24 Months
Popular
IU, GermanyIU, GermanyB.Sc. Computer Science (180 ECTS)
  • 36 Months
Clark UniversityClark UniversityMaster of Business Administration
  • 23 Months
New
Golden Gate UniversityGolden Gate UniversityMaster of Business Administration
  • 20 Months
Clark University, USClark University, USMS in Project Management
  • 20 Months
New
Edgewood CollegeEdgewood CollegeMaster of Business Administration
  • 23 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
The American Business SchoolThe American Business SchoolMBA with specialization
  • 23 Months
New
Aivancity ParisAivancity ParisMSc Artificial Intelligence Engineering
  • 24 Months
Aivancity ParisAivancity ParisMSc Data Engineering
  • 24 Months
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGrad KnowledgeHutupGrad KnowledgeHutData Engineer Bootcamp
  • Self-Paced
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
upGradupGradCloud Computing Bootcamp
  • 7.5 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 5 Months
upGrad KnowledgeHutupGrad KnowledgeHutSAFe® 6.0 POPM Certification
  • 16 Hours
upGradupGradDigital Marketing Accelerator Program
  • 05 Months
upGradupGradAdvanced Certificate Program in GenerativeAI
  • 4 Months
New
upGradupGradData Science Bootcamp with AI
  • 6 Months
Popular
upGradupGradFull Stack Software Development Bootcamp
  • 6 Months
Bestseller
upGradupGradUI/UX Bootcamp
  • 3 Months
PwCupGrad CampusCertification Program in Financial Modelling & Analysis in association with PwC India
  • 4 Months
upGradupGradCertificate Course in Business Analytics & Consulting in association with PwC India
  • 06 Months
upGradupGradDigital Marketing Accelerator Program
  • 05 Months

Python Program for Merge Sort

Updated on 20 December, 2023

6.85K+ views
8 min read

As a multi-paradigm programming language with a structured, object-oriented design approach and simple and uncluttered syntax and grammar, Python is rapidly emerging as the language of choice for programmers working on projects of varying complexity and scale.

Python provides a modular library of pre-built algorithms that allows its users to perform various operations that may help them achieve their task in and of themselves or serve as a step along the way to achieving a larger, more complex goal. One of the more popular such algorithms is one that enables the Merge Sort functionality.

What is Merge Sort?

It is a general-purpose sorting technique that enables users to take a random dataset of any type and from any source and divides it into repetitive stages until eventually it is broken down into its individual components – a recursive technique, commonly referred to as the ‘divide and conquer’ method.

The algorithm then puts together the individual components – again in repetitive stages – but sorts them into a pre-decided, logical sequence at each stage along the way, using the basic comparison and swap until the entire data series is reconstituted in the desired logical sequence. 

Check out our other data science courses at upGrad. 

Divide and Conquer technique

Take, for instance, a random dataset of letters of the alphabet: N, H, V, B, Q, D, Z, R.

Step 1: The original dataset first gets broken down into two groups as follows: 

N, H, V, B Q, D, Z, R

Step 2: Both the resulting arrays get further sub-divided as follows:

N, H V, B Q, D Z, R

Step 3: Finally, all four arrays are further spit-up until the entire data series gets broken down into its individual components:

N H V B Q D Z R

The process then reverses, and the individual data points now begin to merge in a stage-wise manner. But over the course of this merging process, each element in each sub-array gets assesses and swapped so that they sort themselves out in a logical sequence (alphabetical order), as follows:

Step 4: Individual elements merge into pairs while swapping positions as required to form the correct sequence:

H, N B, V D, Q R, Z

Step 5: The recursive process of merging and sorting continues to the next iteration:

B, H, N, V D, Q, R, Z

Step 6: The entire data series is finally reconstituted in its logical alphabetical order:

B, D, H, N, Q, R, V, Z

Merge Sort Implementations

There are two approaches to Merge Sort implementation in Python. The top-down approach and the bottom-up approach.

Top-down Approach:

The more commonly used top-down approach is the one described above. It takes longer and uses up more memory, and is therefore inefficient when working with smaller datasets. However, it is far more reliable, particularly when applied to large datasets. 

Input code:

def merge_sort (inp_arr):
size = len(inp_arr)
if size > 1:
middle = size // 2
left_arr = inp_arr(:middle)
rIght_arr = inp_arr(middle:)
merge_sort(left_arr)
merge _sort(right_arr)
i = 0
j = 0
k = 0

(Where i and j are the iterators for traversing the left and right halves of the data series, respectively, and k is the iterator of the overall data series).

left_size = len(left_arr)
right _size = len(right_arr)
while i < left_size and j < right size:
if left_arr(i) < right_arr (j):
inp_arr(k) - left_arr(i)
i >= 1
else:
inp_arr(k) = right_arr (j)
j += 1
k += 1
while i < left_size:
inp_arr (k) = left_arr(i)
i += 1
k += 1
while j < right_size:
inp_arr (k) = right_arr(j)
j += 1
k += 1
inp_arr = (N, H, V, B, Q, D, Z, R)
print(:Input Array:\n”)
print(inp_arr)
merge_sort (inp_arr)
print(“Sorted Array:\n”)
print (inp_arr)

Output:

Input Array: N, H, V, B, Q, D, Z, R

Output Array: B, D, H, N, Q, R, V, Z

Bottom-up approach:

The bottom-up approach is quicker, uses up less memory, and works efficiently with smaller datasets but may run into problems when working with large data sets. It is therefore less-frequently used.

def merge(left, right):
result = [] x, y = 0, 0
for k in range(0, len(left) + len(right)):
if i == len(left): # if at the end of 1st half,
result.append(right[j]) # add all values of 2nd half
j += 1
elif j == len(right): # if at the end of 2nd half,
result.append(left[x]) # add all values of 1st half
i += 1
elif right[j] < left[i]:
result.append(right[j])
j += 1
else:
result.append(left[i])
i += 1
return result
def mergesort(ar_list):
length = len(ar_list)
size = 1
while size < length:
size+=size # initializes at 2 as described
for pos in range(0, length, size):
start = pos
mid  = pos + int(size / 2)
end = pos + size
left = ar_list[ start : mid ] right = ar_list[ mid : end ] 
ar_list[start:end] = merge(left, right)
return ar_list
ar_list = [N, H, V, B, Q, D, Z, R] print(mergesort(ar_list))

Output:

Input array: N, H, V, B, Q, D, Z, R

Output array: B, D, H, N, Q, R, V, Z

Merge Sort implementation applied to more complex, real-life datasets

Let’s apply the top-down approach to four random off-road vehicles in India:

Brand

Model

Ex-showroom price in Rs Crore

Jeep Wrangler 0.58
Ford  Endeavour  0.35
Jaguar Land Rover Range Rover Sport 2.42
Mercedes Benz G-class 1.76

Input code:

class Car:
def __init__(self, brand, model, price):
self.brand = brand
self.model = model
self.price = price
def __str__(self):
return str.format(“Brand: {}, Model: {}, Price: {}”, self.brand,
self.model, self.price)
def merge(list1, i, j, k, comp_fun):
left_copy = list1[i:k + 1]
r_sublist = list1[k+1:r+1]
left_copy_index = 0
j_sublist_index = 0
sorted_index = i
while left_copy_index < len(left_copy) and j_sublist_index <
len(j_sublist):
if comp_fun(left_copy[left_copy_index], j_sublist[j_sublist_index]):
list1[sorted_index] = left_copy[left_copy_index]
left_copy_index = left_copy_index + 1
else:
list1[sorted_index] = j_sublist[j_sublist_index]
j_sublist_index = j_sublist_index + 1
sorted_index = sorted_index + 1
while left_copy_index < len(left_copy):
list1[sorted_index] = left_copy[left_copy_index]
left_copy_index = left_copy_index + 1
sorted_index = sorted_index + 1
while j_sublist_index < len(j_sublist):
list1[sorted_index] = j_sublist[j_sublist_index]
j_sublist_index = j_sublist_index + 1
sorted_index = sorted_index + 1
def merge_sort(list1, i, j, comp_fun):
if i >= j:
return
k = (i + j)//2
merge_sort(list1, i, k, comp_fun)
merge_sort(list1, k + 1, j, comp_fun)
merge(list1,i, j, k, comp_fun)
car1 = Car(“Jeep”, “Wrangler”, 0.58)
car2 = Car(“Ford”, “Endeavour”, 0.35)
car3 = Car(“Jaguar Land Rover”, “Range Rover Sport”, 1.76)
car4 = Car(“Mercedes Benz”, “G-class”, 2.42)
list1 = [car1, car2, car3, car4]
merge_sort(list1, 0, len(list1) -1, lambda carA, carB: carA.brand < carB.brand)
print(“Cars sorted by brand:”)
for car in list1:
print(car)
print()
merge_sort(list1, 0, len(list1) -1, lambda carA, carB: carA.price< carB.price)
print(“Cars sorted by price:”)
for car in list1:
print(car)

Output:

Cars sorted by brand:

Ford Endeavour

Jaguar Land Rover Range Rover Sport

Jeep Wrangler

Mercedez Benz G-class

Cars sorted by price: 

Ford Endeavour

Jeep Wrangler

Jaguar Land Rover Range Rover

Mercedez Benz G-class

Understanding the Difference Between Insertion Sort and Merge Sort in Python

Insertion sort and merge sort in Python are often considered to be the same thing. But keep scrolling to understand the difference between the two algorithms.

  • Datasets: Merge sort is useful for large data sets. It can help compare the different elements inside an array. Therefore, it is not for small datasets. The insertion sort is more suitable when there are a limited number of elements. Insertion sort skips all the sorted values. Therefore, it works faster while dealing with already sorted or nearly sorted data. 
  • Stability: Merge sort is considered stable because of the presence of two elements with equal value. The values appear in the sorted output, similar to what they were in the unsorted input array. Insertion sort requires O(N2) time on arrays as well as linked lists. If the CPU comes with an efficient memory block move function, the array will be a lot faster. Otherwise, you wouldn’t find much of a time difference. 
  • Sorting Method: In the merge sort algorithm, the sorted data cannot be included within the memory. It requires auxiliary memory for sorting. Insertion sort stems from the idea that one element from the input elements gets consumed in every iteration to find the correct position. The correct position describes its place in a sorted array. 
  • Efficiency: If you compare the time complexity of the two algorithms, merge sort will prove to be better when it comes to time. But in terms of space, the insertion sort algorithm has an edge. 

An Example of the Insertion Sort Algorithm

#include<stdio.h>
void insertionSort(int arr[], int n) {
   int i, key, j;
   for (i = 1; i < n; i++) {
       key = arr[i];
       j = i - 1;
       while (j >= 0 && arr[j] > key) {
           arr[j + 1] = arr[j];
           j = j - 1;
       }
       arr[j + 1] = key;
   }
}
void printArray(int arr[], int n) {
   int i;
   for (i = 0; i < n; i++)
       printf("%d ", arr[i]);
   printf("\n");
}
int main() {
   int arr[] = {12, 11, 13, 5, 6};
   int n = sizeof(arr)/sizeof(arr[0]);
   printf("Given array is \n");
   printArray(arr, n);
   insertionSort(arr, n);
   printf("\nSorted array is \n");
   printArray(arr, n);
   return 0;
}

Output:

Given array is 

12 11 13 5 6 

Sorted array is 

5 6 11 12 13 

What is merge sort in Python using recursion?

The concept of merge sort in Python using recursion helps to sort the numbers on a big array. While using the recursion method, merge sort gets applied on the smaller arrays twice. It leads to the calling of the merge sort algorithm a total of four times. 

We keep passing on the problem but need to deliver a specific thing at a particular point. The stopping point where you deliver something is when you are required to sort an array with a single number. It is something that’s completely in your control while using the recursion method. 

What is the space and time complexity of the merge sort algorithm in Python?

The merge sort algorithm in Python has a time complexity of 0(nLogn) in the best, worst, as well as average cases. The time complexity can always create two halves from the array. The halves are then merged in linear time. For the space complexity, an extra array needs to be sorted to contain the resultant sorted array. Therefore, the space-time complexity can be defined as 0(n). 

You can learn both theoretical and practical aspects of Python with upGrad’s Professional Degree in Data Science. This course helps you learn Python from scratch. Even if you are new to programming and coding, upGrad will offer you a two-week preparatory course so that you can pick up on the basics of programming. you will learn about various tools like Python, SQL,, while working on multiple industry projects.

RELATED PROGRAMS