defselectionSort(data): ''' 选择排序 :param data: 原序列 :return: 升序排列后序列 ''' i = 0 while i < len(data)-1: j = i while j < len(data): if data[i] > data[j]: data[i], data[j] = data[j], data[i] j += 1 i += 1 return data
defmergeSort(data): ''' 归并排序 :param data: 原序列 :return: 升序排列后序列 ''' if len(data)<=1: return data
middle = len(data)//2 left = mergeSort(data[:middle]) right = mergeSort(data[middle:]) # 治 return merge(left, right)
defmerge(left,right): # 分 c = [] h = j = 0 while j<len(left) and h<len(right): if left[j]<right[h]: c.append(left[j]) j+=1 else: c.append(right[h]) h+=1
if j==len(left): for i in right[h:]: c.append(i) if h==len(right): for i in left[j:]: c.append(i)