def merge(num_list,l_b,l_e,r_b,r_e): temp=[] begin=l_b while l_b<=l_e and r_b<=r_e: if num_list[l_b] < num_list[r_b]: temp.append(num_list[l_b]) l_b=l_b+1 else: temp.append(num_list[r_b]) r_b=r_b+1 while l_b<=l_e: temp.append(num_list[l_b]) l_b=l_b+1 while r_b<=r_e: temp.append(num_list[r_b]) r_b=r_b+1 for index in range(0,len(temp)): num_list[begin+index]=temp[index] def mergeSort(num_list,b,e): if b<e: mid=int((b+e)/2) mergeSort(num_list,b,mid) mergeSort(num_list,mid+1,e) l_b=b l_e=mid r_b=mid+1 r_e=e merge(num_list,l_b,l_e,r_b,r_e) def main(): num_list=[5,4,1,7,9,8,6,5,4,7] mergeSort(num_list) if __name__=="__main__": main() |