• Courses
  • Tutorials
  • DSA
  • Data Science
  • Web Tech
October 08, 2024 |1.6K Views

Insertion Sort Algorithm

  Share  1 Like
Description
Discussion

Insertion Sort Algorithm | Comprehensive Guide

Insertion Sort is a simple and intuitive sorting algorithm that builds the final sorted array one item at a time. It is much like the process of sorting playing cards in your hands, where you take one card at a time and insert it into its correct position among the previously sorted cards. Insertion Sort is often used for small datasets and is efficient when the array is nearly sorted.

What is Insertion Sort?

Insertion Sort works by dividing the array into a sorted and an unsorted part. Initially, the first element is considered sorted, and the rest of the array is unsorted. The algorithm takes each element from the unsorted part and places it into its correct position within the sorted part. This process continues until the entire array is sorted.

Key Steps in Insertion Sort

Start with the First Element:

  • The first element is considered sorted by default.

Pick the Next Element:

  • Take the next element from the unsorted portion of the array and compare it with elements in the sorted portion.

Shift Elements:

  • Shift elements in the sorted portion to the right to make space for the new element.

Insert the Element:

  • Insert the picked element into its correct position in the sorted part.

Repeat:

  • Repeat the process for all elements in the unsorted portion.

Example of Insertion Sort

Let’s sort the array: [12, 11, 13, 5, 6]

  1. Start with 12 (the first element) as the sorted part.
  2. Pick 11 and compare it with 12. Since 11 is smaller, insert 11 before 12.
    • Array after insertion: [11, 12, 13, 5, 6]
  3. Pick 13 and compare it with 12. Since 13 is larger, leave it in place.
    • Array remains: [11, 12, 13, 5, 6]
  4. Pick 5 and compare it with the sorted part [11, 12, 13]. Shift elements to the right and insert 5.
    • Array after insertion: [5, 11, 12, 13, 6]
  5. Pick 6 and insert it into its correct position in the sorted part [5, 11, 12, 13].
    • Final sorted array: [5, 6, 11, 12, 13]

Pseudocode for Insertion Sort

python

Copy code

def insertion_sort(arr):    # Traverse from 1 to len(arr)    for i in range(1, len(arr)):        key = arr[i]        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 key < arr[j]:            arr[j + 1] = arr[j]            j -= 1        arr[j + 1] = key

Key Points:

  • Outer Loop: Moves from the second element to the last element.
  • Inner Loop: Shifts elements of the sorted part to the right to insert the new element in its correct position.

Time Complexity of Insertion Sort

  • Best Case: O(n), which occurs when the array is already sorted.
  • Worst Case: O(n²), when the array is sorted in reverse order.
  • Average Case: O(n²), which occurs when the array elements are randomly ordered.

Insertion Sort is efficient for small arrays and arrays that are nearly sorted.

Space Complexity:

  • The space complexity is O(1) because Insertion Sort is an in-place sorting algorithm, meaning it does not require additional memory for sorting the array.

Advantages of Insertion Sort

Simple and Easy to Implement:

  • Insertion Sort has a simple algorithm that is easy to implement and understand, making it a great introduction to sorting algorithms.

Efficient for Small or Nearly Sorted Arrays:

  • Insertion Sort works well for small datasets or nearly sorted arrays, where it can outperform more complex algorithms like QuickSort.

In-Place Sorting:

  • Insertion Sort sorts the array without needing extra space, making it memory-efficient.

Stable Sorting Algorithm:

  • Insertion Sort is a stable algorithm, meaning it maintains the relative order of elements with equal values.

Disadvantages of Insertion Sort

Inefficient for Large Arrays:

  • Insertion Sort has a time complexity of O(n²) in the worst and average cases, making it inefficient for large datasets compared to algorithms like QuickSort or Merge Sort.

Shifting Elements:

  • In cases where many elements need to be shifted, Insertion Sort can be slow.

Applications of Insertion Sort

Small Data Sets:

  • Insertion Sort is ideal for small datasets due to its simplicity and low overhead.

Nearly Sorted Arrays:

  • Insertion Sort performs well when the input is nearly sorted, as its time complexity is linear in the best case.

Online Sorting:

  • In situations where elements are continuously being added, Insertion Sort can be used to keep a running list sorted. For example, inserting new records into a database in sorted order.

Why Learn Insertion Sort?

Learning Insertion Sort provides a solid foundation in sorting algorithms. While it may not be the most efficient for large datasets, it introduces key concepts like in-place sorting and stable sorting algorithms. Understanding Insertion Sort also prepares you for learning more advanced algorithms such as QuickSort and Merge Sort. Additionally, for small or nearly sorted data, Insertion Sort can often be a very practical and simple solution.

Topics Covered:

Definition of Insertion Sort: Understanding how the algorithm works by inserting elements into the correct position.

Steps of the Algorithm: Iterating through the array and shifting elements to sort them.

Time and Space Complexity: Analysis of Insertion Sort's performance in different cases.

Applications: Practical uses of Insertion Sort for small or nearly sorted datasets.

For more details and further examples, check out the full article on GeeksforGeeks: https://www.geeksforgeeks.org/insertion-sort-algorithm/.