0
0
DSA Javascriptprogramming~10 mins

Selection Sort Algorithm in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Selection Sort Algorithm
Start with unsorted array
Set i = 0 (start index)
Find min element from i to end
Swap min element with element at i
Increment i
i < array length - 1?
NoSorted array ready
Back to find min element
Selection sort repeatedly finds the smallest element from the unsorted part and swaps it to the front, moving the boundary of sorted and unsorted parts forward.
Execution Sample
DSA Javascript
const arr = [5, 3, 8, 1, 2];
for(let i=0; i < arr.length - 1; i++) {
  let minIndex = i;
  for(let j=i+1; j < arr.length; j++) {
    if(arr[j] < arr[minIndex]) minIndex = j;
  }
  [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
}
Sorts the array by selecting the smallest element in each pass and swapping it to the front.
Execution Table
StepOperationCurrent iCurrent jMin IndexArray StateSwap Occurred
1Initialize i=00-0[5, 3, 8, 1, 2]No
2Compare arr[j]=3 < arr[minIndex]=5011[5, 3, 8, 1, 2]No
3Compare arr[j]=8 < arr[minIndex]=3021[5, 3, 8, 1, 2]No
4Compare arr[j]=1 < arr[minIndex]=3033[5, 3, 8, 1, 2]No
5Compare arr[j]=2 < arr[minIndex]=1043[5, 3, 8, 1, 2]No
6Swap arr[i]=5 with arr[minIndex]=10-3[1, 3, 8, 5, 2]Yes
7Increment i=11-1[1, 3, 8, 5, 2]No
8Compare arr[j]=8 < arr[minIndex]=3121[1, 3, 8, 5, 2]No
9Compare arr[j]=5 < arr[minIndex]=3131[1, 3, 8, 5, 2]No
10Compare arr[j]=2 < arr[minIndex]=3144[1, 3, 8, 5, 2]No
11Swap arr[i]=3 with arr[minIndex]=21-4[1, 2, 8, 5, 3]Yes
12Increment i=22-2[1, 2, 8, 5, 3]No
13Compare arr[j]=5 < arr[minIndex]=8233[1, 2, 8, 5, 3]No
14Compare arr[j]=3 < arr[minIndex]=5244[1, 2, 8, 5, 3]No
15Swap arr[i]=8 with arr[minIndex]=32-4[1, 2, 3, 5, 8]Yes
16Increment i=33-3[1, 2, 3, 5, 8]No
17Compare arr[j]=8 < arr[minIndex]=5343[1, 2, 3, 5, 8]No
18Swap arr[i]=5 with arr[minIndex]=53-3[1, 2, 3, 5, 8]No
19Increment i=44-4[1, 2, 3, 5, 8]No
20i=4 equals arr.length-1=4, sorting done4--[1, 2, 3, 5, 8]No
💡 i reaches 4 which is arr.length - 1, so sorting is complete
Variable Tracker
VariableStartAfter Step 6After Step 11After Step 15After Step 18Final
i012344
minIndex034433
Array[5, 3, 8, 1, 2][1, 3, 8, 5, 2][1, 2, 8, 5, 3][1, 2, 3, 5, 8][1, 2, 3, 5, 8][1, 2, 3, 5, 8]
Key Moments - 3 Insights
Why do we swap even if minIndex is the same as i?
In step 18, swapping arr[i] with itself does nothing but keeps the algorithm consistent; no change occurs as shown in the execution_table row 18.
Why does the inner loop start from j = i + 1?
Because elements before i are already sorted, so we only look for the minimum in the unsorted part, as shown in execution_table steps 2-5 for i=0.
When does the sorting stop?
Sorting stops when i reaches arr.length - 1, because the last element is then in place, as shown in execution_table step 20.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 6, what is the array state after the swap?
A[1, 3, 8, 5, 2]
B[5, 3, 8, 1, 2]
C[1, 2, 8, 5, 3]
D[3, 5, 8, 1, 2]
💡 Hint
Check the 'Array State' column at step 6 in the execution_table.
At which step does the minIndex first change from 0 to another value?
AStep 4
BStep 2
CStep 3
DStep 5
💡 Hint
Look at the 'Min Index' column changes in the first few steps in execution_table.
If the array was already sorted, how many swaps would occur?
ANone
BOne per iteration
CSwaps only when minIndex != i
DSwaps every step
💡 Hint
Refer to the 'Swap Occurred' column and note when swaps happen in execution_table.
Concept Snapshot
Selection Sort Algorithm:
- Loop i from 0 to length-2
- Find min element index from i to end
- Swap min element with element at i
- Repeat until array sorted
- Time: O(n²), Space: O(1)
- In-place, unstable sort
Full Transcript
Selection sort works by dividing the array into a sorted and unsorted part. It repeatedly finds the smallest element in the unsorted part and swaps it with the first unsorted element, moving the boundary forward. The process continues until the entire array is sorted. The algorithm uses two loops: the outer loop moves the boundary, and the inner loop finds the minimum element. Swapping occurs even if the minimum element is at the current position, but this has no effect. Sorting stops when the outer loop reaches the last element. The time complexity is quadratic, and it sorts the array in place without extra space.