Skip to content

Instantly share code, notes, and snippets.

@Rico040
Created October 24, 2022 18:46
Show Gist options
  • Save Rico040/5c6cf8d32817dec629fe523aa2d06cd7 to your computer and use it in GitHub Desktop.
Save Rico040/5c6cf8d32817dec629fe523aa2d06cd7 to your computer and use it in GitHub Desktop.
we trust in bogosort
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
a := make([]int, 10)
for i := 0; i < len(a); i++ {
a[i] = rand.Intn(100 - 1) + 1
}
fmt.Println(a)
b := Bogosort(a)
fmt.Println(b)
}
func Bogosort(a []int) []int {
n := len(a)
x := 0
for isSorted(a) != true {
for i := 0; i < n; i++ {
x++
r := rand.Intn(n - 1)
a[i], a[r] = a[r], a[i]
}
}
fmt.Println(x, "times bogosorted")
return a
}
func isSorted(a []int) bool {
n := len(a)
for i := 0; i < n - 1; i++ {
if a[i] > a[i + 1] {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment