Tutorialspoint
Problem
Solution
Submissions

Insertion Sort

Certification: Advanced Level Accuracy: 0% Submissions: 0 Points: 15

Write a C++ program that implements the insertion sort algorithm. The program should take an unsorted array of integers and sort it in ascending order by building the sorted array one element at a time.

Example 1
  • Input: array = [12, 11, 13, 5, 6]
  • Output: [5, 6, 11, 12, 13]
  • Explanation:
    • Step 1: Start with first element [12] - already sorted
    • Step 2: Insert 11 into sorted subarray
      • Compare 11 with 12. 11 < 12, move 12 one position right
      • Insert 11 at correct position: [11, 12, 13, 5, 6]
    • Step 3: Insert 13 into sorted subarray
      • Compare 13 with 12. 13 > 12, keep 13 at its position
      • Current array: [11, 12, 13, 5, 6]
    • Step 4: Insert 5 into sorted subarray
      • Compare 5 with 13, 12, and 11. 5 < all of them, move them right
      • Insert 5 at beginning: [5, 11, 12, 13, 6]
    • Step 5: Insert 6 into sorted subarray
      • Compare 6 with 13, 12, and 11. 6 < all of them, move them right
      • Compare 6 with 5. 6 > 5, insert after 5: [5, 6, 11, 12, 13]
    • Step 6: Return sorted array [5, 6, 11, 12, 13]
Example 2
  • Input: array = [4, 3, 2, 10, 12, 1, 5, 6]
  • Output: [1, 2, 3, 4, 5, 6, 10, 12]
  • Explanation:
    • Step 1: Start with first element [4] - already sorted
    • Step 2: Insert 3 into sorted subarray
      • Compare 3 with 4. 3 < 4, move 4 right
      • Insert 3 at beginning: [3, 4, 2, 10, 12, 1, 5, 6]
    • Step 3: Continue insertion process for elements 2, 10, 12, 1, 5, 6
    • Step 4: Return sorted array [1, 2, 3, 4, 5, 6, 10, 12]
Constraints
  • 1 ≤ array.length ≤ 10^3
  • -10^6 ≤ array[i] ≤ 10^6
  • Time Complexity: O(n²), where n is the length of the array
  • Space Complexity: O(1)
ArraysAlgorithmsMicrosoftDeloitte
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

  • Start with the second element (index 1) and consider it as the current element to be inserted.
  • Compare the current element with all elements in the sorted part (left of the current element).
  • Shift all elements greater than the current element one position to the right.
  • Insert the current element at its correct position in the sorted part.
  • Repeat for all elements in the array.

Steps to solve by this approach:

 Step 1: Iterate through the array starting from the second element.

 Step 2: For each element, store the current value as a key.
 Step 3: Compare the key with all previous elements.
 Step 4: Shift greater elements one position ahead.
 Step 5: Insert the key in its correct position in the sorted part.
 Step 6: Continue until the entire array is sorted.

Submitted Code :