Code implementation of Python insertion sort

Introduction to Insertion Sort

        There is an already ordered data sequence, and it is required to insert a number into this already arranged data sequence, but it is required that the data sequence is still in order after insertion. At this time, a new sorting method - insertion sorting is required. The basic operation of insertion sorting is to insert a piece of data into the ordered data that has been sorted, so as to obtain a new ordered data with the number plus one. The algorithm is suitable for sorting a small amount of data, and the time complexity is O(n^2). is a stable sorting method. The insertion algorithm divides the array to be sorted into two parts: the first part contains all the elements of the array, except for the last element (let the array have one more space to insert), while the second part contains only this one element (i.e. the element to be inserted). After the first part is sorted, insert this last element into the sorted first part.

        Although the code implementation of insertion sort is not as simple and crude as bubble sort and selection sort, its principle should be the easiest to understand, because anyone who has played poker should be able to understand it in seconds. Insertion sorting is the most simple and intuitive sorting algorithm. It works by constructing an ordered sequence. For unsorted data, scan from the back to the front in the sorted sequence, find the corresponding position and insert it.

        Insertion sort, like bubble sort, also has an optimization algorithm called split half insertion.


Algorithm steps and principles

        The basic idea of ​​insertion sorting is: at each step, a record to be sorted is inserted into the appropriate position in the previously sorted file according to the size of its key code value, until all are inserted.

        Treat the first element of the first sequence to be sorted as an ordered sequence, and treat the second element to the last element as an unsorted sequence.

        Scans the unsorted sequence sequentially from beginning to end, and inserts each scanned element into its proper position in the sorted sequence. (If the element to be inserted is equal to an element in the ordered sequence, the element to be inserted is inserted after the equal element.)


Insertion sort animation demo


Python code implementation 

def insertionSort(arr):
    for i in range(len(arr)):
        preIndex = i-1
        current = arr[i]
        while preIndex >= 0 and arr[preIndex] > current:
            arr[preIndex+1] = arr[preIndex]
            preIndex-=1
        arr[preIndex+1] = current
    return arr

Summary -  time complexity and stability of insertion sort

        The time complexity of insertion sort is O(N^2).

        Insertion sort is stable because the element to be inserted can be placed in front when the ordered partial element is equal to the element to be inserted.

Guess you like

Origin blog.csdn.net/leyang0910/article/details/130470293