Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python Program for Iterative Merge Sort

  • Last Updated : 30 Dec, 2020

Following is a typical recursive implementation of Merge Sort that uses last element as pivot.


# Recursive Python Program for merge sort
def merge(left, right):
    if not len(left) or not len(right):
        return left or right
    result = []
    i, j = 0, 0
    while (len(result) < len(left) + len(right)):
        if left[i] < right[j]:
            i+= 1
            j+= 1
        if i == len(left) or j == len(right):
            result.extend(left[i:] or right[j:])
    return result
def mergesort(list):
    if len(list) < 2:
        return list
    middle = len(list)/2
    left = mergesort(list[:middle])
    right = mergesort(list[middle:])
    return merge(left, right)
seq = [12, 11, 13, 5, 6, 7]
print("Given array is")
print("Sorted array is")
# Code Contributed by Mohit Gupta_OMG 

Please refer complete article on Iterative Merge Sort for more details!

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!