Insertion Sort in Python Program

In this article, we will learn about the implementation of Insertion Sort in Python. Insertion sort is a simple sorting algorithm that builds the final sorted array one item at a time, similar to how you might sort playing cards in your hands.

Algorithm

  • Iterate over the input elements by growing the sorted array at each iteration.

  • Compare the current element with the largest value available in the sorted array.

  • If the current element is greater, then it leaves the element in its place and moves on to the next element else it finds its correct position in the sorted array and moves it to that position.

  • This is achieved by shifting all the elements towards the right, which are larger than the current element, in the sorted array to one position ahead.

Now let's see the visual representation of the algorithm ?

Initial Array: 64 25 12 22 11 Step 1: Insert 25 25 64 12 22 11 Final Sorted Array: 11 12 22 25 64

Implementation

Here's the implementation of insertion sort with numbers ?

def insertion_sort(arr):
    for i in range(1, len(arr)):
        key = arr[i]
        # Move elements of arr[0..i-1], that are greater
        # than key, to one position ahead of their current position
        j = i - 1
        while j >= 0 and key < arr[j]:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = key

# Example with numbers
numbers = [64, 25, 12, 22, 11, 90]
print("Original array:", numbers)
insertion_sort(numbers)
print("Sorted array:", numbers)
Original array: [64, 25, 12, 22, 11, 90]
Sorted array: [11, 12, 22, 25, 64, 90]

Example with Characters

Insertion sort also works with characters and strings ?

def insertion_sort(arr):
    for i in range(1, len(arr)):
        key = arr[i]
        j = i - 1
        while j >= 0 and key < arr[j]:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = key

# Example with characters
letters = ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']
print("Original letters:", letters)
insertion_sort(letters)
print("Sorted letters:", letters)
Original letters: ['t', 'u', 't', 'o', 'r', 'i', 'a', 'l']
Sorted letters: ['a', 'i', 'l', 'o', 'r', 't', 't', 'u']

How It Works

The algorithm maintains two subarrays:

  • Sorted subarray: Elements from index 0 to i-1

  • Unsorted subarray: Elements from index i to n-1

At each iteration, the algorithm picks the first element from the unsorted subarray and places it at its correct position in the sorted subarray.

Complexity Analysis

Complexity Best Case Average Case Worst Case
Time O(n) O(n²) O(n²)
Space O(1) O(1) O(1)

Conclusion

Insertion sort is simple to implement and efficient for small datasets or nearly sorted arrays. While it has O(n²) time complexity in worst case, it performs well when the input size is small or the array is already partially sorted.

Updated on: 2026-03-25T06:31:27+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements