Python programming to achieve Merge Sorting, python Merge Sorting

Source: Internet
Author: User

Python programming to achieve Merge Sorting, python Merge Sorting

Because a question about leetcode last week (Median of Two Sorted Arrays), I would like to carefully understand the implementation of Merge Sorting.

I 'd like to explain the sorting Logic first:

First of all, the merging and sorting use the bipartite method. In the final analysis, the idea is still to divide and conquer. Get a long array, divide it into two parts on the left and right, and then recursively divide it down. Then they are combined according to the two ordered arrays. This may be difficult to understand, so I will give a picture I have drawn.

The first step of merging and sorting is shown here. The array is split recursively according to the middle, and then the array is sorted by the method of sorting the two ordered arrays.

The method for sorting two sorted arrays is very simple. At the same time, the ratio of the first position of the two arrays is size, and the small one is placed into an empty array, move the pointer at the position of the empty array to the next one, and compare it with the previous position of the other array, and so on. To the end of any array, all the elements in the other I array are appended to the end of the new array.

The time complexity of recursive splitting is logN. However, the complexity of sorting two ordered arrays is N. the time complexity of this algorithm is N * logN, so it is NlogN.

Based on this analysis, we can look at a correct behavior.

After dividing the leftmost parts into the finest parts, you cannot divide the Left and Right Parts and then start merging.

Merge [4, 7] For the first combination

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

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

The fourth combination completes the merging of [3, 5, 9]

The fifth combination completes the sorting of [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:]:   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)

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.