Add selection sort
This commit is contained in:
parent
c28460944e
commit
a46c88b794
1 changed files with 16 additions and 0 deletions
16
SelectionSort.nim
Normal file
16
SelectionSort.nim
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
proc selectionSort(list:seq[int]):seq[int] =
|
||||
result = list
|
||||
|
||||
proc indexOfMin(list:seq[int], startIndex:int): int =
|
||||
result = startIndex
|
||||
for i in startIndex .. list.high:
|
||||
if list[i] < list[result]:
|
||||
result = i
|
||||
|
||||
for i in result.low .. result.high:
|
||||
swap(result[i], result[result.indexOfMin(i)])
|
||||
|
||||
when isMainModule:
|
||||
let
|
||||
seq1 = @[1, 3, 2, 56, 2, 1, 32, 45, 23, 41, 6, 47, 212, 32, 6, 98, 54, 457, 37, 4]
|
||||
echo seq1.selectionSort
|
||||
Loading…
Add table
Add a link
Reference in a new issue