Look at animation algorithm: sort insert sort


brief introduction

Insert sort is to insert the elements to be sorted into the sorted array to form a new ordered array.

This algorithm is called insert sort.

Insert sort example

Similarly, if we have an array: 29, 10, 14, 37, 20, 25, 44, 15, how can we insert and sort it?

Let’s first look at an animation of insertion and sorting, and have an intuitive understanding of it

Look at animation algorithm: sort insert sort

Let’s analyze the sorting process.

Eight numbers. We have seven rounds.

In the first round, assume that 29 is an ordered array. Starting from the second element, insert a new element into the ordered array. That is to say, insert 10 into the array [29]. We get [10,29].

In the second round, [10,29] has been ordered. Select the third element 14 in the array and insert the ordered array [10,29].

First, compare 29 with 14 and find that 29 > 14, then move 29 to the right by one bit [10,29], then compare 10 with 14 and find that 10 is less than 14, then insert 14 after 10 [10,14,29], and so on.

Insert sort Java program

Let’s see how to write a java program

public class InsertionSort {

    public void doInsertSort(int[] array){
        log.info (the array before sorting is: {} ", array);
        int n = array.length;
        //Insert from the second element
        for (int i = 1; i < n; ++i) {
            int key = array[i];
            int j = i - 1;
            //J represents the ordered array before the element is inserted
            //The ordered array is compared with the data to be inserted from the back to the front. If it is larger than the data to be inserted, it needs to move one bit to the right
            while (j >= 0 && array[j] > key) {
                array[j + 1] = array[j];
                    j = j - 1;
            //The position of the last j + 1 is where the new element needs to be inserted
            array[j + 1] = key;
            log.info (the array sorted in the {} round is: {} ", I + 1, array);


    public static void main(String[] args) {
        int[] array= {29,10,14,37,20,25,44,15};
        InsertionSort insertionSort=new InsertionSort();

Results of operation:

Look at animation algorithm: sort insert sort

Time complexity of insertion sort

From the code, we can see that there is a for loop in the insert sort, and there is a while loop in the for loop.

So the time complexity of insertion sort is O (N 2).

Code address of this article:


This article has been included inhttp://www.flydean.com/algorithm-insertion-sort/

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you don’t know are waiting for you to discover!

Welcome to my official account: “those things in procedure”, understand technology, know you better!

Look at animation algorithm: sort insert sort

Recommended Today

How to Build a Cybersecurity Career

Original text:How to Build a Cybersecurity Career How to build the cause of network security Normative guidelines for building a successful career in the field of information security fromDaniel miesslerstayinformation safetyCreated / updated: December 17, 2019 I’ve been doing itinformation safety(now many people call it network security) it’s been about 20 years, and I’ve spent […]