-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: menambahkan algoritma sorting bogo sorting (#2)
Signed-off-by: slowy07 <[email protected]>
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |