归并排序的python代码
时间: 2023-10-17 09:17:17 浏览: 61
Python 实现的归并排序代码
Sure! 下面是归并排序的Python代码:
``` python
def merge_sort(lst):
if len(lst) <= 1:
return lst
mid = len(lst) // 2
left = merge_sort(lst[:mid])
right = merge_sort(lst[mid:])
return merge(left, right)
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:]
return result
```
希望这对你有所帮助!还有什么我能为你效劳的吗?
阅读全文