From 25d863ecf385d308afd56026555922ea3a9a9f9f Mon Sep 17 00:00:00 2001 From: arfy slowy Date: Tue, 1 Oct 2024 02:34:09 +0700 Subject: [PATCH] feat: menambahkan algoritma sorting bogo sorting (#2) Signed-off-by: slowy07 --- sorting/bogo_sorting.nim | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 sorting/bogo_sorting.nim diff --git a/sorting/bogo_sorting.nim b/sorting/bogo_sorting.nim new file mode 100644 index 0000000..4766b63 --- /dev/null +++ b/sorting/bogo_sorting.nim @@ -0,0 +1,31 @@ +{.push raises: [].} + +runnableExamples: + var array = @[3, 1, 2] + bogoSorting(array) + doAssert telahDisorting(array) + +import random + +func telahDisorting[T](array: openArray[T]): bool = + for i in 0.. array[i + 1]: + return false + return true + +proc bogoSorting*[T](array: var openArray[T]) = + while not telahDisorting(array): + shuffle(array) + +when isMainModule: + import std/unittest + suite "BogoSortingTest": + test "test fungsi dari bogoSorting integer": + var array = @[3, 1, 2] + bogoSorting(array) + check telahDisorting(array) + + test "sorting array dari string": + var array = @["b", "c", "a"] + bogoSorting(array) + check telahDisorting(array)