diff options
| author | Paul Buetow <paul@buetow.org> | 2020-07-17 09:03:52 +0100 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2020-07-17 09:03:52 +0100 |
| commit | 8f291992bdd98bd7ca6eefe084790e56d00833a8 (patch) | |
| tree | 3313bbfd675a1f5fef0178152a4ade83b5b27f2e | |
| parent | e6e3b27756974ad7255345c98260918a96f3a476 (diff) | |
fix shell sort
| -rw-r--r-- | ds/comparer.go | 4 | ||||
| -rw-r--r-- | sort/sort_test.go | 17 |
2 files changed, 21 insertions, 0 deletions
diff --git a/ds/comparer.go b/ds/comparer.go index 8fb3727..c132ea9 100644 --- a/ds/comparer.go +++ b/ds/comparer.go @@ -8,3 +8,7 @@ type Comparer interface { HigherEqual(a Comparer) bool Int() int } + +func (c []Comparer) firstN(n int) []Comparer { + return c +} diff --git a/sort/sort_test.go b/sort/sort_test.go index 9593cde..00f056a 100644 --- a/sort/sort_test.go +++ b/sort/sort_test.go @@ -33,6 +33,12 @@ func TestShellSort(t *testing.T) { } } +func TestShuffleSort(t *testing.T) { + for i := 1; i <= maxLength; i *= 10 { + testShuffle(Shuffle, i, t) + } +} + func BenchmarkInsertionSort(b *testing.B) { for i := 1; i <= maxLength; i *= 10 { benchmark(Insertion, i, b) @@ -63,6 +69,17 @@ func test(sort sortAlgorithm, length int, t *testing.T) { t.Run(fmt.Sprintf("%d", length), cb) } +func testShuffle(sort sortAlgorithm, length int, t *testing.T) { + cb := func(t *testing.T) { + t.Parallel() + a := sort(ds.SortedIntegers(length)) + if Sorted(a) { + t.Errorf("Array sorted: %v", a) + } + } + t.Run(fmt.Sprintf("%d", length), cb) +} + func benchmark(sort sortAlgorithm, length int, b *testing.B) { cb := func(b *testing.B) { a := makeIntegers(length, length) |
