-
Notifications
You must be signed in to change notification settings - Fork 0
/
mergesort.py
62 lines (55 loc) · 1.34 KB
/
mergesort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#! /usr/bin/env python
# -*- coding: utf-8 -*-
def merge_sort(alist):
if len(alist) < 2:
return alist[:]
middle = len(alist) / 2
left = merge_sort(alist[:middle])
right = merge_sort(alist[middle:])
return merged(left, right)
def merged(left, right):
result = []
i, j = 0, 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
while i < len(left):
result.append(left[i])
i += 1
while j < len(right):
result.append(right[j])
j += 1
return result
def mergeSort(alist):
"""
分治思想
归并排序先递归分解列表,再合并
"""
if len(alist) <= 1:
return alist
num = len(alist) // 2
left = mergeSort(alist[:num])
right = mergeSort(alist[num:])
return merge(left, right)
def merge(left, right):
"""
合并操作
"""
l, r = 0, 0
result = []
while l < len(left) and r < len(right):
if left[l] < right[r]:
result.append(left[l])
l += 1
else:
result.append(right[r])
r += 1
result += left[l:]
result += right[r:]
return result
alist = [54, 26, 93, 17, 77, 31, 44, 55, 20]
print merge_sort(alist)