summaryrefslogtreecommitdiff
path: root/sort/shuffle.go
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2020-07-17 09:58:23 +0100
committerPaul Buetow <paul@buetow.org>2020-07-17 09:58:23 +0100
commite858efc16960b86175d655add938acd3f1edd13e (patch)
tree94bb8eb0487b45ebbe65b24b2e1468e96362c262 /sort/shuffle.go
parent82a314d3f211f9da4f0a906ccbe2df279c79e6de (diff)
shuffle sort works
Diffstat (limited to 'sort/shuffle.go')
-rw-r--r--sort/shuffle.go16
1 files changed, 7 insertions, 9 deletions
diff --git a/sort/shuffle.go b/sort/shuffle.go
index 040456b..9c6f5b9 100644
--- a/sort/shuffle.go
+++ b/sort/shuffle.go
@@ -2,23 +2,21 @@ package sort
import (
"algorithms/ds"
+ "math/rand"
)
func Shuffle(a ds.ArrayList) ds.ArrayList {
length := len(a)
+
for i := 0; i < length; i++ {
- min := i
- for j := i + 1; j < length; j++ {
- if a[min].Higher(a[j]) {
- min = j
- }
- }
- if min == i {
+ r := length - rand.Intn(length-i) - 1
+ if r == i {
continue
}
tmp := a[i]
- a[i] = a[min]
- a[min] = tmp
+ a[i] = a[r]
+ a[r] = tmp
}
+
return a
}