Learning Algorithms Recipes: Mergesort

This is an algorithm to sort an array. It can be classified as a divide and conquer algorithm.

Input: array of elements. E.g. arr = [5, 4, 1, 2]

Output: sorted array

Time Complexity: 0(nlogn)

image.png

def merge_sort(L):
   if len(L) > 1:
       mid = len(L) // 2
       l = L[:mid]
       r = L[mid:]
       merge_sort(l)
       merge_sort(r)

      # MERGE SUB-ARRAYS Logic