Skip to content

Commit

Permalink
feat: menambahkan algoritma quick sorting (#3)
Browse files Browse the repository at this point in the history
* feat: menambahkan algoritma quick sorting

Signed-off-by: slowy07 <slowy.arfy@proton.me>

* fix: menghapus fungsi logika array

Signed-off-by: slowy07 <slowy.arfy@proton.me>

---------

Signed-off-by: slowy07 <slowy.arfy@proton.me>
  • Loading branch information
slowy07 authored Oct 2, 2024
1 parent 25d863e commit 8310f18
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions sorting/quick_sorting.nim
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 8310f18

Please sign in to comment.