赞
踩
归并排序,分治法 O(nlogn)
有递归。
def merge_sort(seq): if len(seq) <= 1: return seq else: mid = int(len(seq) / 2) left_half = merge_sort(seq[:mid]) right_half = merge_sort(seq[mid:]) # 合并两个有序数组 new_seq = merge_sorted_list(left_half, right_half) return new_seq def merge_sorted_list(sorted_a, sorted_b): length_a, length_b = len(sorted_a), len(sorted_b) a = b = 0 new_sorted_seq = list() while a < length_a and b < length_b: if sorted_a[a] < sorted_b[b]: new_sorted_seq.append(sorted_a[a]) a += 1 else: new_sorted_seq.append(sorted_b[b]) b += 1 # 只剩下a或者b了,把剩下的a加进去,或者剩下的b加进去。 while a < length_a: new_sorted_seq.append(sorted_a[a]) a += 1 while b < length_b: new_sorted_seq.append(sorted_b[b]) b += 1 return new_sorted_seq def test_merge_sort(): import random seq = list(range(10)) random.shuffle(seq) assert merge_sort(seq) == sorted(seq)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。