Insertion Sort


Insertion Sort is one of the simplest and most intuitive sorting algorithms. It works by building a sorted array one element at a time. This sorting method is much like the way you might sort playing cards in your hands. You take one card at a time and insert it into the correct position in the sorted portion of the list. Despite being inefficient for large datasets, Insertion Sort can perform well for small datasets or nearly sorted arrays.


What is Insertion Sort?

Insertion Sort is a comparison-based sorting algorithm that builds the sorted list one element at a time by taking each new element and inserting it into its correct position among the already sorted elements. It is often compared to the way people sort playing cards by picking one card at a time and placing it in the correct order.

Key Properties of Insertion Sort:

  • In-Place: The sorting is done within the list without requiring additional storage.
  • Stable: The algorithm maintains the relative order of elements with equal values.
  • Efficient for Small Data: It works well for small or partially sorted datasets.
  • Time Complexity: O(n2) in the worst case, making it inefficient for large lists.

How Does Insertion Sort Work?

  1. Divide the List: Start by considering the first element of the list as already sorted.
  2. Select an Element: Take the next element from the unsorted part of the list.
  3. Find its Correct Position: Compare the selected element with the elements in the sorted part of the list.
  4. Shift Elements: If the selected element is smaller than the compared element, shift the compared element one position to the right.
  5. Insert the Element: Place the selected element in its correct position.
  6. Repeat: Continue this process until the entire list is sorted.

Insertion Sort: Step-by-Step Process

  1. Initialization: Consider the first element as sorted and begin with the second element.
  2. Element Insertion: Compare the current element with the elements in the sorted part of the list.
  3. Shifting: Shift the larger elements to the right to make space for the current element.
  4. Insert: Place the current element in the appropriate position.
  5. Repeat: Repeat the process for all elements until the list is sorted.

Insertion Sort: Python Code Implementation

Here is a Python implementation of the Insertion Sort algorithm:

def insertion_sort(arr):
    # Traverse through 1 to len(arr)
    for i in range(1, len(arr)):
        key = arr[i]  # Element to be inserted
        j = i - 1
        
        # Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position
        while j >= 0 and arr[j] > key:
            arr[j + 1] = arr[j]
            j -= 1
        
        # Insert the key at the correct position
        arr[j + 1] = key
    
    return arr

# Example usage:
arr = [12, 11, 13, 5, 6]
print("Unsorted Array:", arr)
sorted_arr = insertion_sort(arr)
print("Sorted Array:", sorted_arr)

Output:

Unsorted Array: [12, 11, 13, 5, 6]
Sorted Array: [5, 6, 11, 12, 13]

Explanation of Code:

  • Outer Loop: The outer loop traverses through each element of the list, starting from the second element (index 1).
  • Key Element: The key is the element to be inserted into the sorted portion of the list.
  • Inner Loop: The inner loop compares the key with each element in the sorted part of the list and shifts elements to the right until the correct position for the key is found.
  • Insert: Once the correct position is found, the key is inserted.

Time and Space Complexity of Insertion Sort

Time Complexity:

  • Worst Case: O(n2) – This happens when the list is sorted in reverse order.
  • Best Case: O(n) – This happens when the list is already sorted, and the algorithm only needs to make one pass.
  • Average Case: O(n2) – On average, the algorithm will need to shift about half of the elements for each insertion.

Space Complexity:

  • Space Complexity: O(1) – Insertion Sort is an in-place sorting algorithm, meaning it doesn't require additional memory apart from the input array.

Advantages of Insertion Sort

  1. Simple to Implement: Insertion Sort is easy to understand and implement, making it an excellent choice for teaching sorting algorithms.
  2. Efficient for Small Lists: It performs well on small datasets or nearly sorted lists.
  3. Stable Sort: It preserves the relative order of equal elements, which can be important in certain situations (e.g., when sorting records with multiple fields).
  4. In-Place Sorting: It does not require extra space, which makes it space-efficient.

Disadvantages of Insertion Sort

  1. Inefficient for Large Lists: The O(n2) time complexity makes it impractical for large datasets or when performance is critical.
  2. Redundant Comparisons and Shifts: Even for nearly sorted lists, the algorithm might still perform a large number of unnecessary comparisons and shifts.

When to Use Insertion Sort?

  1. Small Datasets: Insertion Sort is ideal for small datasets, where the inefficiency of the algorithm is less noticeable.
  2. Nearly Sorted Lists: If the list is already nearly sorted, Insertion Sort can perform very efficiently, with time complexity close to O(n).
  3. Educational Purposes: Due to its simplicity, Insertion Sort is often used in educational contexts to demonstrate the basic principles of sorting algorithms.