0
0
DSA C++programming~10 mins

Insertion Sort Algorithm in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Insertion Sort Algorithm
Start with second element i=1
Compare current element with elements before it
Shift elements greater than current to the right
Insert current element at correct position
Increment i
i < array length?
NoSorted array ready
Back to Compare
Insertion sort picks each element from the second one, shifts bigger elements to the right, and inserts it in the correct place, repeating until the array is sorted.
Execution Sample
DSA C++
int arr[] = {5, 3, 8, 1, 2};
int n = 5;
for (int i = 1; i < n; i++) {
  int key = arr[i];
  int j = i - 1;
  while (j >= 0 && arr[j] > key) {
    arr[j + 1] = arr[j];
    j--;
  }
  arr[j + 1] = key;
}
Sorts the array by inserting each element into its correct position among the previously sorted elements.
Execution Table
StepOperationArray StateComparisonShift or InsertVisual State
1Pick element at i=1 (3)[5, 3, 8, 1, 2]5 > 3Shift 5 right[5, 5, 8, 1, 2]
2Insert key 3 at position j+1=0[5, 5, 8, 1, 2]N/AInsert 3[3, 5, 8, 1, 2]
3Pick element at i=2 (8)[3, 5, 8, 1, 2]5 > 8? NoNo shift[3, 5, 8, 1, 2]
4Pick element at i=3 (1)[3, 5, 8, 1, 2]8 > 1Shift 8 right[3, 5, 8, 8, 2]
5Compare 5 > 1[3, 5, 8, 8, 2]5 > 1Shift 5 right[3, 5, 5, 8, 2]
6Compare 3 > 1[3, 5, 5, 8, 2]3 > 1Shift 3 right[3, 3, 5, 8, 2]
7Insert key 1 at position j+1=0[3, 3, 5, 8, 2]N/AInsert 1[1, 3, 5, 8, 2]
8Pick element at i=4 (2)[1, 3, 5, 8, 2]8 > 2Shift 8 right[1, 3, 5, 8, 8]
9Compare 5 > 2[1, 3, 5, 8, 8]5 > 2Shift 5 right[1, 3, 5, 5, 8]
10Compare 3 > 2[1, 3, 5, 5, 8]3 > 2Shift 3 right[1, 3, 3, 5, 8]
11Compare 1 > 2? No[1, 3, 3, 5, 8]1 > 2? NoNo shift[1, 3, 3, 5, 8]
12Insert key 2 at position j+1=1[1, 3, 3, 5, 8]N/AInsert 2[1, 2, 3, 5, 8]
13i=5 equals array length, sorting done[1, 2, 3, 5, 8]N/ADone[1, 2, 3, 5, 8]
💡 i reaches array length 5, no more elements to insert, array is sorted
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9After Step 10After Step 11After Step 12Final
i11123333444445
key3338111122222N/A
j0-1-1110-1-121000N/A
Array[5, 3, 8, 1, 2][5, 5, 8, 1, 2][3, 5, 8, 1, 2][3, 5, 8, 1, 2][3, 5, 8, 8, 2][3, 5, 5, 8, 2][3, 3, 5, 8, 2][1, 3, 5, 8, 2][1, 3, 5, 8, 8][1, 3, 5, 5, 8][1, 3, 3, 5, 8][1, 3, 3, 5, 8][1, 2, 3, 5, 8][1, 2, 3, 5, 8]
Key Moments - 3 Insights
Why do we shift elements to the right instead of swapping directly?
We shift elements to the right to make space for the key element to be inserted at the correct position. This is shown in steps 1, 4, 5, 6, 8, 9, and 10 where elements are moved right before inserting the key.
Why does the inner loop stop when arr[j] <= key?
The inner loop stops because the correct position for the key is found when the element at j is less than or equal to the key. This is shown in step 11 where 1 > 2 is false, so no more shifting happens.
What happens when the key is already greater than the previous element?
No shifting is needed and the key stays in place. This is shown in step 3 where 5 > 8 is false, so the array remains unchanged.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the array state after step 7?
A[1, 3, 5, 8, 2]
B[3, 3, 5, 8, 2]
C[3, 5, 8, 1, 2]
D[1, 2, 3, 5, 8]
💡 Hint
Check the 'Array State' column in row with Step 7 in execution_table.
At which step does the condition 'arr[j] > key' become false for the first time?
AStep 6
BStep 11
CStep 3
DStep 9
💡 Hint
Look at the 'Comparison' column in execution_table rows to find where 'arr[j] > key' is false.
If the initial array was already sorted, how would the 'Shift or Insert' column change?
AAll shifts would be replaced by inserts
BNo shifts would occur, only inserts at the same position
CShifts would increase because elements are moved more
DThe array would be reversed
💡 Hint
Refer to variable_tracker and execution_table to see when shifts happen and when they don't.
Concept Snapshot
Insertion Sort Algorithm:
- Start from second element (index 1)
- Compare with previous elements
- Shift larger elements right
- Insert current element at correct position
- Repeat until array is sorted
- Time: O(n^2), Space: O(1)
Full Transcript
Insertion sort works by taking each element from the second position onward and inserting it into the correct place among the elements before it. We compare the current element (key) with elements to its left. If those elements are bigger, we shift them one position to the right to make space. When we find the right spot, we insert the key. This repeats until the whole array is sorted. The execution table shows each step with array states, comparisons, shifts, and insertions. Variables i, j, and key track the current positions and values. Key moments clarify why shifting is done instead of swapping, when the inner loop stops, and what happens if the array is already sorted. The visual quiz tests understanding of array states and conditions during sorting. Insertion sort is simple but can be slow for large arrays.