0
0
DSA C++programming~10 mins

Selection Sort Algorithm in DSA C++ - 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
Is i < array length - 1?
NoSorted array, Done
Back to find min element
Selection sort repeatedly finds the smallest element in the unsorted part and swaps it to the front, moving the boundary of sorted and unsorted parts forward.
Execution Sample
DSA C++
int arr[] = {64, 25, 12, 22, 11};
int n = 5;
for (int i = 0; i < n-1; i++) {
  int min_idx = i;
  for (int j = i+1; j < n; j++) {
    if (arr[j] < arr[min_idx]) min_idx = j;
  }
  std::swap(arr[min_idx], arr[i]);
}
Sorts the array by selecting the minimum element from the unsorted part and swapping it with the first unsorted element.
Execution Table
StepOperationijmin_idxArray StateAction
1Initialize i=00-0[64, 25, 12, 22, 11]Start outer loop
2Compare arr[j] with arr[min_idx]010[64, 25, 12, 22, 11]25 < 64? Yes, min_idx=1
3Compare arr[j] with arr[min_idx]021[64, 25, 12, 22, 11]12 < 25? Yes, min_idx=2
4Compare arr[j] with arr[min_idx]032[64, 25, 12, 22, 11]22 < 12? No, min_idx=2
5Compare arr[j] with arr[min_idx]042[64, 25, 12, 22, 11]11 < 12? Yes, min_idx=4
6Swap arr[min_idx] and arr[i]0-4[11, 25, 12, 22, 64]Swap 64 and 11
7Increment i=11-1[11, 25, 12, 22, 64]Start next outer loop
8Compare arr[j] with arr[min_idx]121[11, 25, 12, 22, 64]12 < 25? Yes, min_idx=2
9Compare arr[j] with arr[min_idx]132[11, 25, 12, 22, 64]22 < 12? No, min_idx=2
10Compare arr[j] with arr[min_idx]142[11, 25, 12, 22, 64]64 < 12? No, min_idx=2
11Swap arr[min_idx] and arr[i]1-2[11, 12, 25, 22, 64]Swap 25 and 12
12Increment i=22-2[11, 12, 25, 22, 64]Start next outer loop
13Compare arr[j] with arr[min_idx]232[11, 12, 25, 22, 64]22 < 25? Yes, min_idx=3
14Compare arr[j] with arr[min_idx]243[11, 12, 25, 22, 64]64 < 22? No, min_idx=3
15Swap arr[min_idx] and arr[i]2-3[11, 12, 22, 25, 64]Swap 25 and 22
16Increment i=33-3[11, 12, 22, 25, 64]Start next outer loop
17Compare arr[j] with arr[min_idx]343[11, 12, 22, 25, 64]64 < 25? No, min_idx=3
18Swap arr[min_idx] and arr[i]3-3[11, 12, 22, 25, 64]Swap 25 and 25 (no change)
19Increment i=44-4[11, 12, 22, 25, 64]Outer loop ends, array sorted
💡 i reaches 4, condition i < n-1 (4 < 4) is False, sorting complete
Variable Tracker
VariableStartAfter Step 6After Step 11After Step 15After Step 18Final
i001234
min_idx042334
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 swap after finding the minimum element instead of swapping every time we find a smaller element?
We only swap once per outer loop after finding the minimum to reduce unnecessary swaps. See steps 2-5 where min_idx updates but swap happens only at step 6.
Why does the inner loop start from j = i + 1 instead of j = 0?
Because elements before i are already sorted, so we only search for the minimum in the unsorted part. This is shown in the execution_table where j starts from i+1 each time.
What happens when min_idx equals i during swap?
Swapping an element with itself does nothing, but the algorithm still performs the swap step for consistency, as seen in step 18.
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 the outer loop variable i become 2?
AStep 7
BStep 12
CStep 11
DStep 15
💡 Hint
Look at the 'i' column in the execution_table and find when i changes to 2.
If the array was already sorted, how would the number of swaps change?
ASwaps would stay the same
BSwaps would decrease to zero
CSwaps would increase
DSwaps would be random
💡 Hint
Selection sort always swaps once per outer loop even if the minimum is at the current position, see step 18.
Concept Snapshot
Selection Sort Algorithm:
- Repeatedly find the minimum element from unsorted part
- Swap it with the first unsorted element
- Outer loop runs from i=0 to n-2
- Inner loop finds min_idx from i+1 to end
- Swaps happen once per outer loop
- Time complexity: O(n²), in-place sorting
Full Transcript
Selection sort works by dividing the array into sorted and unsorted parts. It starts at the beginning, finds the smallest element in the unsorted part, and swaps it with the first unsorted element. This process repeats, moving the boundary of sorted elements forward until the whole array is sorted. The inner loop finds the minimum element index, and the outer loop swaps it into place. Even if the minimum is already at the current position, a swap is performed for consistency. The algorithm runs in O(n squared) time and sorts the array in place.