Merge sort and quick sort are more suitable for large-scale data sorting. Both used the idea of partition.
Merge sort
The core idea of merge sort of quite simple, if you want to sort an array, we first array is divided into two portions back and forth from the middle, then both the front and rear portions, respectively, sorting and then sorted both portions are combined together again. This step by step down the divide and rule, the entire sorting into smaller sub-problems to solve.
Since this is a recursive, so find out the termination conditions are as follows:
merge_sort(p...r) = merge(merge_sort(p...middle),merge_sort(middle+1...r)
Wherein when p> no decomposition was continued = r, or, if a middle = middle // 2, when the middle == 1 no longer continue to decompose.
The recursive formula is explained as follows:
def merge(left:list,right:list)->list: temp = list() if left[-1] <= right[0]: temp = left + right return temp if right[-1] <= left[0]: temp = right +left return temp left_index = right_index = 0 while left_index <len(left) and right_index <len(right): if left[left_index] < right[right_index]: temp.append(left[left_index]) left_index += 1 else: temp.append(right[right_index]) right_index += 1 if left_index == len(left): temp += right[right_index:] else: temp += left[left_index:] return temp def merge_sort(li:list)->list: if len(li) <= 1: return li middle = len(li)//2 left = merge_sort(li[:middle]) right = merge_sort(li[middle:]) return merge(left,right)