This article mainly introduces the python insertion sorting algorithm, and compares and analyzes the implementation skills of the Python insertion sorting algorithm through two simple examples, for more information, see the example in this article. Share it with you for your reference. The details are as follows:
Def insertsort (array): for removed_index in range (1, len (array )): removed_value = array [removed_index] insert_index = removed_index while insert_index> 0 and array [insert_index-1]> removed_value: array [insert_index] = array [insert_index-1] insert_index-= 1 array [insert_index] = removed_value
Another version:
Def insertsort (array): for lastsortedelement in range (len (array)-1): checked = lastsortedelement while array [checked]> array [lastsortedelement + 1] and checked> = 0: checked-= 1 # Insert the number into the correct position array [checked + 1], array [checked + 2: lastsortedelement + 2] = array [lastsortedelement + 1], array [checked + 1: lastsortedelement + 1] return array
I hope this article will help you with Python programming.