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 (or descending) 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 algorithm is selection sort.

It proceeds as follows:

  1. The minimum number is found in the entire array and then swapped with the first element.
  2. Next, the second minimum is found, by iterating over the array starting at index 1 and going up to the very end of the array, and then swapped with the second element.
  3. Next, the third minimum is found, by iterating over the array starting at index 2 and again going up to the very end of the array, and then 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 selection_sort() that takes in a given array and sorts it (in ascending order) using the selection sort algorithm.

Note that the array must be sorted in-place, i.e. the original array must be modified. Moreover, the function must NOT return anything itself.