Insertion sort is a simple and intuitive sorting algorithm that builds a sorted array (or list) one element at a time by comparing each new element to the already sorted elements and inserting it into its correct position.
The Big Picture
Think of sorting a hand of playing cards. You pick up the cards one by one and place each new card into the correct position relative to the cards already in your hand. This process of finding the right spot and inserting the card is what insertion sort does.
Core Concepts
- Incremental Building: The sorted section of the list grows incrementally, one element at a time.
- Insertion: Each new element is placed in the correct position among the previously sorted elements.
- Shifting Elements: If necessary, elements in the sorted section are shifted to make room for the new element.
Detailed Walkthrough
- Start with the first element: The first element is trivially sorted by itself.
- Move to the next element: Take the next element from the unsorted portion.
- Find its correct position: Compare this element with the elements in the sorted portion, moving from right to left, until you find an element smaller than or equal to it.
- Shift elements if needed: If the new element is smaller than any of the sorted elements, shift those elements to the right.
- Insert the new element: Place the new element in its correct position.
- Repeat: Continue this process for each element until the entire list is sorted.
Understanding Through an Example
Let's sort the list [4, 3, 2, 10, 12, 1, 5, 6]
using insertion sort:
Initial state:
- Sorted part:
[4]
- Unsorted part:
[3, 2, 10, 12, 1, 5, 6]
- Sorted part:
Insert
3
:- Compare
3
with4
. Since3
is smaller, shift4
to the right and insert3
at the start. - Result:
[3, 4] | [2, 10, 12, 1, 5, 6]
- Compare
Insert
2
:- Compare
2
with4
and3
. Shift both to the right and insert2
at the start. - Result:
[2, 3, 4] | [10, 12, 1, 5, 6]
- Compare
Insert
10
:10
is larger than4
, so it stays at the end of the sorted section.- Result:
[2, 3, 4, 10] | [12, 1, 5, 6]
Insert
12
:12
is larger than10
, so it stays at the end.- Result:
[2, 3, 4, 10, 12] | [1, 5, 6]
Insert
1
:- Compare
1
with all sorted elements and shift them right. - Result:
[1, 2, 3, 4, 10, 12] | [5, 6]
- Compare
Insert
5
:- Compare
5
with12
and10
, shift them right. - Result:
[1, 2, 3, 4, 5, 10, 12] | [6]
- Compare
Insert
6
:- Compare
6
with12
and10
, shift them right. - Result:
[1, 2, 3, 4, 5, 6, 10, 12] | []
- Compare
Conclusion and Summary
Insertion sort works by incrementally building a sorted list, one element at a time. It inserts each new element into its correct position by comparing it with the elements in the sorted section and shifting those elements if necessary. This method is straightforward and efficient for small or nearly sorted datasets but can be inefficient for large lists due to the need for multiple comparisons and shifts.
Test Your Understanding
- Describe the basic process of insertion sort.
- Why might insertion sort be inefficient for large datasets?
- What happens when an element is inserted into the sorted part of the list?
Reference
For more details, check out the Wikipedia page on insertion sort.
'200===Dev Language > DS And Algorithm' 카테고리의 다른 글
Merge Sort Introduced (0) | 2024.05.27 |
---|---|
Merge Sort Introduced (0) | 2024.05.27 |
Bellman-Ford Algorithm (0) | 2024.05.27 |
Dijkstra's Algorithm Introduced (0) | 2024.05.27 |
자료구조 알고리즘 (배열 연산) (0) | 2020.11.24 |