Python implementation merge Sort, merge sort detailed analysis

Source: Internet
Author: User

The process of learning to merge and sort is very painful. It is not commonly used, and it seems that the time complexity seems to be the lowest in several sorts, and less than the time complexity of the fast queue, but it does not perform the fastest.
Many friends do not understand the complexity of the time is low why the running speed is not necessarily fast, this unclear partner can look at my previous published article http://www.cnblogs.com/Lin-Yi/p/7301535.html
After reading it, you may have a new understanding of the complexity of the time.

My views are often not the official definition, I hope to help more weak-minded students to read the idea ~

Merge sort:
Merge separately, split into individual elements, merge in the correct order

If we have an n number of columns, subscript from 0 to N-1
First, the process of separation.
1 we divide this sequence into two small numbers according to N//2.
2 Divide the two small series by half the new length into two smaller
。。。 Keep repeating, until every number separates.
For example: 6 5 4 3 2 1
First time n=6 n//2=3 divided into 6 5 4 3 2 1
Second n=3 N//2=1 divided into 6 5 4 3 2 1
The third n=1 part is not divided.
n=2 N//2=1 divided into 5 4 2 1

This is followed by the process of merging the sort:
3 After separation we follow the last divided two number comparison size to form the correct order after the combination binding
Just give the example last line of the last separated number sorted after the binding becomes 4 5 1 2
The second-to-last line after sorting turns out to be 6 4 5 3 12 after sorting the newest separate numbers
4 for each set of data according to the results of the last separation, after the collation of the binding
6 and 4 5 (two numbers bound) to sort
3 and 1 2 (two numbers bound) to sort
After the first line of the above example is sorted 4 5 6 1 2 32 Group of data
5 Sort the two groups that were last separated
Hold it. 4 5 6 1 2 32 arrays, sorted, each time the first (smallest) comparison of each column is taken out, the smaller number is placed in the result array. Then the next sort.
Each array takes out the first number, the small one takes it out and puts it in the first place, 1, and it becomes 4 5 6 2 3
Each array takes out the first book the smaller one is placed in the next position 1 2 is taken out, pending sort 4 5 6 2
Each array takes out the first book the smaller one is placed in the next position 1 2 3 is taken out, pending sort 4 5 6
If an array is empty, it means that the other array must be more than the last large append of the sorted array 1 2 3 4 5 6
The equivalent of each time we get two ordered lists to merge, from the two list of the first element of the comparison, the small take out, in the new first element comparison, the small to take out
So that until two lists are empty, two lists are merged sequentially

End

Time complexity: Best worst is O (n log n)
Stability: Stable
Cons: Every time you split the score group to Happy array, each merging array to open a new array, space complexity is very large

This is implemented in Python
def merge_sort (LI): #不断递归调用自己一直到拆分成成单个元素的时候就返回这个元素, no longer split if Len (li) = = 1:return Li #取拆分的中间位置 mid = l     En (LI)//2 #拆分过后左右两侧子串 left = Li[:mid] right = Li[mid:] #对拆分过后的左右再拆分 until there is only one element #最后一次递归时候ll和lr都会接到一个元素的列表    # before the last recursion the LL and RL will receive the ordered subsequence LL = Merge_sort (left) RL =merge_sort (right) # We return the two split results after sorting and returning the correct order of the sub-list # Here we call to carry a function to help us in order to merge LL and LR return merge (LL, RL) #这里接收两个列表def merge (left, right): # from two sequential lists in order to take the data comparison and put Resul t # each time we compare the smallest number of two lists, put the smaller one into the result result = [] while Len (left) >0 and Len (right) >0: #为了保持稳定性, when encountering equal Priority to put the left number into the result list, because left is also a large sequence of the comparison of the right if left[0] <= right[0]: result.append (left.pop (0)) Else : Result.append (Right.pop (0)) #while循环出来之后 show that one of the arrays has no data, we add another array to the result array. result + = Left result + = Right return resultif __name__ = = ' __main__ ': li = [5,4, 3, 2, 1] li2 = Merge_sort (li) print (LI2)

  

The algorithm process is very painful to understand! Good grievance

However, I hope that everyone will calm down to learn a little bit, certain harvest!

Python implementation merge Sort, merge sort detailed analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.