tquicksort.nim 630 B

123456789101112131415161718192021222324
  1. proc QuickSort(list: seq[int]): seq[int] =
  2. if len(list) == 0:
  3. return @[]
  4. var pivot = list[0]
  5. var left: seq[int] = @[]
  6. var right: seq[int] = @[]
  7. for i in low(list)..high(list):
  8. if list[i] < pivot:
  9. left.add(list[i])
  10. elif list[i] > pivot:
  11. right.add(list[i])
  12. result = QuickSort(left) &
  13. pivot &
  14. QuickSort(right)
  15. proc echoSeq(a: seq[int]) =
  16. for i in low(a)..high(a):
  17. echo(a[i])
  18. let list = QuickSort(@[89,23,15,23,56,123,356,12,7,1,6,2,9,4,3])
  19. let expected = @[1, 2, 3, 4, 6, 7, 9, 12, 15, 23, 56, 89, 123, 356]
  20. doAssert list == expected