Python binary Insert Sort __python

Source: Internet
Author: User

Basic Ideas

In order to find the right insertion point for the direct insertion sort, a method of finding a bit with a similar binary method is used.


Python Implementation

Def Binaryinsert (a):
# binary Insertion sort: small-> large
# using the method of binary lookup on the basis of direct insertion sort for 
    i in xrange (1, Len (a)):
        index = a[i]<  C6/>low = 0
        hight = i-1 while low
        <= hight:
            mid = (low + hight)/2
            If Index > A[mid]: Low
                = Mid + 1
            else:
                hight = mid-1
        # After the loop is low, mid is the same, hight = Low-1 for
        J. Xrange (I, Low,-1): 
 A[J] = a[j-1]
        a[low] = index return
    a


Efficiency

Although the process of finding the insertion point is superior to the direct insertion method, the element moves the same number of times, so the complexity is still: O (n^2)

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.