Insert Sort algorithm review (Python implementation)

Source: Internet
Author: User

The basic method for inserting a sort is to insert a record to be sorted by the size of its keyword into the appropriate position in the previously sorted sequence, each step until all records have been inserted.

binary insertion Sorting is an improvement to the insertion sorting algorithm, which, in the process of sorting algorithms, inserts the elements sequentially into the sequential sequence. Since the first half of the sequence is already ordered, we do not need to find the insertion point sequentially, and we can use the binary lookup method to speed up the search for the insertion point. The action is: in the process of inserting a new element into an ordered array, when looking for the insertion point, the first element to be inserted is set to A[low], the end element is set to A[high], then the wheel will be inserted into the element with A[m], where m= (Low+high)/ 2 compared, if smaller than the reference element, select A[low] to a[m-1] for the new insert area (i.e. high=m-1), otherwise select a[m+1] to A[high] for the new insert area (i.e. low=m+1), so that until Low<=high is not established, After this position, all elements are moved back one bit, and the new element is inserted into the a[high+1].

C=[-1,49,38,65,97,76,13,27,49]#where [0]=-1 this position is a staging unitd=[-1,49,38,65,97,76,13,27,49]#Insert sort directly------------------------------------------------------defInsertsort (list): forIinchRange (2, Len (list)):ifLIST[I]&LT;LIST[I-1]:#List[i] must be inserted into an ordered sub-tableList[0]=list[i]#Copy as SentinelLIST[I]=LIST[I-1]#First, list[i] The previous element is moved back oneJ=i-2#Judging from the front of List[i]             whilelist[0]<List[j]: list[j+1]=List[j] J-=1list[j+1]=LIST[0]#in the end, if it is not smaller than [j], it is inserted [j+1].#binary Insert Sort------------------------------------------------------defBinaryinsertsort (list): forIinchRange (2, Len (list): List[0]=List[i] Low=1 High=i-1 whilelow<=high:m= (Low+high)/2#binary            ifLIST[0]&LT;LIST[M]:#insertion point in low half areaHigh=m-1Else:#insertion point in high half areaLow=m+1J=i-1#record and move back         whileJ>=high+1: List[j+1]=List[j] J-=1List[high+1]=List[0]insertsort (c)PrintC[1:] Binaryinsertsort (d)PrintD[1:]

The resulting output has a sequence table:

Insert Sort algorithm review (Python implementation)

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.