Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
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

AlgorithmSelection Sort
  1. Step 1:
    Start.
  2. Step 2:
    Read the number of elements n.
  3. Step 3:
    Read the n elements into the array A.
  4. Step 4:
    Repeat Steps 5 to 7 for i = 0 to n-2.
  5. Step 5:
    Set min = i.
  6. Step 6:
    Repeat for j = i+1 to n-1:
    • If A[j] < A[min], set min = j.
  7. Step 7:
    Swap A[i] and A[min] (temp = A[i], A[i] = A[min], A[min] = temp).
  8. Step 8:
    Display the sorted array A.
  9. Step 9:
    Stop.
Example run (generated)checked by code
Input29 10 14 37 13
After pass 110 29 14 37 13
After pass 210 13 14 37 29
After pass 310 13 14 37 29
After pass 410 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