Implementation code for Python insertion sorting algorithm

Source: Internet
Author: User
1. Algorithm:
There is a set of keywords {k 1, K 2, ..., K n}; The ordering begins with the thought that K 1 is an ordered sequence; let K 2 insert an ordered sequence with a table length of 1, making it an ordered sequence with a table length of 2, and then let K 3 insert an ordered sequence with a table length of 2, which becomes a table 3 ordered sequence, and so on, finally let K n insert the above table length as the ordered sequence of n-1, a table with an ordered sequence of N.

2.python inserting sort codes

Copy the Code code as follows:


def insertion_sort (List2):
For I in range (1, Len (LIST2)):
Save = List2[i]
j = I
While J > 0 and List2[j-1] > Save:
LIST2[J] = list2[j-1]
J-= 1
LIST2[J] = Save

Results: [2, 3, 4, 21, 33, 44, 45, 67]

3. Complexity of Time: O (n*n)

  • 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.