Algorithms

Below are some homework / notes about algorithms.

Merge Sort

def merge_sort(l):
    """ merge sort implementation """
    length = len(l)
    if length in [0, 1]:  # base cases
        return l
    middle = length // 2
    left, right = l[:middle], l[middle:] 
    left_sorted = merge_sort(left)
    right_sorted = merge_sort(right)
    merged = []
    i = j = 0
    while i != len(left_sorted) and j != len(right_sorted):
        if left_sorted[i] < right_sorted[j]:
            merged.append(left_sorted[i])
            i += 1
        elif left_sorted[i] > right_sorted[j]:
            merged.append(right_sorted[j])
            j += 1
        else:
            raise NotImplementedError
    if i != len(left_sorted):
        merged.extend(left_sorted[i:])
    if j != len(right_sorted):
        merged.extend(right_sorted[j:])
    return merged

vim