summaryrefslogtreecommitdiff
path: root/sort
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2020-07-14 22:41:16 +0100
committerPaul Buetow <paul@buetow.org>2020-07-14 22:41:16 +0100
commit7f87861624a1c5feb5523479db1c242a98f659b2 (patch)
treeed71e8e045c21738b3305e4609d66c5a4a2d5032 /sort
parent1a175ce919dddb8d3a58533f3827a105483442c0 (diff)
add shellsort and refactor a bit
Diffstat (limited to 'sort')
-rw-r--r--sort/sort_test.go51
1 files changed, 39 insertions, 12 deletions
diff --git a/sort/sort_test.go b/sort/sort_test.go
index 60f8aa3..9593cde 100644
--- a/sort/sort_test.go
+++ b/sort/sort_test.go
@@ -8,46 +8,73 @@ import (
// Store results here to avoid compiler optimizations
var benchResult []ds.Comparer
+var benchResultInt []int
const maxLength int = 10000
type sortAlgorithm func([]ds.Comparer) []ds.Comparer
+type sortAlgorithmInt func([]int) []int
-func TestSort(t *testing.T) {
+func TestSelectionSort(t *testing.T) {
for i := 1; i <= maxLength; i *= 10 {
- test("Selection", i, Selection, t)
- test("Insertion", i, Insertion, t)
+ test(Selection, i, t)
}
}
-func BenchmarkSort(b *testing.B) {
+func TestInsertionSort(t *testing.T) {
for i := 1; i <= maxLength; i *= 10 {
- benchmark("Insertion", i, Insertion, b)
+ test(Insertion, i, t)
}
+}
+
+func TestShellSort(t *testing.T) {
+ for i := 1; i <= maxLength; i *= 10 {
+ test(Shell, i, t)
+ }
+}
+
+func BenchmarkInsertionSort(b *testing.B) {
+ for i := 1; i <= maxLength; i *= 10 {
+ benchmark(Insertion, i, b)
+ }
+}
+
+func BenchmarkSelectionSort(b *testing.B) {
+ for i := 1; i <= maxLength; i *= 10 {
+ benchmark(Selection, i, b)
+ }
+}
+
+func BenchmarkShellSort(b *testing.B) {
for i := 1; i <= maxLength; i *= 10 {
- benchmark("Selection", i, Selection, b)
+ benchmark(Shell, i, b)
}
}
-func test(name string, length int, sort sortAlgorithm, t *testing.T) {
+func test(sort sortAlgorithm, length int, t *testing.T) {
cb := func(t *testing.T) {
t.Parallel()
- a := ds.RandomIntegers(length, length)
+ a := makeIntegers(length, length)
a = sort(a)
if !Sorted(a) {
t.Errorf("Array not sorted: %v", a)
}
}
- t.Run(fmt.Sprintf("Test%s%d", name, length), cb)
+ t.Run(fmt.Sprintf("%d", length), cb)
}
-func benchmark(name string, length int, sort sortAlgorithm, b *testing.B) {
+func benchmark(sort sortAlgorithm, length int, b *testing.B) {
cb := func(b *testing.B) {
- a := ds.RandomIntegers(length, length)
+ a := makeIntegers(length, length)
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchResult = sort(a)
}
}
- b.Run(fmt.Sprintf("Benchmark%s%d", name, length), cb)
+ b.Run(fmt.Sprintf("%d", length), cb)
+}
+
+func makeIntegers(length, max int) []ds.Comparer {
+ //return ds.ReverseSortedIntegers(length)
+ return ds.RandomIntegers(length, max)
}