Before we talk about the merger sort, we'll discuss the next division.
Divide-and-conquer: it is to divide a complex problem into two or more identical or similar sub-problems, and then divide the problem into smaller sub-problems ... Until the last sub-problem can be easily solved directly (from Baidu 233333)
The core of divide and conquer law is to divide big problem into small problem. An algorithm that belongs to the leader ...
Merge sort: Divide the problem into multiple and then resolve it after merging
def mergesort (lists): If Len (lists) <= 1: return lists num = int (len (lists)/2) Left = mergesort (list S[:num]) Right = MergeSort (lists[num:]) return merge (left, right) def merge (Left,right): R, l=0, 0 Result=[] While L<len (left) and R<len (right): if LEFT[L] < Right[r]: result.append (left[l)) L + = 1 else: result.append (Right[r]) r + = 1 result + = Right[r:] result+= left[l:] return result
The merge sort is divided into two steps, splitting and merging
Merging: We imagine there are two ordered arrays of how we sort them [1,3,5,6], [2,4,9,10] process should be 1-2 compare 2-3 compare 3-5 .... So the complexity is O (n)
Split: 1-2-4-8 We know that the problem here is with the number of iterations/2 so the complexity here is O (log n)
So the complexity of the merge sort is O (n log n)
Sort Series 03--Merge sort