Skip to content

Commit 0bbc5fd

Browse files
committed
Added Bogosort in Go
1 parent 75990cf commit 0bbc5fd

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Bogosort/Go/jcla1/bogosort.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package bogosort
2+
3+
import "math/rand"
4+
5+
func Bogosort(arr []int) []int {
6+
for !isSorted(arr) {
7+
fisherYates(arr)
8+
}
9+
10+
return arr
11+
}
12+
13+
func isSorted(arr []int) bool {
14+
for i := 1; i < len(arr); i++ {
15+
if arr[i-1] > arr[i] {
16+
return false
17+
}
18+
}
19+
20+
return true
21+
}
22+
23+
// This function is just for shuffling,
24+
// not part of the actual algorithm
25+
func fisherYates(arr []int) []int {
26+
m := len(arr)
27+
var i int
28+
29+
for m != 0 {
30+
i = rand.Intn(m)
31+
m -= 1
32+
arr[i], arr[m] = arr[m], arr[i]
33+
}
34+
35+
return arr
36+
}

0 commit comments

Comments
 (0)