STD 01 - Selection Sort
Intro
Selection sort works with two subarrays: the sorted section, and the unsorted section. It works by taking the minimum value in the unsorted array, putting it at the end of the sorted array, and continuing until there is no unsorted array.
Task
Implement this pseudocode in
- Python,
- or C++,
- or (if you like) both,
adding comments as necessary. Be aware that min may be a protected term so you may need a different variable name e.g. minn. Note that a swap needs to be performed, and this, in the pseudocode, is the subfunction SWAP. You need to work out how to implement the swap. This does not have to be done using a subfunction: the swap could be implemented directly – the choice is yours.
Pseudo-code
SELECTION_SORT(A)
FOR i TO length(A)-1
min ← i
FOR j ← i + 1 TO length(A)
IF A[j] < A[min]
min ← j
SWAP (A, i, min)
RETURN A