golanglabs

Log | Files | Refs | README | LICENSE

commit 0ce18fa30d90d6a4902d6f2b7a13c4417c0e5173
parent 3373f83348fc6bf902085a2dcaa5c533c8f41abd
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Sat,  6 Apr 2024 17:09:29 +0200

Implemented fisher-yates shuffle.

Diffstat:
Msrc/dsa/fisher_yates.go | 19++++++++++++++++++-
Msrc/main.go | 8++++++--
Msrc/routing_webserver/webserver.go | 2+-
3 files changed, 25 insertions(+), 4 deletions(-)

diff --git a/src/dsa/fisher_yates.go b/src/dsa/fisher_yates.go @@ -1,5 +1,22 @@ package dsa -func FisherYates() { +import ( + "math/rand/v2" +) +func FisherYates(s []int) []int { + for i := int32(len(s) - 1); i > 0; i-- { + r := rand.Int32N(i) + s[i], s[r] = switchPos(s[i], s[r]) + } + + return s +} + +func switchPos(x int, y int) (int, int) { + z := x + x = y + y = z + + return x, y } diff --git a/src/main.go b/src/main.go @@ -8,8 +8,12 @@ import ( ) func main() { - slice := []int{1, 2, 3, 4} - dsa.MergeSort(slice) + slice := []int{9, 8, 7, 6, 5, 4, 3, 2, 1, 0} + PrintArr(slice) + slice = dsa.MergeSort(slice) + PrintArr(slice) + slice = dsa.FisherYates(slice) + PrintArr(slice) } // print an []int array diff --git a/src/routing_webserver/webserver.go b/src/routing_webserver/webserver.go @@ -12,7 +12,7 @@ func main() { } type Routes struct { - route string `json:"path"` + Route string `json:"path"` } func loadRoutes() {