Tutorialspoint
Problem
Solution
Submissions

Insertion Sort Algorithm

Certification: Intermediate Level Accuracy: 100% Submissions: 1 Points: 10

Write a Python program that implements the Insertion Sort algorithm to sort a list of integers in ascending order. Insertion Sort builds the final sorted array one item at a time, taking one element from the input data in each iteration and inserting it into its correct position in the already sorted part of the array.

Example 1
  • Input: [12, 11, 13, 5, 6]
  • Output: [5, 6, 11, 12, 13]
  • Explanation:
    • Step 1: Take the input array [12, 11, 13, 5, 6].
    • Step 2: Start with [12] as the sorted portion and [11, 13, 5, 6] as unsorted.
    • Step 3: Take 11, compare with sorted elements, and insert: [11, 12] | [13, 5, 6].
    • Step 4: Take 13, compare and insert: [11, 12, 13] | [5, 6].
    • Step 5: Take 5, compare and insert: [5, 11, 12, 13] | [6].
    • Step 6: Take 6, compare and insert: [5, 6, 11, 12, 13].
    • Step 7: Return the sorted array [5, 6, 11, 12, 13].
Example 2
  • Input: [64, 34, 25, 12, 22, 11, 90]
  • Output: [11, 12, 22, 25, 34, 64, 90]
  • Explanation:
    • Step 1: Take the input array [64, 34, 25, 12, 22, 11, 90].
    • Step 2: Start with [64] as the sorted portion and [34, 25, 12, 22, 11, 90] as unsorted.
    • Step 3: Take 34, compare with sorted elements, and insert: [34, 64] | [25, 12, 22, 11, 90].
    • Step 4: Take 25, compare and insert: [25, 34, 64] | [12, 22, 11, 90].
    • Step 5: Continue this process for all elements.
    • Step 6: Return the sorted array [11, 12, 22, 25, 34, 64, 90].
Constraints
  • 1 ≤ len(arr) ≤ 10^3
  • -10^6 ≤ arr[i] ≤ 10^6
  • Time Complexity: O(n²), where n is the length of the array
  • Space Complexity: O(1)
ArraysNumberControl StructuresIBMHCL Technologies
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Iterate through the array starting from the second element
  • For each element, compare it with all elements in the sorted part of the array
  • Shift all elements greater than the current element one position ahead
  • Insert the current element at its correct position in the sorted part
  • The sorted part of the array grows with each iteration
  • Optimize by using binary search to find the insertion position
  • For small arrays or nearly sorted arrays, Insertion Sort can be very efficient

Steps to solve by this approach:

 Step 1: Create a copy of the input array to avoid modifying the original.
 Step 2: Start from the second element (index 1) and iterate through the array.
 Step 3: For each element, store its value in a variable 'key'.
 Step 4: Compare key with all elements in the sorted portion (0 to i-1).
 Step 5: Shift elements that are greater than key one position ahead.
 Step 6: Insert the key at its correct position in the sorted sequence.
 Step 7: Return the sorted array.

Submitted Code :