0
0
DSA Goprogramming~10 mins

Selection Sort Algorithm in DSA Go - 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?
NoDone
Back to find min element
Selection sort repeatedly finds the smallest element in the unsorted part and swaps it with the first unsorted element, moving the boundary forward.
Execution Sample
DSA Go
arr := []int{64, 25, 12, 22, 11}
for i := 0; i < len(arr)-1; i++ {
  minIdx := i
  for j := i+1; j < len(arr); j++ {
    if arr[j] < arr[minIdx] {
      minIdx = j
    }
  }
  arr[i], arr[minIdx] = arr[minIdx], arr[i]
}
This code sorts the array by selecting the smallest element from the unsorted part and swapping it to the front.
Execution Table
StepOperationijminIdxArray StateAction
1Initialize i=00-0[64, 25, 12, 22, 11]Start outer loop
2Compare arr[j]=25 < arr[minIdx]=64010[64, 25, 12, 22, 11]Update minIdx=1
3Compare arr[j]=12 < arr[minIdx]=25021[64, 25, 12, 22, 11]Update minIdx=2
4Compare arr[j]=22 < arr[minIdx]=12032[64, 25, 12, 22, 11]No change
5Compare arr[j]=11 < arr[minIdx]=12042[64, 25, 12, 22, 11]Update minIdx=4
6Swap arr[i]=64 with arr[minIdx]=110-4[11, 25, 12, 22, 64]Swap elements at 0 and 4
7Increment i=11-1[11, 25, 12, 22, 64]Start next outer loop
8Compare arr[j]=12 < arr[minIdx]=25121[11, 25, 12, 22, 64]Update minIdx=2
9Compare arr[j]=22 < arr[minIdx]=12132[11, 25, 12, 22, 64]No change
10Compare arr[j]=64 < arr[minIdx]=12142[11, 25, 12, 22, 64]No change
11Swap arr[i]=25 with arr[minIdx]=121-2[11, 12, 25, 22, 64]Swap elements at 1 and 2
12Increment i=22-2[11, 12, 25, 22, 64]Start next outer loop
13Compare arr[j]=22 < arr[minIdx]=25232[11, 12, 25, 22, 64]Update minIdx=3
14Compare arr[j]=64 < arr[minIdx]=22243[11, 12, 25, 22, 64]No change
15Swap arr[i]=25 with arr[minIdx]=222-3[11, 12, 22, 25, 64]Swap elements at 2 and 3
16Increment i=33-3[11, 12, 22, 25, 64]Start next outer loop
17Compare arr[j]=64 < arr[minIdx]=25343[11, 12, 22, 25, 64]No change
18Swap arr[i]=25 with arr[minIdx]=253-3[11, 12, 22, 25, 64]Swap elements at 3 and 3 (no change)
19Increment i=44-4[11, 12, 22, 25, 64]Outer loop ends, array sorted
💡 i reaches 4 which is len(arr)-1, outer loop ends, array is sorted
Variable Tracker
VariableStartAfter Step 6After Step 11After Step 15After Step 18Final
i001234
minIdx042334
Array[64, 25, 12, 22, 11][11, 25, 12, 22, 64][11, 12, 25, 22, 64][11, 12, 22, 25, 64][11, 12, 22, 25, 64][11, 12, 22, 25, 64]
Key Moments - 3 Insights
Why do we update minIdx only when arr[j] < arr[minIdx]?
Because minIdx must always point to the smallest element found so far in the unsorted part. This is shown in steps 2, 3, 5 where minIdx changes only when a smaller element is found.
Why do we swap arr[i] with arr[minIdx] even if minIdx equals i?
Swapping an element with itself does nothing but keeps the algorithm consistent. Step 18 shows this case where swapping arr[3] with arr[3] does not change the array.
Why does the outer loop run only until i < len(arr)-1?
Because after placing the smallest elements up to the second last position, the last element is automatically sorted. The exit note confirms the loop ends at i=4 for array length 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 6, what is the array state after the swap?
A[25, 11, 12, 22, 64]
B[64, 25, 12, 22, 11]
C[11, 25, 12, 22, 64]
D[11, 12, 25, 22, 64]
💡 Hint
Check the 'Array State' column at step 6 in the execution_table
At which step does minIdx first change from 0 to 1?
AStep 3
BStep 2
CStep 5
DStep 4
💡 Hint
Look at the 'minIdx' column in execution_table rows 1 to 5
If the array was already sorted, how would the swaps change?
ASwaps would happen only when minIdx != i
BNo swaps would occur
COnly one swap at the end
DSwaps would still happen at every step
💡 Hint
Refer to step 18 where swap happens even if minIdx == i, but in sorted array swaps happen only if minIdx differs from i
Concept Snapshot
Selection Sort Algorithm:
- Repeatedly find the smallest element in unsorted part
- Swap it with the first unsorted element
- Move boundary forward by one
- Runs in O(n²) time
- In-place and simple to implement
Full Transcript
Selection sort works by dividing the array into sorted and unsorted parts. It starts at the first element and finds the smallest element in the unsorted part. Then it swaps this smallest element with the first unsorted element. This process repeats by moving the boundary forward until the whole array is sorted. The execution table shows each comparison and swap step by step, tracking the index i, the current minimum index minIdx, and the array state after each operation. Key moments clarify why minIdx updates only when a smaller element is found, why swapping with itself is allowed, and why the outer loop stops before the last element. The visual quiz tests understanding of array states and index changes during sorting.