Python Insert Sort

Source: Internet
Author: User

Insert Sort Summary:

    1. The element that currently needs to be sorted (Array[i]), compared to the last element already sorted (Array[i-1]), and loops to the next element to be sorted if the condition continues to execute the subsequent program.
    2. Caches the value of the element that is currently being sorted so that the correct location is found for insertion.
    3. The sorted element is compared to the element that is already numbered, and moves backwards (ascending) larger than it is.
    4. The element to be sorted, inserted into the correct position.

Example code:

Array = [3, 4, 1, 6, 2, 9, 7, 0, 8, 5]#Insert_sort forIinchRange (1, Len (array)):ifARRAY[I-1] >Array[i]: Temp= Array[i]#elements that currently need to be sortedindex = i#used to record where the sort element needs to be inserted         whileIndex > 0 andARRAY[INDEX-1] >Temp:array[index]= Array[index-1]#move the sorted element back one bit, leaving the position to be insertedIndex-= 1Array[index]= Temp#inserts the elements that need to be sorted into the specified position#print sort result.Print(array)

Python Insert Sort

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.