diff options
| author | Paul Buetow <paul@buetow.org> | 2020-07-17 09:58:23 +0100 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2020-07-17 09:58:23 +0100 |
| commit | e858efc16960b86175d655add938acd3f1edd13e (patch) | |
| tree | 94bb8eb0487b45ebbe65b24b2e1468e96362c262 /sort | |
| parent | 82a314d3f211f9da4f0a906ccbe2df279c79e6de (diff) | |
shuffle sort works
Diffstat (limited to 'sort')
| -rw-r--r-- | sort/shuffle.go | 16 | ||||
| -rw-r--r-- | sort/sort_test.go | 9 |
2 files changed, 14 insertions, 11 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 } diff --git a/sort/sort_test.go b/sort/sort_test.go index d8250c4..ad75fff 100644 --- a/sort/sort_test.go +++ b/sort/sort_test.go @@ -34,7 +34,7 @@ func TestShellSort(t *testing.T) { } func TestShuffleSort(t *testing.T) { - for i := 1; i <= maxLength; i *= 10 { + for i := 10; i <= maxLength; i *= 10 { testShuffle(Shuffle, i, t) } } @@ -57,6 +57,12 @@ func BenchmarkShellSort(b *testing.B) { } } +func BenchmarkShuffleSort(b *testing.B) { + for i := 1; i <= maxLength; i *= 10 { + benchmark(Shuffle, i, b) + } +} + func test(sort sortAlgorithm, length int, t *testing.T) { cb := func(t *testing.T) { t.Parallel() @@ -92,6 +98,5 @@ func benchmark(sort sortAlgorithm, length int, b *testing.B) { } func makeIntegers(length, max int) ds.ArrayList { - //return ds.ReverseSortedIntegers(length) return ds.RandomIntegers(length, max) } |
