Insertion sort

  • Computer Science
  • insertion sort algorithm
  • insertion sort time…
  • insertion sort space…
  • insertion sort advantages
  • insertion sort disadvantages
  • insertion sort pseudocode

Podcast

Triedenie vkladaním: Ako si usporiadať karty0:00 / 11:34
0:001:00 remaining

Demystifying Insertion Sort: A Simple Yet Powerful Sorting Algorithm

Insertion sort is a straightforward and intuitive sorting algorithm that builds a final sorted array one item at a time. It's particularly effective for small data sets or for lists that are already mostly sorted. Understanding its mechanics is fundamental for any student delving into algorithms, offering insights into efficiency and practical applications. Its fundamental principle can be easily understood by imagining how one might sort a hand of playing cards.

What is Insertion Sort and How Does it Work?

Imagine you're sorting a hand of playing cards. You typically start with an empty left hand and the cards to be sorted in your right hand. You pick one card at a time from your right hand. For each card you pick, you compare it with the cards already in your left hand (which are always kept sorted).

You then insert the new card into its correct position among the sorted cards, shifting any larger cards to the right to make space. This process repeats until all cards from your right hand have been moved to your left, resulting in a fully sorted hand. This card sorting analogy perfectly illustrates the core mechanism of insertion sort.

Insertion sort applies this exact logic to an array of elements. It divides the array into two conceptual parts: a sorted sub-array and an unsorted sub-array. Initially, the first element of the array is considered sorted (as a single element is always sorted).

How Insertion Sort Works: A Step-by-Step Guide

The algorithm then iteratively takes elements from the unsorted part and inserts them into their correct position within the sorted part. Here's a breakdown:

  1. Initialization: The first element of the array is considered sorted. The remaining elements form the unsorted part.
  2. Iteration: For each element in the unsorted part (starting from the second element of the array, at index 1): a. Select Key: Take the current element from the unsorted part. This element is often referred to as the 'key'. b. Comparison and Shifting: Compare the key with elements in the sorted sub-array, moving from right to left (from the element just before the key, backwards towards the beginning of the array). c. Find Position: If an element in the sorted sub-array is greater than the key, shift it one position to the right to make space for the key. d. Insertion: Continue shifting elements until an element smaller than or equal to the key is found, or the beginning of the array is reached. Insert the key into the position immediately after the smaller element (or at the beginning if no smaller element was found).
  3. Completion: Repeat step 2 until all elements from the original array have been moved into the sorted sub-array.

Insertion Sort Example: Sorting an Array

Let's sort the array [5, 2, 4, 6, 1, 3] using insertion sort to see it in action.

Initial Array: [5, 2, 4, 6, 1, 3]

  • Step 1: Consider 5 as sorted.
  • Sorted part: [5]
  • Unsorted part: [2, 4, 6, 1, 3]
  • Array state: [5 | 2, 4, 6, 1, 3] (The vertical bar separates sorted from unsorted)
  • Step 2: Take 2 (key = 2).
  • Compare 2 with 5. Since 2 < 5, shift 5 to the right.
  • Array state: [ _, 5 | 4, 6, 1, 3]
  • Insert 2 into the empty spot. Sorted part: [2, 5]
  • Array state: [2, 5 | 4, 6, 1, 3]
  • Step 3: Take 4 (key = 4).
  • Compare 4 with 5. Since 4 < 5, shift 5 to the right.
  • Array state: [2, _, 5 | 6, 1, 3]
  • Compare 4 with 2. Since 4 > 2, stop shifting.
  • Insert 4 into the empty spot. Sorted part: [2, 4, 5]
  • Array state: [2, 4, 5 | 6, 1, 3]
  • Step 4: Take 6 (key = 6).
  • Compare 6 with 5. Since 6 > 5, stop shifting.
  • Insert 6 after 5 (it's already in place). Sorted part: [2, 4, 5, 6]
  • Array state: [2, 4, 5, 6 | 1, 3]
  • Step 5: Take 1 (key = 1).
  • Compare 1 with 6. Shift 6.
  • Compare 1 with 5. Shift 5.
  • Compare 1 with 4. Shift 4.
  • Compare 1 with 2. Shift 2.
  • Array state: [ _, 2, 4, 5, 6 | 3]
  • Insert 1 into the empty spot. Sorted part: [1, 2, 4, 5, 6]
  • Array state: [1, 2, 4, 5, 6 | 3]
  • Step 6: Take 3 (key = 3).
  • Compare 3 with 6. Shift 6.
  • Compare 3 with 5. Shift 5.
  • Compare 3 with 4. Shift 4.
  • Compare 3 with 2. Since 3 > 2, stop shifting.
  • Array state: [1, 2, _, 4, 5, 6 | ]
  • Insert 3 into the empty spot. Sorted part: [1, 2, 3, 4, 5, 6]
  • Array state: [1, 2, 3, 4, 5, 6 | ]

The array is now fully sorted: [1, 2, 3, 4, 5, 6].

Insertion Sort Pseudocode Explained

Understanding the pseudocode provides a clear, concise view of the algorithm's logic:

function insertionSort(array A, n):
 for i from 1 to n-1:
 key = A[i]
 j = i - 1

 while j >= 0 and A[j] > key:
 A[j + 1] = A[j] // Shift elements to the right
 j = j - 1
 A[j + 1] = key // Insert the key into its correct position

In this pseudocode:

  • A represents the array to be sorted.
  • n is the number of elements in the array.
  • The outer for loop iterates from the second element (i = 1) up to the last element (n-1). This loop picks each element from the unsorted part.
  • key stores the current element to be inserted into the sorted sub-array.
  • The inner while loop handles the comparison and shifting process, moving elements larger than key one position to the right to make space.
  • Finally, key is placed in its correct position within the sorted sub-array.

Key Properties of Insertion Sort Algorithm

Insertion sort possesses several important characteristics that define its behavior and suitability for various tasks:

  1. In-place: Insertion sort is an in-place algorithm. This means it sorts the array by modifying the original array directly, requiring only a constant amount of additional memory space, typically O(1) auxiliary space, for temporary storage of the 'key' element during each insertion.
  2. Stable: Insertion sort is a stable sorting algorithm. If two elements have equal values, their relative order in the input array is preserved in the sorted output. For example, if you have [(A, 5), (B, 5), (C, 2)] and sort by the number, a stable sort would result in [(C, 2), (A, 5), (B, 5)], maintaining 'A' before 'B'. This property is crucial when elements have associated data and their original order matters for equal keys.
  3. Adaptive: Insertion sort is adaptive. Its performance improves for data sets that are already partially sorted. For nearly sorted arrays, it performs exceptionally well, approaching linear time complexity.
  4. Online: Insertion sort is an online algorithm. It can sort a list as it receives elements, meaning it does not need the entire input data to be available before it can start sorting. This makes it suitable for situations where data arrives sequentially.

Insertion Sort Time Complexity Analysis

Time complexity describes how the running time of an algorithm grows with the size of the input. For insertion sort, we analyze the number of comparisons and shifts performed. Let n be the number of elements in the array.

  • Best Case: O(n)
  • This occurs when the array is already sorted in ascending order (e.g., [1, 2, 3, 4, 5]). For each element from the second one onwards, only one comparison is needed (with the element immediately to its left) to confirm it is in the correct position. No elements need to be shifted. The total number of comparisons is approximately n - 1, which is proportional to n. Hence, the best-case time complexity is linear, O(n).
  • Average Case: O(n²)
  • On average, for a randomly ordered array, each element will need to be shifted about halfway through the sorted sub-array. This still results in a quadratic number of operations, similar to the worst case. Therefore, the average-case time complexity is also O(n²).
  • Worst Case: O(n²)
  • This occurs when the array is sorted in reverse order (e.g., [5, 4, 3, 2, 1]). In this case, each element (from the second one) must be compared with all elements in the sorted sub-array and shifted to the very beginning. The number of comparisons and shifts for the i-th element is i - 1. Summing this for all elements from i = 1 to n - 1 gives 0 + 1 + 2 +... + (n - 1) = n(n - 1) / 2. This is approximately n²/2 operations, which is proportional to n². Thus, the worst-case time complexity is quadratic, O(n²).
  • Nearly Sorted Data: O(n)
  • A significant advantage of insertion sort is its efficiency with nearly sorted data. If an array is only slightly out of order, meaning each element is not far from its final sorted position, the number of shifts and comparisons required for each insertion becomes very small. In such cases, the algorithm approaches linear time complexity, O(n), making it a good choice for maintaining sorted lists where new elements are frequently added or for sorting data that is expected to be mostly sorted.

Space Complexity of Insertion Sort

Insertion sort has a space complexity of O(1). This means it requires a constant amount of extra memory, regardless of the input size n. It only uses a few temporary variables to hold the current key and manage indices during the sorting process. This makes it very memory-efficient, especially for systems with limited memory resources.

Advantages and Disadvantages of Insertion Sort

Like all algorithms, insertion sort has its strengths and weaknesses:

Advantages:

  • Simplicity: It's easy to understand and implement, making it a great starting point for learning sorting algorithms.
  • Efficiency for Small Data Sets: It performs well for small arrays, where the overhead of more complex algorithms isn't justified.
  • Efficiency for Nearly Sorted Data: As an adaptive algorithm, it is very efficient for arrays that are already partially sorted, approaching O(n) performance.
  • In-place: Requires minimal additional memory (O(1) auxiliary space), which is beneficial for memory-constrained environments.
  • Stable: Preserves the relative order of equal elements, which is important in specific applications.
  • Online: Can sort a list as elements are received, allowing for dynamic sorting.

Disadvantages:

  • Inefficiency for Large, Unsorted Data Sets: Its O(n²) average and worst-case time complexity makes it impractical for very large arrays compared to more advanced algorithms like Merge Sort or Quick Sort (which have O(n log n) complexity).
  • Number of Swaps/Shifts: It can involve a large number of element shifts in the worst case, which can be slow for arrays where elements are large or expensive to move (e.g., objects with many properties).

Common Misconceptions About Insertion Sort for Students

Students often encounter certain pitfalls or misunderstandings when learning about insertion sort:

  1. Confusing with Selection Sort: While both are simple, in-place sorts, insertion sort places the current element into its correct position within the already sorted part. In contrast, selection sort finds the smallest remaining element and swaps it into the next available position in the sorted part.
  2. Underestimating O(n²): Students sometimes underestimate how quickly O(n²) performance degrades for large n. An algorithm that takes 1 second for 1,000 elements might take 100 seconds for 10,000 elements, and 10,000 seconds (nearly 3 hours) for 100,000 elements if its complexity is O(n²). The quadratic growth quickly becomes a bottleneck.
  3. Ignoring Stability: The stability property is often overlooked but is important in practical applications where elements might have secondary sorting criteria or associated data. For example, if you sort a list of students by grade, and then by name, a stable sort preserves the original order of students with the same grade.
  4. Not Recognizing Adaptive Behavior: Failing to appreciate that insertion sort can be very fast (O(n)) for nearly sorted data, which makes it a good choice for certain real-world scenarios or as a component in hybrid sorting algorithms (like Timsort).

Observing Insertion Sort in a Simulation

An interactive simulation of insertion sort allows for a dynamic understanding of its mechanics. When using such a simulation, pay close attention to the following aspects:

  • Stepping through Key Insertion: The simulation will typically highlight the 'key' element being considered in each iteration. Observe how this key is compared with elements in the already sorted portion of the array. You will see elements in the sorted part shifting to the right to create a gap for the key.
  • Visualizing Sorted vs. Unsorted Parts: The simulation often visually distinguishes between the sorted and unsorted sections of the array. Notice how the sorted section grows by one element in each main iteration.
  • Best-Case Scenario: Try running the simulation with an already sorted array (e.g., [1, 2, 3, 4, 5]). Observe how few comparisons and no shifts occur, demonstrating the O(n) efficiency.
  • Worst-Case Scenario: Run the simulation with a reverse-sorted array (e.g., [5, 4, 3, 2, 1]). You will see the maximum number of comparisons and shifts as each key has to move all the way to the beginning of the sorted sub-array, illustrating the O(n²) behavior.
  • Nearly Sorted Data: Experiment with an array that has just a few elements out of place (e.g., [1, 3, 2, 4, 5]). Notice that while there are some shifts, they are localized, and the algorithm quickly finds the correct position for the key, reflecting its adaptive nature and near O(n) performance in such cases.
  • Stability Observation: If the simulation allows for elements with duplicate values (e.g., [(A, 5), (B, 2), (C, 5)]), observe that the relative order of equal elements (e.g., A before C) is maintained after sorting. This confirms the algorithm's stability.

By actively stepping through these scenarios in a simulation, you can gain a deeper, intuitive understanding of insertion sort's operational details, its efficiency characteristics, and why it behaves differently under various input conditions.

Frequently Asked Questions about Insertion Sort

What are the main advantages of insertion sort?

The main advantages of insertion sort include its simplicity, efficiency for small data sets, excellent performance with nearly sorted data (O(n)), minimal additional memory requirements (O(1) in-place), its stability (preserving relative order of equal elements), and its online capability, meaning it can sort data as it arrives.

When should I use insertion sort?

Insertion sort is an ideal choice for small arrays, data sets that are already mostly sorted, or when memory usage is a critical concern. It's also suitable for online sorting where elements are received one by one and need to be kept sorted continuously.

Is insertion sort stable? What does that mean?

Yes, insertion sort is a stable sorting algorithm. This means that if there are two elements with the same value, their original relative order in the input array will be preserved in the sorted output array. For example, if you have (item A, 7) and (item B, 7), after sorting, item A will still appear before item B if it did originally.

Why is insertion sort considered adaptive?

Insertion sort is adaptive because its performance improves significantly when the input array is already partially or nearly sorted. In such cases, it performs fewer comparisons and shifts, approaching a linear time complexity of O(n) rather than its typical O(n²) worst-case performance.

What is the space complexity of insertion sort?

The space complexity of insertion sort is O(1). This means it uses a constant amount of extra memory, regardless of the size of the input array. It only needs a few temporary variables to perform its operations, making it a very memory-efficient sorting algorithm.

Check what you already know

Test · 20 questions · first 3 free

Question 1 of 30%

During the insertion sort process, if an element in the sorted sub-array is greater than the key, it is shifted one position to the right to make space for the key.

Practise it with flashcards

18 cards · first 3 free, no account

1 / 18

What kind of sorting algorithm is insertion sort?

Insertion sort is a straightforward and intuitive sorting algorithm that builds a final sorted array one item at a time.

Tap to flip · Swipe to navigate

Practise it, don't just read it

18 flashcards, a test with explanations and a podcast for this topic. The account is free and you pick up right where you left off.

  • No credit card
  • Web and mobile app
  • 330,000+ students
Start for free

Related topics