Sort the following number using selection sort and give the required steps.


Q.) Sort the following number using selection sort and give the required steps.

Subject: data structure

Selection sort is a simple comparison-based sorting algorithm. It works by repeatedly selecting the smallest (or largest, depending on the sorting order) element from the unsorted section and moving it to the beginning of the list. The algorithm maintains two sublists in a given array:

  1. The sublist which is already sorted.
  2. The remaining sublist which is unsorted.

In every iteration of selection sort, the minimum element from the unsorted sublist is picked and moved to the sorted sublist.

Here are the steps for sorting an array using selection sort:

  1. Start with the first element of the array as the initial 'minimum' (for ascending order).
  2. Compare this 'minimum' with the second element.
  3. If the second element is smaller than the 'minimum', assign this element as the new 'minimum'.
  4. Continue this process for all elements of the unsorted portion of the array to find the overall minimum.
  5. After completing the iteration, swap the 'minimum' with the first element of the unsorted portion of the array.
  6. Move the boundary of the unsorted portion by one element to the right.
  7. Repeat the process until the entire array is sorted.

Let's apply these steps to the given numbers:

Unsorted array: [29, 10, 14, 37, 13]

Iteration Current Array State Minimum Value Swap Elements
1 [29, 10, 14, 37, 13] 10 Swap 29 with 10
2 [10, 29, 14, 37, 13] 13 Swap 29 with 13
3 [10, 13, 14, 37, 29] 14 No swap needed (already in place)
4 [10, 13, 14, 37, 29] 29 Swap 37 with 29
5 [10, 13, 14, 29, 37] - No swap needed (last element)

Detailed steps:

Iteration 1:

  • Initial array: [29, 10, 14, 37, 13]
  • Minimum value is 29.
  • Compare with next element (10), which is smaller, so now minimum is 10.
  • Continue comparing with the rest: 14, 37, and 13. The minimum remains 10.
  • Swap the minimum (10) with the first element (29).
  • Resulting array: [10, 29, 14, 37, 13]

Iteration 2:

  • Consider the subarray from the second element: [29, 14, 37, 13]
  • Minimum value is 29.
  • Compare with next elements: 14, 37, and 13. The minimum is now 13.
  • Swap the minimum (13) with the first element of the subarray (29).
  • Resulting array: [10, 13, 14, 37, 29]

Iteration 3:

  • Consider the subarray from the third element: [14, 37, 29]
  • Minimum value is 14.
  • Compare with next elements: 37 and 29. The minimum remains 14.
  • No swap needed as 14 is already in its correct position.
  • Resulting array: [10, 13, 14, 37, 29]

Iteration 4:

  • Consider the subarray from the fourth element: [37, 29]
  • Minimum value is 37.
  • Compare with next element: 29, which is smaller, so now minimum is 29.
  • Swap the minimum (29) with the first element of the subarray (37).
  • Resulting array: [10, 13, 14, 29, 37]

Iteration 5:

  • Only one element remains (37), which is already in its correct position.
  • The array is now fully sorted: [10, 13, 14, 29, 37]

The array is now sorted in ascending order using the selection sort algorithm.