File tree Expand file tree Collapse file tree 1 file changed +22
-23
lines changed
contents/bogo_sort/code/go Expand file tree Collapse file tree 1 file changed +22
-23
lines changed Original file line number Diff line number Diff line change 3
3
package main
4
4
5
5
import (
6
- "fmt"
7
- "math/rand"
8
- "time"
6
+ "fmt"
7
+ "math/rand"
8
+ "time"
9
9
)
10
10
11
- func shuffle (a []int ) []int {
12
- rand .Seed (time .Now ().UnixNano ())
13
- for i := len (a ) - 1 ; i > 0 ; i -- {
14
- j := rand .Intn (i + 1 )
15
- a [i ], a [j ] = a [j ], a [i ]
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 ]
16
15
}
17
- return a
18
16
}
19
17
20
- func is_sorted (a []int ) bool {
21
- for i := 0 ; i < len (a )- 1 ; i ++ {
22
- if a [i + 1 ] < a [i ] {
23
- return false
24
- }
25
- }
26
- return true
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
27
25
}
28
26
29
- func bogo_sort (a * []int ) {
30
- for ! is_sorted (* a ) {
31
- * a = shuffle (* a )
32
- }
27
+ func bogoSort (a * []int ) {
28
+ for ! isSorted (* a ) {
29
+ shuffle (a )
30
+ }
33
31
}
34
32
35
33
func main () {
36
- a := []int {1 , 3 , 4 , 2 }
37
- bogo_sort (& a )
38
- fmt .Println (a )
34
+ rand .Seed (time .Now ().UnixNano ())
35
+ a := []int {1 , 3 , 4 , 2 }
36
+ bogoSort (& a )
37
+ fmt .Println (a )
39
38
}
You can’t perform that action at this time.
0 commit comments