summaryrefslogtreecommitdiff
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
parent8f291992bdd98bd7ca6eefe084790e56d00833a8 (diff)
refactor
-rw-r--r--ds/arraylist.go37
-rw-r--r--ds/comparer.go4
-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
7 files changed, 46 insertions, 25 deletions
diff --git a/ds/arraylist.go b/ds/arraylist.go
new file mode 100644
index 0000000..65c42ef
--- /dev/null
+++ b/ds/arraylist.go
@@ -0,0 +1,37 @@
+package ds
+
+import (
+ "fmt"
+ "strings"
+)
+
+type ArrayList []Comparer
+
+func (a ArrayList) FirstN(n int) string {
+ var sb strings.Builder
+ j := n
+
+ length := len(a)
+ if j > length {
+ j = length
+ }
+
+ for i := 0; i < j; i++ {
+ fmt.Fprintf(&sb, "%v ", a[i])
+ }
+
+ if j < length {
+ fmt.Fprintf(&sb, "... ")
+ }
+
+ return sb.String()
+}
+
+func (a ArrayList) Sorted() bool {
+ for i := len(a) - 1; i > 0; i-- {
+ if a[i].Lower(a[i-1]) {
+ return false
+ }
+ }
+ return true
+}
diff --git a/ds/comparer.go b/ds/comparer.go
index c132ea9..8fb3727 100644
--- a/ds/comparer.go
+++ b/ds/comparer.go
@@ -8,7 +8,3 @@ type Comparer interface {
HigherEqual(a Comparer) bool
Int() int
}
-
-func (c []Comparer) firstN(n int) []Comparer {
- return c
-}
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
-}