summaryrefslogtreecommitdiff
path: root/sort
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2020-07-17 09:25:48 +0100
committerPaul Buetow <paul@buetow.org>2020-07-17 09:25:48 +0100
commit224cff0a5113b1ea1a09fd48caf164e5690ec8a7 (patch)
tree06faddeef4fdc4792b47576b332f28cea0bacbbb /sort
parent8f291992bdd98bd7ca6eefe084790e56d00833a8 (diff)
refactor
Diffstat (limited to 'sort')
-rw-r--r--sort/insertion.go2
-rw-r--r--sort/selection.go2
-rw-r--r--sort/shell.go2
-rw-r--r--sort/sort_test.go12
-rw-r--r--sort/sorted.go12
5 files changed, 9 insertions, 21 deletions
diff --git a/sort/insertion.go b/sort/insertion.go
index 8e4af30..465e8b4 100644
--- a/sort/insertion.go
+++ b/sort/insertion.go
@@ -4,7 +4,7 @@ import (
"algorithms/ds"
)
-func Insertion(a []ds.Comparer) []ds.Comparer {
+func Insertion(a ds.ArrayList) ds.ArrayList {
length := len(a)
for i := 0; i < length; i++ {
diff --git a/sort/selection.go b/sort/selection.go
index d084fc0..3a5774f 100644
--- a/sort/selection.go
+++ b/sort/selection.go
@@ -4,7 +4,7 @@ import (
"algorithms/ds"
)
-func Selection(a []ds.Comparer) []ds.Comparer {
+func Selection(a ds.ArrayList) ds.ArrayList {
length := len(a)
for i := 0; i < length; i++ {
min := i
diff --git a/sort/shell.go b/sort/shell.go
index 39414f5..4132ca6 100644
--- a/sort/shell.go
+++ b/sort/shell.go
@@ -4,7 +4,7 @@ import (
"algorithms/ds"
)
-func Shell(a []ds.Comparer) []ds.Comparer {
+func Shell(a ds.ArrayList) ds.ArrayList {
length := len(a)
// h-sort the array
h := 1
diff --git a/sort/sort_test.go b/sort/sort_test.go
index 00f056a..d8250c4 100644
--- a/sort/sort_test.go
+++ b/sort/sort_test.go
@@ -7,12 +7,12 @@ import (
)
// Store results here to avoid compiler optimizations
-var benchResult []ds.Comparer
+var benchResult ds.ArrayList
var benchResultInt []int
const maxLength int = 10000
-type sortAlgorithm func([]ds.Comparer) []ds.Comparer
+type sortAlgorithm func(ds.ArrayList) ds.ArrayList
type sortAlgorithmInt func([]int) []int
func TestSelectionSort(t *testing.T) {
@@ -62,7 +62,7 @@ func test(sort sortAlgorithm, length int, t *testing.T) {
t.Parallel()
a := makeIntegers(length, length)
a = sort(a)
- if !Sorted(a) {
+ if !a.Sorted() {
t.Errorf("Array not sorted: %v", a)
}
}
@@ -73,8 +73,8 @@ 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)
+ if a.Sorted() {
+ t.Errorf("Array sorted: %v", a.FirstN(10))
}
}
t.Run(fmt.Sprintf("%d", length), cb)
@@ -91,7 +91,7 @@ func benchmark(sort sortAlgorithm, length int, b *testing.B) {
b.Run(fmt.Sprintf("%d", length), cb)
}
-func makeIntegers(length, max int) []ds.Comparer {
+func makeIntegers(length, max int) ds.ArrayList {
//return ds.ReverseSortedIntegers(length)
return ds.RandomIntegers(length, max)
}
diff --git a/sort/sorted.go b/sort/sorted.go
deleted file mode 100644
index b29e3c6..0000000
--- a/sort/sorted.go
+++ /dev/null
@@ -1,12 +0,0 @@
-package sort
-
-import "algorithms/ds"
-
-func Sorted(a []ds.Comparer) bool {
- for i := len(a) - 1; i > 0; i-- {
- if a[i].Lower(a[i-1]) {
- return false
- }
- }
- return true
-}