013.【Sorting Algorithm】Merge sorting method

1. Merge sort method

The merge sort method is to combine two or more sorted numbers into one large and sorted number series by merging. The first is to divide the unordered sequence into several small parts. The rule of dividing into several parts is to divide the length of each segment by 2 (half and half) until it can no longer be divided, then sort the divided sequence, and finally Gradually merge into a sorted large number column. The final result of the merge sort method also has two forms, namely an increasing sequence and a decreasing sequence.
insert image description here

2. Incremental sort using merge sort

Sort the list: 33, 10, 49, 78, 57, 96, 66, 21 using merge sort. The specific code is as follows:

def merge_sort(data):                                	# 自定义合并排序法函数

    if len(data) <= 1:                             		# 判断列表元素是否小于或等于1    
        return data                                 	# 当列表元素只有一个的时候,直接返回
    mid = len(data) // 2                               	# 分隔长度计算,整个数据的长度除以2取整
    left = data[:mid]                                  	# 左半边数据
    right = data[mid:]                                	# 右半边数据
    left = merge_sort(left)                             # 调用merge_sort()函数继续对左半边分隔并排序
    right = merge_sort(right)                           # 调用merge_sort()函数继续对右半边分隔并排序
    # 递归地进行排序
    result = []                                      	# 用来存储结果值
    while left and right:                               # 循环合并,判断条件是:左下标和右下标是否为真
        if left[0] <= right[0]:                      	# 判断左边数小于右边数
            result.append(left.pop(0))             		# 结果增加left[0]的值
        else:
            result.append(right.pop(0))             	# 结果增加right[0]的值
    if left:                                        	# 如果left的值为真
        result += left                                	# 结果显示左侧数据
    if right:                                       	# 如果right的值为真
        result += right                              	# 结果显示右侧数据
    return result                                    	# 返回排序后的结果


data = [33, 10, 49, 78, 57, 96, 66, 21]            		# 创建数列并初始化

print("原始数据为:", data)                           					# 输出原始数据
print('------------------------------------------------------')     	# 输出分界符
print("排序之后的数据为:", merge_sort(data))         						# 调用函数,输出排序好的数据
print('------------------------------------------------------')      	# 输出分界符

Guess you like

Origin blog.csdn.net/qq_42226855/article/details/131227380