Sorting algorithms/Cocktail sort: Difference between revisions

Content added Content deleted
(→‎{{header|R}}: Used a cool R trick and tried to follow pseudo-code more closely. Also added more outputs.)
(Updated to work with current version of Nim (1.4).)
Line 2,630: Line 2,630:


=={{header|Nim}}==
=={{header|Nim}}==
<lang nim>template trySwap(): stmt {.immediate.} =
<lang nim>template trySwap(): untyped =
if a[i] < a[i-1]:
if a[i] < a[i-1]:
swap a[i], a[i-1]
swap a[i], a[i-1]
Line 2,640: Line 2,640:
while not t:
while not t:
t = true
t = true
for i in 1 .. <l: trySwap
for i in 1 ..< l: trySwap
if t: break
if t: break
for i in countdown(l-1, 1): trySwap
for i in countdown(l-1, 1): trySwap