0
0
DSA Javascriptprogramming~10 mins

Insertion Sort Algorithm in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Insertion Sort Algorithm
Start at index 1
Pick key element
Compare with elements to left
Shift elements right if greater
Insert key at correct position
Move to next index
Repeat until end of array
Sorted array
Insertion sort picks each element from the unsorted part and inserts it into the correct position in the sorted part by shifting larger elements to the right.
Execution Sample
DSA Javascript
const arr = [5, 3, 8, 1];
for(let i=1; i<arr.length; i++) {
  let key = arr[i];
  let j = i - 1;
  while(j >= 0 && arr[j] > key) {
    arr[j+1] = arr[j]; j--;
  }
  arr[j+1] = key;
}
Sorts the array [5, 3, 8, 1] step-by-step by inserting each element into its correct position.
Execution Table
StepOperationIndex iKeyIndex jComparisonShift or InsertArray State
1Start outer loop130arr[0]=5 > 3Shift arr[0] to arr[1][5, 5, 8, 1]
2Continue inner loop13-1j < 0, stopInsert key at arr[0][3, 5, 8, 1]
3Start outer loop281arr[1]=5 > 8? NoInsert key at arr[2][3, 5, 8, 1]
4Start outer loop312arr[2]=8 > 1Shift arr[2] to arr[3][3, 5, 8, 8]
5Continue inner loop311arr[1]=5 > 1Shift arr[1] to arr[2][3, 5, 5, 8]
6Continue inner loop310arr[0]=3 > 1Shift arr[0] to arr[1][3, 3, 5, 8]
7Continue inner loop31-1j < 0, stopInsert key at arr[0][1, 3, 5, 8]
8Outer loop ends4----[1, 3, 5, 8]
💡 Outer loop index i reaches array length 4, sorting complete.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6After Step 7Final
iundefined11233334
keyundefined3381111-
jundefined0-11210-1-
arr[5, 3, 8, 1][5, 5, 8, 1][3, 5, 8, 1][3, 5, 8, 1][3, 5, 8, 8][3, 5, 5, 8][3, 3, 5, 8][1, 3, 5, 8][1, 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. Swapping repeatedly would be less efficient. See steps 1, 4-6 in execution_table where shifting happens before inserting the key.
Why does the inner loop stop when j becomes -1?
j becomes -1 when we have compared the key with all elements to the left and found no smaller element. This means the key belongs at the start. See steps 2 and 7 where j < 0 stops the loop.
Why do we insert the key at arr[j+1] after shifting?
After shifting larger elements right, j points to one position left of where the key should go. So we insert at j+1. This is shown in steps 2, 3, and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What happens to the array?
AElement 5 is shifted right to index 3
BThe key 1 is inserted at index 3 without shifts
CElement 8 at index 2 is shifted right to index 3
DNo changes to the array
💡 Hint
Check the 'Shift or Insert' and 'Array State' columns at step 4.
At which step does the inner loop stop because j < 0?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look for 'j < 0, stop' in the 'Comparison' column.
If the initial array was already sorted, how would the 'Shift or Insert' column change?
AThere would be no shifts, only inserts at the same position
BAll elements would be shifted right
CThe key would always be inserted at index 0
DThe array would be reversed
💡 Hint
Consider how insertion sort behaves when elements are already in order.
Concept Snapshot
Insertion Sort Algorithm:
- Start from index 1, pick key element
- Compare key with sorted part to left
- Shift larger elements right
- Insert key at correct position
- Repeat until array end
- Time: O(n^2), best O(n) if sorted
Full Transcript
Insertion sort works by taking one element at a time from the unsorted part of the array and inserting it into the correct position in the sorted part. We start from the second element (index 1) and pick it as the key. We compare the key with elements to its left. If elements are larger, we shift them right to make space. When we find the correct spot or reach the start, we insert the key. This repeats until the whole array is sorted. The execution table shows each step, including comparisons, shifts, and insertions, with the array state after each operation. Key variables tracked are i (outer loop index), j (inner loop index), key (current element), and the array itself. Important points include why shifting is used instead of swapping, why the inner loop stops when j < 0, and why insertion happens at arr[j+1]. The quiz questions test understanding of these steps and the behavior of insertion sort on sorted arrays.