From cdea66e8071f83c32b101b1e70993e6407aac43a Mon Sep 17 00:00:00 2001 From: slowy07 Date: Tue, 1 Oct 2024 23:20:21 +0700 Subject: [PATCH 1/2] feat: menambahkan algoritma quick sorting Signed-off-by: slowy07 --- sorting/quick_sorting.nim | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 sorting/quick_sorting.nim diff --git a/sorting/quick_sorting.nim b/sorting/quick_sorting.nim new file mode 100644 index 0000000..667ff24 --- /dev/null +++ b/sorting/quick_sorting.nim @@ -0,0 +1,34 @@ +{.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]) = + if a.len == 0: + raise newException(ValueError, "Nilai Tidak Boleh Kosong") + 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) From ca75e8f4c3575c508e2acdf441df481de1c30c9f Mon Sep 17 00:00:00 2001 From: slowy07 Date: Tue, 1 Oct 2024 23:23:32 +0700 Subject: [PATCH 2/2] fix: menghapus fungsi logika array Signed-off-by: slowy07 --- sorting/quick_sorting.nim | 2 -- 1 file changed, 2 deletions(-) diff --git a/sorting/quick_sorting.nim b/sorting/quick_sorting.nim index 667ff24..e30f58f 100644 --- a/sorting/quick_sorting.nim +++ b/sorting/quick_sorting.nim @@ -21,8 +21,6 @@ proc sort[T](a: var openArray[T], low, high: int) = sort(a, pi + 1, high) proc quickSorting*[T](a: var openArray[T]) = - if a.len == 0: - raise newException(ValueError, "Nilai Tidak Boleh Kosong") sort(a, 0, a.high) when isMainModule: