Python's example of merging and sorting two sequential tables _python

Source: Internet
Author: User

Assuming that there are 2 sequential table L1, L2, how to efficiently combine 2 lists and maintain an orderly state, where the default sort is positive sequence.

The idea is relatively simple, but is to compare L1 and L2 The first element of the head, will be smaller than a new list, and so on, until all the elements are placed in the new list.

Consider the 2 list L1 = [2], L2 = [1], how will they be merged? (Note: The following implementations change the values of L1 and L2)

Copy Code code as follows:

def signle_merge_sort (L1, L2):
TMP = []
If l1[0] < L2[0]:
Tmp.append (L1[0])
Tmp.extend (L2)
Del L2[0]
Else
Tmp.append (L2[0])
Tmp.extend (L1)
Del L1[0]
return tmp

This really can only deal with an element of the situation, can not solve the problem, but at least we have a general idea. If there are 2 elements in the list, the above method is not. We need to solve the boundary judgment problem, that is, when a L1 or L2 has an empty, the remaining list is added to the end of the sort result. Then make sure that the function handles only one element at a time and solves the problem by recursion.
Copy Code code as follows:

def recursion_merge_sort1 (L1, L2):
TMP = []
If Len (l1) = = 0:
Tmp.extend (L2)
return tmp
Elif len (L2) = = 0:
Tmp.extend (L1)
return tmp
Else
If l1[0] < L2[0]:
Tmp.append (L1[0])
Del L1[0]
Else
Tmp.append (L2[0])
Del L2[0]
TMP + = Recursion_merge_sort1 (L1, L2)
return tmp

There are 2 problems with the above program: if there is too much to judge, and each time you initialize TMP, it seems to be unfriendly to memory usage. Consider that the program terminates when a L1 or L2 is empty, and you can rewrite it a little bit:
Copy Code code as follows:

def _recursion_merge_sort2 (L1, L2, TMP):
If Len (l1) = = 0 or len (L2) = 0:
Tmp.extend (L1)
Tmp.extend (L2)
return tmp
Else
If l1[0] < L2[0]:
Tmp.append (L1[0])
Del L1[0]
Else
Tmp.append (L2[0])
Del L2[0]
Return _recursion_merge_sort2 (L1, L2, TMP)

def recursion_merge_sort2 (L1, L2):
Return _recursion_merge_sort2 (L1, L2, [])


But for python, even tail recursion is not as efficient, and in order to avoid a stack, it's usually done with loops, and then slightly rewritten:
Copy Code code as follows:

def loop_merge_sort (L1, L2):
TMP = []
While Len (L1) > 0 and Len (L2) > 0:
If l1[0] < L2[0]:
Tmp.append (L1[0])
Del L1[0]
Else
Tmp.append (L2[0])
Del L2[0]
Tmp.extend (L1)
Tmp.extend (L2)
return tmp

Today, a pit, a good reflection, is this.

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.