Skip to content

Commit 8871209

Browse files
author
Julian
authored
Merge pull request #365 from christopherm99/master
Add bogo sort in Golang
2 parents 4627e63 + dd9b728 commit 8871209

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

CONTRIBUTORS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
This file lists everyone, who contributed to this repo and wanted to show up here. If you're looking for information on contribution, please check the `CONTRIBUTING.md` out.
1+
This file lists everyone, who contributed to this repo and wanted to show up here. If you're looking for information on contribution, please check the `CONTRIBUTING.md` out.
22

33
# Contributors
44

@@ -38,3 +38,4 @@
3838
- PaddyKe
3939
- nic-hartley
4040
- crafter312
41+
- Christopher Milan

contents/bogo_sort/code/go/main.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Submitted by Christopher Milan (christopherm99)
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"math/rand"
8+
"time"
9+
)
10+
11+
func shuffle(a *[]int) {
12+
for i := len(*a) - 1; i > 0; i-- {
13+
j := rand.Intn(i + 1)
14+
(*a)[i], (*a)[j] = (*a)[j], (*a)[i]
15+
}
16+
}
17+
18+
func isSorted(a []int) bool {
19+
for i := 0; i < len(a)-1; i++ {
20+
if a[i+1] < a[i] {
21+
return false
22+
}
23+
}
24+
return true
25+
}
26+
27+
func bogoSort(a *[]int) {
28+
for !isSorted(*a) {
29+
shuffle(a)
30+
}
31+
}
32+
33+
func main() {
34+
rand.Seed(time.Now().UnixNano())
35+
a := []int{1, 3, 4, 2}
36+
bogoSort(&a)
37+
fmt.Println(a)
38+
}

0 commit comments

Comments
 (0)