Merge Sort in Python

Reading time ~2 minutes

Time complexity: O(n*log(n)) for any case.

The idea is really simpe and adheres ‘divide and conquer’ paradigm:

  1. divide on halves until get list with length equal 1 //just a recursive calls
  2. merge recursively // takes log(n) merges with n comparison in each

Writing it in python takes only few lines:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
def merge(l, r):
    result = []
    i = j = 0
    while i < len(l) and j < len(r):
        if l[i] < r[j]:
            result.append(l[i])
            i += 1
        else:
            result.append(r[j])
            j += 1
    result += l[i:]
    result += r[j:]
    return result

def msort(a):
    if len(a) == 1:
        return a
    mid = len(a) / 2
    return merge(msort(a[:mid]), msort(a[mid:]))


array = [101, 50, 2, 43, 6, 100, 8, 20, 3, 15]
print array
print msort(array)

How to Download Multiple Instagram Photos at Once?

You want to print your Insta photos or just keep them save and archived? Out of luck - there is no obvious way to download them, unless y...… Continue reading

Polymer and Vulcanization

Published on August 08, 2016

Jeff Dean of Google

Published on August 06, 2016