-
Notifications
You must be signed in to change notification settings - Fork 2
/
merge_sort.py
107 lines (88 loc) · 2.49 KB
/
merge_sort.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
#-*- encoding: UTF-8-*-
"""
* Copyright © https://github.com/jarry All rights reserved.
* @author: jarryli@gmail.com
* @version: 1.0
"""
import time
def merge_sort1(values):
result = values
if len(values) > 1:
mid = len(values) // 2
left = values[:mid]
right = values[mid:]
left = merge_sort1(left)
right = merge_sort1(right)
result = []
while len(left) > 0 and len(right) > 0:
# print('values:', result)
if left[0] < right[0]:
result.append(left[0])
left.pop(0)
else:
result.append(right[0])
right.pop(0)
for i in left:
result.append(i)
for i in right:
result.append(i)
return result
## merge_sort2
def merge_sort2(arr):
if len(arr) > 1:
mid = len(arr) // 2
left_arr = arr[:mid]
right_arr = arr[mid:]
# 左侧递归排序
merge_sort2(left_arr)
# 右侧递归排序
merge_sort2(right_arr)
i = j = k = 0
# 将数据复制到左右临时列表
while i < len(left_arr) and j < len(right_arr):
if left_arr[i] < right_arr[j]:
arr[k] = left_arr[i]
i += 1
else:
arr[k] = right_arr[j]
j += 1
k += 1
# 将左侧剩余复制
while i < len(left_arr):
arr[k] = left_arr[i]
i += 1
k += 1
# 将右侧剩余复制
while j < len(right_arr):
arr[k] = right_arr[j]
j += 1
k += 1
if __name__ == '__main__':
# 1. merge_sort1
arr = [7, 11, 9, 10, 12, 13, 8]
print("\r\n Source array1:")
print(arr)
start_time = time.time()
arr = merge_sort1(arr)
print("time:" + str((time.time() - start_time) * 1000) + " ms")
print("merge_sort1 sorted: ", arr)
# 2. merge_sort2
arr = [7, 11, 9, 10, 12, 13, 8]
print("\r\n Source array2:")
print(arr)
start_time = time.time()
merge_sort2(arr)
print("time:" + str((time.time() - start_time) * 1000) + " ms")
print("merge_sort2 sorted: ", arr)
"""
jarry@jarrys-mbp mergesort % python merge_sort.py
Source array1:
[7, 11, 9, 10, 12, 13, 8]
time:0.0290870666504 ms
('merge_sort1 sorted: ', [7, 8, 9, 10, 11, 12, 13])
Source array2:
[7, 11, 9, 10, 12, 13, 8]
time:0.0190734863281 ms
('merge_sort2 sorted: ', [7, 8, 9, 10, 11, 12, 13])
"""