Objective

Given an array of numbers, sort it using the selection sort algorithm.

Difficulty

Easy

Description

Sorting is one of the most useful aspects of studying programming and computer algorithms. At its core, it's just to rearrange the existing elements of an array such that they line up in ascending order.

That is, the smallest element comes first, then the second smallest element, then the third smallest element, and so on and so forth.

Now there are various kinds of sorting algorithms. Each attacks the problem of sorting in a different way. One particularly simple and intuitive sorting algorithms is selection sort.

It proceeds as follows:

  1. The minimum is found in the entire array, and swapped with the first element.
  2. Next, the minimum is found in the entire array starting at index 1, and swapped with the second element.
  3. Next, the minimum is found in the entire array starting at index 2, and swapped with the third element.
  4. This goes on until the array to go over has effectively just one element to consider.

In other words, we successively find minimum values and swap them with successive elements in the array, starting with the first element.

The reason why this algorithm is called 'selection sort' is because in each iteration, the next minimum is selected and consequently swapped.

In this exercise, you have to create a function selectionSort() that takes in a given array and sorts it in-place (i.e. the original array must be modified) using the selection sort algorithm.

Note that the function MUST NOT return anything.