Introduction to Selection Sort

Sorting algorithm 01

Gunavaran Brihadiswaran
Nerd For Tech

--

Image by author

Selection sort is one of the basic sorting algorithms which is easy to understand and implement. The idea is simple: given an array of elements to be sorted,

  • Divide the array into two subarrays: sorted and unsorted subarrays
  • In each iteration, find the maximum/minimum element in the unsorted subarray and swap it with the first element of the unsorted subarray. After swapping, the first element of the unsorted subarray will be appended to the sorted subarray. Swapping the minimum value will sort the elements in ascending order while swapping the maximum value will sort the elements in descending order.
  • Continue the iteration until the last element

Let’s look at an example for better understanding.

Suppose the array of numbers to be sorted in ascending order is (8,4,6,3,1,9,5)

Image by author

Initially, the entire array will be the unsorted subarray as shown in the figure above. We find the minimum value 1 and swap it with 8, which is the first element of the unsorted subarray. The sorted subarray is colored in yellow. The next minimum value in the…

--

--

Gunavaran Brihadiswaran
Nerd For Tech

A Computer Science Research Student who loves to do Research, Write and Travel