B.Tech S3 (S) Examinations May 2026 (2024 Scheme)QP 06PCCST303112504Part A · Q73 marksCO4Module 4
Question 7
Write algorithm to sort an array using selection sort.
Topics: Selection sort
Watch this exact question run
Loading visualizer…
Solution
- Step 1:Start.
- Step 2:Read the number of elements n.
- Step 3:Read the n elements into the array A.
- Step 4:Repeat Steps 5 to 7 for i = 0 to n-2.
- Step 5:Set min = i.
- Step 6:Repeat for j = i+1 to n-1:
- If A[j] < A[min], set min = j.
- Step 7:Swap A[i] and A[min] (temp = A[i], A[i] = A[min], A[min] = temp).
- Step 8:Display the sorted array A.
- Step 9:Stop.
| Input | 29 10 14 37 13 |
|---|---|
| After pass 1 | 10 29 14 37 13 |
| After pass 2 | 10 13 14 37 29 |
| After pass 3 | 10 13 14 37 29 |
| After pass 4 | 10 13 14 29 37 |
Totals: comparisons = 10, swaps = 3
How to write it in the exam
Always n(n − 1)/2 comparisons → O(n²) in every case; at most n − 1 swaps; not stable.
How this answer was verified
- Property tests
- selection_sort.c — exact output tested
- tests/step-algorithms.test.ts — Step algorithm matches the visualizer after every pass