Insertion Sort in Python
Insertion Sort is very simple and very intuitive sorting algorithm similar to how one arranges playing cards in one’s hand. This algorithm builds the final sorted array one at a time. When working with small datasets and partially sorted data, this algorithm is good, though it is not as efficient on larger datasets.
Here’s a detailed explanation of how Insertion Sort works:
Algorithm
- Divide the list into two parts:
- The sorted part (initially contains the first element only).
- The unsorted part (contains the rest of the elements).
- Pick the first element from the unsorted part and compare it backward with the elements of the sorted part.
- Insert the picked element into its correct position in the sorted part by shifting larger elements one position to the right.
- Repeat this until all elements from the unsorted part get included into the sorted part.
Steps with Example
Let’s sort the array [5, 3, 8, 6, 2]
:
- Initial Array:
[5, 3, 8, 6, 2]
:- The sorted part:
[5]
- The unsorted part:
[3, 8, 6, 2]
- The sorted part:
- Step 1:
- Choose
3
from the unsorted part. - Compare
3
with5
in the sorted part. Since3 < 5
, shift5
to the right. - Insert
3
in the first position. - Array becomes:
[3, 5, 8, 6, 2]
- Choose
- Step 2:
- Choose
8
. - Compare
8
with5
. Since8 > 5
, it is already in the right place. - Array stays:
[3, 5, 8, 6, 2]
- Choose
- Step 3:
- Select
6
. - Compare
6
with8
. Since6 < 8
, move8
one position to the right. - Compare
6
with5
. Since6 > 5
, place6
after5
. - Array becomes:
[3, 5, 6, 8, 2]
- Select
- Step 4:
- Select
2
. - Compare
2
with8
,6
,5
, and3
. Since2 < all
, shift them all to the right. - Insert
2
in the first position. - Array becomes:
[2, 3, 5, 6, 8]
- Select
Python Implementation
Here’s how Insertion Sort can be implemented in Python:
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i] # The element to be inserted
j = i - 1 # Index of the last element in the sorted part
# Move elements of the sorted part that are greater than the key
while j >= 0 and arr[j] > key:
arr[j + 1] = arr[j]
j -= 1
# Insert the key into the correct position
arr[j + 1] = key
return arr
# Example usage
numbers = [5, 3, 8, 6, 2]
sorted_numbers = insertion_sort(numbers)
print("Sorted Array:", sorted_numbers)
Explanation of Code
- Outer Loop: Runs for each element starting from the second element (i = 1).
- Key: current element to insert in the already sorted portion of the array.
- Inner Loop: Shifts elements of the sorted part that are greater than the key to the right.
- Insertion: Positioning the key into its corresponding location.
Time Complexity
- Best Case: O(n) when the array is already sorted.
- Worst Case: O(n^2) when the array is sorted in reverse order.
- Average Case: O(n^2).
Space Complexity
- Insertion Sort is in-place, so the space complexity is O(1).
Advantages
- Simple and easy to implement.
- Efficient for small datasets or partially sorted data.
Disadvantages
- Not suitable for large datasets due to its O(n^2) time complexity.