from sys import argv from random import shuffle def zipper(x, y): """prec: x and y are SORTED list of sortable items of the same type. post: returns a combined list containing x and y that is sorted.""" p = 0 q = 0 out = [] while p < len(x) and q < len(y): if x[p] < y[q]: out.append(x[p]) p += 1 else: out.append(y[q]) q += 1 out += x[p:] out += y[q:] return out def merge_sort(x): if len(x) <= 1: return x mid = len(x)//2 first = x[:mid] second = x[mid:] return zipper(merge_sort(first), merge_sort(second)) n = int(argv[1]) x = list(range(n)) shuffle(x) print(x) print(merge_sort(x))