Time complexity: O(n*log(n)) for any case.
The idea is really simpe and adheres ‘divide and conquer’ paradigm:
- divide on halves until get list with length equal 1 //just a recursive calls
- 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)