forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.py
More file actions
49 lines (39 loc) · 876 Bytes
/
sort.py
File metadata and controls
49 lines (39 loc) · 876 Bytes
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
#!/usr/bin/env python
# encoding: utf-8
"""
@author: zhanghe
@software: PyCharm
@file: sort.py
@time: 2017/6/7 上午9:48
"""
def merge(left, right):
i, j = 0, 0
result = []
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
result += left[i:]
result += right[j:]
print result
return result
def merge_sort(lists):
print lists
# 归并排序
if len(lists) <= 1:
return lists
num = len(lists) / 2
print '左 分',
left = merge_sort(lists[:num])
print '右 分',
right = merge_sort(lists[num:])
print '\t治',
return merge(left, right)
if __name__ == '__main__':
a = [7, 2, 4, 7, 9, 3, 5, 7, 8, 1, 3, 60, 4, 2, 6]
print merge_sort(a)
"""
"""