Skip to content

Commit b8586ce

Browse files
committed
Added Fisher-Yates shuffle in Go
1 parent 7280425 commit b8586ce

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package fisherYates
2+
3+
import (
4+
"math/rand"
5+
)
6+
7+
// Make sure you seed the PRNG before using this function
8+
// Fisher-Yates shuffle runs in O(n) and uses O(1) space
9+
func fisherYates(arr []int) []int {
10+
m := len(arr)
11+
var i int
12+
13+
for m != 0 {
14+
i = rand.Intn(m)
15+
m -= 1
16+
arr[i], arr[m] = arr[m], arr[i]
17+
}
18+
19+
return arr
20+
}

0 commit comments

Comments
 (0)