Selection Sort

Languages

Implement a function that performs a selection sort. The function should take in an array of integers and return an array with the integers sorted in ascending order.

Selection Sort expected input and output

Examples

selectionSort([9, 3, 6, 2, 1, 11]); // [1, 2, 3, 6, 9, 11]
selectionSort([12, 16, 14, 1, 2, 3]); // [1, 2, 3, 12, 14, 16]

Recap

Selection sort is a sorting algorithm that repeatedly scans an unsorted array and with each iteration finds the minimum element to build up a sorted array.

Here is the basic idea behind selection sort:

  1. Find the minimum element in the array.
  2. Swap it with the element at the first position.
  3. Find the second minimum element in the array.
  4. Swap it with the element at the second position.
  5. Continue this process until the entire array is sorted.

Selection Sort explanation