Skip to content

Instantly share code, notes, and snippets.

@gurimusan
Created March 11, 2016 05:08
Show Gist options
  • Save gurimusan/69561407537921552fa4 to your computer and use it in GitHub Desktop.
Save gurimusan/69561407537921552fa4 to your computer and use it in GitHub Desktop.
discard """
Run as following.
$ nim c -r --verbosity:0 insert_sort.nim
"""
proc insert_sort(alist: var seq[int], position: int, value: int) =
var i: int
i = position - 1
while i >= 0 and alist[i] > value:
alist[i+1] = alist[i]
dec i
alist[i+1] = value
proc sort(alist: var seq[int]) =
for i in countup(1, high(alist)):
insert_sort(alist, i, alist[i])
var alist = @[2, 4, 5, 0, 1]
sort(alist)
echo(alist)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment