diff --git a/sorting/quick_sorting.nim b/sorting/quick_sorting.nim new file mode 100644 index 0000000..e30f58f --- /dev/null +++ b/sorting/quick_sorting.nim @@ -0,0 +1,32 @@ +{.push raises: [].} + +runnableExamples: + var array = @[3, 1, 2] + quickSorting(array) + +proc partition[T](a: var openArray[T], low, high: int): int = + var i = low - 1 + let pivot = a[high] + for j in low .. high: + if a[j] < pivot: + inc i + swap a[i], a[j] + swap a[i + 1], a[high] + return i + 1 + +proc sort[T](a: var openArray[T], low, high: int) = + if low < high: + var pi = partition(a, low, high) + sort(a, low, pi - 1) + sort(a, pi + 1, high) + +proc quickSorting*[T](a: var openArray[T]) = + sort(a, 0, a.high) + +when isMainModule: + import std/unittest + + suite "Testing Quick Sorting": + test "test fungsi quick sorting": + var array = @[3, 1, 2] + quickSorting(array)