Eitol
6/4/2017 - 1:20 PM

Merge sort using queues made in python3

Merge sort using queues made in python3

from collections import deque


def merge_sort(v: list) -> list:
    if len(v) < 2:
        return v
    middle = int(len(v) / 2)
    left = deque(merge_sort(v[:middle]))
    right = deque(merge_sort(v[middle:]))
    out = []
    while left and right:
        out += [left.popleft()] if left[0] < right[0] else [right.popleft()]
    out += left or right
    return out


# Test 1
v_t1 = [7, 5, 9, 3, 1, 6]
print(merge_sort(v_t1))

# Test 2
v_t1 = [1, 7, 5, 9, 3, 1, 6]
print(merge_sort(v_t1))

# Test 3
v_t1 = []
print(merge_sort(v_t1))

# Test 4
v_t1 = ['z', 'a', 'f', '<', 'c', '*']
print(merge_sort(v_t1))