Insertion Sort is a simple and efficient comparison-based sorting algorithm suitable for small datasets and nearly sorted data. It builds the final sorted array one element at a time by repeatedly inserting the next element into the correct position within the sorted portion of the array.
The algorithm divides the input array into two parts:
- Sorted Subarray: Initially contains the first element of the array.
- Unsorted Subarray: Contains the rest of the elements.
The algorithm proceeds by picking each element from the unsorted subarray and inserting it into the correct position in the sorted subarray.
Step-by-step process:
- Start with the second element (index 1) of the array; consider the first element (index 0) as already sorted.
- Compare the current element with the elements in the sorted subarray:
- Shift all elements in the sorted subarray that are greater than the current element to the right by one position.
- Insert the current element into its correct position within the sorted subarray.
- Move to the next element in the unsorted subarray and repeat the process until the entire array is sorted.
Let's walk through an example to illustrate how Insertion Sort works.
Given array: [12, 11, 13, 5, 6]
Initial state:
- Sorted Subarray:
[12]
- Unsorted Subarray:
[11, 13, 5, 6]
Detailed steps:
- Initial array:
[12, 11, 13, 5, 6]
- Compare
11
with12
:- Since
11
<12
, shift12
to the right.
- Since
- Array after shifting:
[12, 12, 13, 5, 6]
- Insert
11
at position 0. - Array after insertion:
[11, 12, 13, 5, 6]
Result after Pass 1: [11, 12, 13, 5, 6]
- Initial array:
[11, 12, 13, 5, 6]
- Compare
13
with12
:- Since
13
>=12
, no shifting is needed.
- Since
- Insert
13
at its current position. - Array remains unchanged:
[11, 12, 13, 5, 6]
Result after Pass 2: [11, 12, 13, 5, 6]
- Initial array:
[11, 12, 13, 5, 6]
- Compare
5
with13
:- Since
5
<13
, shift13
to the right.
- Since
- Array after shifting:
[11, 12, 13, 13, 6]
- Compare
5
with12
:- Since
5
<12
, shift12
to the right.
- Since
- Array after shifting:
[11, 12, 12, 13, 6]
- Compare
5
with11
:- Since
5
<11
, shift11
to the right.
- Since
- Array after shifting:
[11, 11, 12, 13, 6]
- Insert
5
at position 0. - Array after insertion:
[5, 11, 12, 13, 6]
Result after Pass 3: [5, 11, 12, 13, 6]
- Initial array:
[5, 11, 12, 13, 6]
- Compare
6
with13
:- Since
6
<13
, shift13
to the right.
- Since
- Array after shifting:
[5, 11, 12, 13, 13]
- Compare
6
with12
:- Since
6
<12
, shift12
to the right.
- Since
- Array after shifting:
[5, 11, 12, 12, 13]
- Compare
6
with11
:- Since
6
<11
, shift11
to the right.
- Since
- Array after shifting:
[5, 11, 11, 12, 13]
- Compare
6
with5
:- Since
6
>=5
, insert6
at position 1.
- Since
- Array after insertion:
[5, 6, 11, 12, 13]
Result after Pass 4: [5, 6, 11, 12, 13]
Final sorted array: [5, 6, 11, 12, 13]
The time complexity of Insertion Sort depends on the initial order of the elements in the array.
-
Best Case:
- Occurs when the array is already sorted.
- The algorithm only compares each element with its predecessor.
- Time Complexity:
O(n)
-
Average Case:
- Occurs when the array elements are in random order.
- On average, each element is compared with half of the sorted subarray.
- Time Complexity:
O(n^2)
-
Worst Case:
- Occurs when the array is sorted in reverse order.
- Each new element is compared with all elements in the sorted subarray and placed at the beginning.
- Time Complexity:
O(n^2)
- Space Complexity:
O(1)
- Insertion Sort is an in-place sorting algorithm.
- It requires a constant amount of additional memory space regardless of the input size.