What is Insertion Sort?
Insertion sort arranges an array or list by 'inserting' each element into its appropriate position in each iteration.
This method is relatively straightforward and simple to implement, but its efficiency decreases as the amount of data increases.
How Insertion Sort Works
-
Start from the first element
: Consider the first element of the array as already sorted, and start the sorting process from the second element. -
Select current element
: Choose the element currently being sorted. -
Find appropriate position
: Insert the chosen element into the appropriate position within the already sorted portion of the array. To do this, shift elements that are greater than the selected element to the right. -
Repeat the process
: Repeat this process for all elements in the array.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.