def Merge(nums, low, mid, high):
res = []
i = low
j = mid+1
while i<=mid and j<=high:
if nums[i]<=nums[j]:
res.append(nums[i])
i += 1
else:
res.append(nums[j])
j += 1
while i<=mid:
res.append(nums[i])
i += 1
while j<=high:
res.append(nums[j])
j += 1
nums[low:high+1] = res[:]
def MergeSort(nums, low, high):
if low<high:
mid = (low+high)/2
MergeSort(nums, low, mid)
MergeSort(nums, mid+1, high)
Merge(nums, low, mid, high)
if __name__ == '__main__':
nums = [int(i) for i in raw_input().split()]
n = len(nums)
nums.insert(0, 0)
MergeSort(nums, 1, n)
for i in nums[1:]:
print i,
print ""
归并排序(二路归并) Python实现
最新推荐文章于 2024-05-04 10:50:24 发布