Introduction to the method of merging and sorting in Python programming

Source: Internet
Author: User
This article is mainly for you to introduce the Python programming implementation of the specific code to merge, with a certain reference value, interested in small partners can refer to

Because of a problem leetcode last week (Median of the Sorted Arrays), I want to take a closer look at the implementation of the merge sort.

Let's start with a sort of idea:

In the first place, the two-point method was used, and the final thought was divided. Get a long array and divide it into the left and right two parts, and then recursively divide it. They are then combined in the same way as two sequential arrays. This may be difficult to understand, so give a picture of me.

This shows the first step in the merge sort, recursively splitting the array according to middle, and then sorting it using the method of sorting two ordered arrays after the last minute.

The two ordered array ordering method is very simple, at the same time, the first position of the two array is larger than the size of the small into an empty array, and then placed in the empty array of the position of the pointer back one, and then continue to the other array of the previous position to compare, and so on. All the elements in the other I array are appended to the new array after the last array is out of the stack.

Because the time complexity of recursive splitting is logn However, the complexity of the method of ordering two ordered arrays is n the time complexity of the algorithm is N*LOGN so it is nlogn.

Based on this wave analysis, we can look at a behavior that is right.

When the leftmost point is the most fine, it is no longer possible to divide the left and right and start merging.

The first combination completes the merger of [4, 7]

The second combination completes the merging of [4, 7, 8]

The third combination completes the merger of [3, 5]

Fourth combined completion [3, 5, 9]

The fifth combination completes the sorting of the merge end [3, 4, 5, 7, 8, 9].

Put the Python code below.

def merge (A, b): c = [] H = j = 0 While J < Len (a) and H < Len (b):  if A[J] < B[h]:   c.append (A[j])   J + = 1  Else:   c.append (B[h])   H + = 1 if j = Len (a): for  I in B[h:]:   c.append (i) Else: for  i in a[j:]:< C9/>c.append (i) return cdef merge_sort (lists): If Len (lists) <= 1:  return lists middle = len (lists)/2 left = Merge _sort (Lists[:middle]) right = Merge_sort (lists[middle:]) return merge (left, right) if name = = ' main ': a = [4, 7, 8, 3, 5, 9 ] Print Merge_sort (a)
Related Article

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.