Skip to main content

Understanding Insertion Sort: A Clear and Concise Python Tutorial for All Skill Levels

Insertion sort is an intuitive and straightforward sorting algorithm that builds the final sorted array (or list) one item at a time. It's particularly useful for small data sets or when adding new elements to an already sorted list.

Purpose of Insertion Sort

The goal of insertion sort is to rearrange the elements in a list so that they are in increasing order. Think of it like sorting playing cards in your hand: you start with one card and then insert each subsequent card into its correct position relative to the cards already sorted.

Python Code Implementation

Below is a well-commented implementation of insertion sort in Python:

def insertion_sort(arr):
    # Traverse through 1 to len(arr)
    for i in range(1, len(arr)):
        key = arr[i]  # The element to be positioned
        
        # Move elements of arr[0..i-1], that are greater than key,
        # one position ahead of their current position
        j = i - 1
        while j >= 0 and key < arr[j]:
            arr[j + 1] = arr[j]
            j -= 1
        
        # Place the key at after the element just smaller than it.
        arr[j + 1] = key

    return arr

# Example usage:
example_list = [12, 11, 13, 5, 6]
sorted_list = insertion_sort(example_list)
print("Sorted list:", sorted_list)

Explanation

  • Initialization: We start by assuming the first element is already sorted.
  • Key Element: For each subsequent element (key), we compare it with elements in the sorted portion of the array.
  • Shifting Elements: If an element in the sorted portion is larger than key, we shift it one position to the right.
  • Insertion: Once we find the correct position for key, we insert it.

Example Demonstration

Consider the list [12, 11, 13, 5, 6]. Here's how insertion sort processes this:

  1. Start with [12] (first element is considered sorted).
  2. Insert 11 before 12: [11, 12, 13, 5, 6].
  3. 13 is already in the correct position: [11, 12, 13, 5, 6].
  4. Insert 5 at the beginning: [5, 11, 12, 13, 6].
  5. Finally, insert 6: [5, 6, 11, 12, 13].

Input: [12, 11, 13, 5, 6]

Output: [5, 6, 11, 12, 13]

Time Complexity

The time complexity of insertion sort is:

  • Best Case: (O(n)) - when the list is already sorted.
  • Average and Worst Case: (O(n^2)) - due to nested loops.

Insertion sort is efficient for small data sets or nearly sorted lists but less so for larger, unsorted arrays compared to more advanced algorithms like quicksort or mergesort.

Conclusion

Insertion sort is a simple yet powerful algorithm that provides a solid foundation in understanding sorting techniques. Its simplicity makes it an excellent starting point for beginners, while its efficiency on small data sets can be appreciated by seasoned developers.


Comments