In C programming, how does the selection sort algorithm arrange an array of n elements in ascending order?

Difficulty: Easy

Correct Answer: On each pass it scans the unsorted part of the array, selects the smallest element, and swaps it with the first element of the unsorted part

Explanation:


Introduction / Context:
Selection sort is one of the simplest comparison based sorting algorithms taught in introductory C programming and data structures courses. Even though it is not efficient for large inputs, it is an excellent example for understanding how nested loops and simple selection logic can be used to reorder an array into ascending order.


Given Data / Assumptions:

  • We have an array of n elements stored in contiguous memory.
  • We want to sort the array into ascending order using selection sort.
  • We are interested in the high level idea of the algorithm, not in the exact C code.


Concept / Approach:
The core idea of selection sort is to repeatedly select the smallest element from the unsorted portion of the array and move it to the front of that unsorted region. At the beginning, the entire array is considered unsorted. On the first pass, the algorithm scans all elements, finds the minimum, and swaps it into index 0. On the second pass, it scans from index 1 to n - 1, finds the smallest element in that subarray, and swaps it into index 1. This continues until the unsorted region shrinks to size one, at which point the array is fully sorted.


Step-by-Step Solution:
Step 1: Initialize an outer loop index i from 0 to n - 2 that marks the boundary between sorted and unsorted parts.Step 2: For each i, assume the minimum is at position i, then run an inner loop j from i + 1 to n - 1 to search for a smaller element.Step 3: If a smaller element is found at position minIndex, update minIndex.Step 4: After the inner loop completes, swap the element at i with the element at minIndex.Step 5: Repeat until the unsorted part is exhausted and the array becomes fully sorted.


Verification / Alternative check:
If you manually trace selection sort on a small array, such as [29, 10, 14, 37], the first pass selects 10 and moves it to the front, giving [10, 29, 14, 37]. The second pass selects 14 from the remaining elements and swaps it into the second position. This behaviour matches the description in the correct option.


Why Other Options Are Wrong:
Option B describes bubble sort, which uses repeated adjacent swaps to move the largest element to the end.Option C describes quick sort, which partitions around a pivot.Option D describes insertion sort, which inserts each element into an already sorted prefix.


Common Pitfalls:
Many learners confuse selection sort with bubble sort because both algorithms use nested loops. The main difference is that selection sort explicitly searches for the minimum element and performs at most one swap per pass, while bubble sort may perform many swaps in each pass. Remembering this helps distinguish exam options.


Final Answer:
The correct description is On each pass it scans the unsorted part of the array, selects the smallest element, and swaps it with the first element of the unsorted part.

More Questions from Programming

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion