Insert algorithm (Insertion Sort)
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:
The insertion sort algorithm compares the first two elements:
The algorithm found that both 14 and 33 were in ascending order. Now, 14 is in the sorted list of children.
The insertion algorithm continues to move to the next element and compares 33 and 27.
And see that 33 is not in the right position.
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.
Now in the list of children we have two values 14 and 27. Continue to compare 33 with 10.
These two values are not in order.
So we swap them.
Swapping leads to 27 and 10 in no order.
So we also exchange them.
We see again that 14 and 10 are not in order.
And we continue to swap these two numbers. Finally, after the third loop we have 4 elements.
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)
You should read it
- Shell Sort in data structure and algorithm
- Bubble Sort (Bubble Sort)
- Selection sort algorithm (Selection Sort)
- Alignment algorithm (Merge Sort)
- Quick Sort (Quick Sort)
- Binary Search algorithm (Binary Search)
- What is algorithm?
- How does YouTube algorithm work?
- Panda 4.0 algorithm was updated by Google on May 20, 2014
- Greedy Algorithm (Greedy Algorithm)
- Facebook is about to change the algorithm, articles that use 'interactive traps' will be downgraded
- Toshiba's algorithm helps normal computers calculate faster than supercomputers
Maybe you are interested
Will TikTok split the source code to create a US-specific algorithm?
Google Core Algorithm March 2024: Reduce unhelpful content by up to 40%.
Is Google's Helpful Content Algorithm Really Helpful?
The Mathematics of Winning: Algorithms Behind Online Slot Games
Artificial intelligence algorithms read human thoughts
Windows XP activation algorithm 'cracked' after 21 years