nim-random/ThanosSort.nim
joachimschmidt557 c28460944e Add thanos sort
2019-04-25 10:57:18 +02:00

20 lines
560 B
Nim

proc thanosSort(list:seq[int]): seq[int] =
proc isSorted(list:seq[int]):bool =
for i in list.low .. list.high - 1:
if list[i] > list[i+1]:
return false
return true
proc snap(list:seq[int]):seq[int] =
for i in list.low .. (list.high div 2):
result.add list[i * 2]
result = list
while not result.isSorted:
result = result.snap
when isMainModule:
let
seq1 = @[1, 3, 5, 7, 23, 22, 13, 74, 4223, 3, 665, 62, 245, 2, 57, 3, 2, 4, 7, 4]
echo seq1.thanosSort