-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selection Sort.fs
28 lines (21 loc) · 993 Bytes
/
Selection Sort.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/// Contains the selection sort implementation.
[<CompilationRepresentation (CompilationRepresentationFlags.ModuleSuffix)>]
module NikonTheThird.SortingAlgorithms.Algorithms.SelectionSort
open NikonTheThird.SortingAlgorithms.Algorithm
/// https://en.wikipedia.org/wiki/Selection_sort
[<Algorithm "Selection Sort">]
let selectionSort : Algorithm = algorithm {
let! count = getCount
for index = 0 to count - 1 do
use! _leftLabel = setLabel' (Primary (index - 1)) "L"
/// Keeps track of the currently found minimal item.
let mutable minIndex = index
// Check all other items above it if one is smaller.
for otherIndex = index + 1 to count - 1 do
match! compare otherIndex minIndex with
| LessThan -> do minIndex <- otherIndex
| _ -> do ()
// If the smallest found item is not in the right spot, move it there.
if minIndex <> index then
do! swap minIndex index
}