Merge sort in Python

Source: Internet
Author: User

1 #Coding=utf-82 #!/usr/bin/python3 4 ImportSYS5 6 defmerge (Nums, first, Middle, last):7     "Merge"8     #slice boundaries, left-open, starting from 09Lnums = nums[first:middle+1 ]TenRnums = nums[middle+1:last+1 ] One lnums.append (sys.maxint) A rnums.append (sys.maxint) -L =0 -R =0 the      forIinchRange (First, last+1 ): -         ifLNUMS[L] <Rnums[r]: -Nums[i] =Lnums[l] -L+=1 +         Else: -Nums[i] =Rnums[r] +R+=1 A  at defMerge_sort (Nums, First, last): -     """MergeSort - The mergesort function passes the subscript, not the number of elements -     """ -     ifFirst <Last : inMiddle = (first + last)/2 -         #print "Middle in loops", Count, "is:", Middle + Merge_sort (nums, First, middle) -Merge_sort (Nums, middle+1, last) the merge (Nums,first, middle, last) *         #print "loops:", Count, ">>", Nums $ Panax Notoginseng  -  the if __name__=='__main__': +Nums = [10, 8, 4,-1, 2, 6, 7, 3] A     Print "Nums is", Nums theMerge_sort (nums, 0, 7) +     Print "Merge_sort", Nums

/usr/bin/python2.7/root/pycharmprojects/sort/merge-sort.py
Nums is [10, 8, 4,-1, 2, 6, 7, 3]
Merge_sort [-1, 2, 3, 4, 6, 7, 8, 10]

Process finished with exit code 0

Merge sort in Python

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.