Skip to content

Commit

Permalink
feat: menambahkan algoritma sorting bogo sorting (#2)
Browse files Browse the repository at this point in the history
Signed-off-by: slowy07 <[email protected]>
  • Loading branch information
slowy07 authored Sep 30, 2024
1 parent ef65c5d commit 25d863e
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions sorting/bogo_sorting.nim
Original file line number Diff line number Diff line change
@@ -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.len - 1:
if array[i] > 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)

0 comments on commit 25d863e

Please sign in to comment.