Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f3e55d5

Browse files
Add Python
1 parent d41475a commit f3e55d5

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

‎5.mergeSort.md

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,30 @@ function merge(left, right)
7070

7171
return result;
7272
}
73-
```
73+
```
74+
75+
## 4. Python 代码实现
76+
77+
```python
78+
def mergeSort(arr):
79+
import math
80+
if(len(arr)<2):
81+
return arr
82+
middle = math.floor(len(arr)/2)
83+
left = arr[0:middle]
84+
right = arr[middle:]
85+
return merge(mergeSort(left), mergeSort(right))
86+
87+
def merge(left,right):
88+
result = []
89+
while left and right:
90+
if left[0] <= right[0]:
91+
result.append(left.pop(0));
92+
else:
93+
result.append(right.pop(0));
94+
while left:
95+
result.append(left.pop(0));
96+
while right:
97+
result.append(right.pop(0));
98+
return result
99+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /