Insert algorithm (Insertion Sort)

Sort insertion is a sorting algorithm based on in-place comparison. Here, a sub-list is always maintained in sorted form. Inserting is inserting an element into the sorted list of children. The element is inserted in the appropriate position so that the list is still in order.

What is sort insertion (Insertion Sort)?

Sort insertion is a sorting algorithm based on in-place comparison. Here, a sub-list is always maintained in sorted form. Inserting is inserting an element into the sorted list of children. The element is inserted in the appropriate position so that the list is still in order.

With the array data structure, we imagine that: the array consists of two parts: a sorted list of children and the other part are unordered elements. The insertion sort algorithm will perform a continuous search through that array, and unordered elements will be moved and inserted into the appropriate position in the sub-list (of the same array).

This algorithm is not suitable for use with large data sets when the case complexity is worst and the average case is Ο (n2) where n is the number of elements.

How to solve the insertion arrangement?

For example, we have an array of unordered elements:

Picture 1 of Insert algorithm (Insertion Sort)

The insertion sort algorithm compares the first two elements:

Picture 2 of Insert algorithm (Insertion Sort)

The algorithm found that both 14 and 33 were in ascending order. Now, 14 is in the sorted list of children.

Picture 3 of Insert algorithm (Insertion Sort)

The insertion algorithm continues to move to the next element and compares 33 and 27.

Picture 4 of Insert algorithm (Insertion Sort)

And see that 33 is not in the right position.

Picture 5 of Insert algorithm (Insertion Sort)

Algorithm to insert and swap positions of 33 and 27. Also check all elements in the sorted list. Here, we see that in this sub-list only one element 14 and 27 is greater than 14. Therefore the sub-list remains the same after the swap.

Picture 6 of Insert algorithm (Insertion Sort)

Now in the list of children we have two values ​​14 and 27. Continue to compare 33 with 10.

Picture 7 of Insert algorithm (Insertion Sort)

These two values ​​are not in order.

Picture 8 of Insert algorithm (Insertion Sort)

So we swap them.

Picture 9 of Insert algorithm (Insertion Sort)

Swapping leads to 27 and 10 in no order.

Picture 10 of Insert algorithm (Insertion Sort)

So we also exchange them.

Picture 11 of Insert algorithm (Insertion Sort)

We see again that 14 and 10 are not in order.

Picture 12 of Insert algorithm (Insertion Sort)

And we continue to swap these two numbers. Finally, after the third loop we have 4 elements.

Picture 13 of Insert algorithm (Insertion Sort)

The above process will continue until all unordered values ​​are sorted into the sorted child list.

Next we explore the programming aspect of insertion sorting algorithm.

Insert algorithm (Insertion Sort)

From the above illustration, we have a general picture of the insertion arrangement algorithm, from which we will have the basic steps in the algorithm as follows:

 Bước 1 : Kiểm tra nếu phần tử đầu tiên đã được sắp xếp. trả về 1 Bước 2 : Lấy phần tử kế tiếp Bước 3 : So sánh với tất cả phần tử trong danh sách con đã qua sắp xếp Bước 4 : Dịch chuyển tất cả phần tử trong danh sách con mà lớn hơn giá trị để được sắp xếp Bước 5 : Chèn giá trị đó Bước 6 : Lặp lại cho tới khi danh sách được sắp xếp

Sample algorithm for arranging foam

 B ắ t đầ u h à m insertionSort ( A : m ả ng ph ầ n t ử ) int holePosition int valueToInsert for i = 1 t ớ i length ( A ) th ự c hi ệ n : /* chọn một giá trị để chèn */ valueToInsert = A [ i ] holePosition = i /*xác định vị trí cho phần tử được chèn */ while holePosition > 0 v à A [ holePosition - 1 ] > valueToInsert th ự c hi ệ n : A [ holePosition ] = A [ holePosition - 1 ] holePosition = holePosition - 1 k ế t th ú c while /* chèn giá trị tại vị trí trên */ A [ holePosition ] = valueToInsert k ế t th ú c for K ế t th ú c h à m 

According to Tutorialspoint

Previous lesson: Bubble Sort (Bubble Sort)

Next lesson: Selection sort algorithm (Selection Sort)

Update 25 May 2019
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile