Introduction to the implementation and simple improvement of the insertion Sorting Algorithm in Python programs

Source: Internet
Author: User
This article mainly introduces the implementation and simple improvement of the insertion Sorting Algorithm in Python. The worst time complexity of the insertion sorting algorithm is O (n ^ 2 ), the optimal time complexity is O (n). There is a certain amount of optimization space. If you need it, you can refer to the following general example for implementing insert sorting in Python:

# Coding = cp936 # coding = cp936 # insert Sorting Algorithm def InsertionSort (A): for j in range (1, len ()): key = A [j] I = J-1 # forward lookup insert position while I> = 0 and A [I]> key: A [I + 1] = A [I] I = I-1 A [I + 1] = key # initialize input data A = [] input = raw_input ('Please input some numbers: ') # Enter commas to separate integer columns such as, 7, 6, 5, 1, 8, 34for item in input. split (','):. append (int (item) InsertionSort (A) # insert sort print

The principle of the insert algorithm is: Compare the current element with the sorted part. when the conditions are met, insert the element, and all the elements after the inserted point are moved back.
However, I was misled by this description and made some detours during implementation. For example, the following list is available:

test = [2, 5, 11, 21, 10, 18, 24]

For example, if the current element is 10, the initial implementation idea is to start from the first element in the list and continue to compare to element 11 to find the proper position. in the end, sorting can be achieved, but one problem is that after I insert 10 to the position of 11, both 11 and 21 need to be moved back, which requires another loop, the implementation is as follows:

def insertSort(sort_list):  list_length = len(sort_list)  if list_length < 2 :    return sort_list  for i in range(1,list_length):    key = sort_list[i]    j = 0    while j < i:      if sort_list[j] > key:        for k in range(i,j,-1):          sort_list[k] = sort_list[k-1]        sort_list[j] = key        break      j += 1  return sort_list

First, three cyclic variables and three-tier loops are introduced, which is less efficient. Second, the code structure is chaotic and needs to be improved.

Later, I thought that I could move an element to a proper position. If I went to the supermarket to buy fruit and got an inappropriate one, I would always put it aside without touching it. Specific to the algorithm implementation, the following list is also used as an example. The current element is 10. First, it is compared with the adjacent 21. If 21 is larger than 10, then 21 is moved one by one, that is, move to the location where 10 is located. Then, compare 10 and 11, and move one digit after 11. When we compare to element 5, we find that we have found the location where 10 should be stored, at this time, the Movement is complete.
The code is implemented as follows:

def insertSort(sort_list):  list_length = len(sort_list)  if list_length < 2 :    return sort_list  for i in range(1,list_length):    key = sort_list[i]    j = i - 1    while j >=0 and sort_list[j] > key:      sort_list[j+1] = sort_list[j]      j -= 1    sort_list[j+1] = key  return sort_list

You can easily compare them.

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.