summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <pbuetow@mimecast.com>2020-08-08 14:06:12 +0100
committerPaul Buetow <pbuetow@mimecast.com>2020-08-08 14:06:12 +0100
commit06c8d4c68650bbc1fc158d90a44c4c17644c7889 (patch)
tree3b2bd79680bc233dadd93e1a2be48e00ee60b66b
parent44e930b71107310eae55060cf0aa2cac7089d239 (diff)
wrapup
-rw-r--r--ds/arraylist.go35
-rw-r--r--ds/integer.go78
-rw-r--r--sort/insertion.go2
-rw-r--r--sort/merge.go2
-rw-r--r--sort/quick3way.go6
-rw-r--r--sort/selection.go2
-rw-r--r--sort/shell.go6
-rw-r--r--sort/shuffle.go4
-rw-r--r--sort/sort_test.go11
9 files changed, 47 insertions, 99 deletions
diff --git a/ds/arraylist.go b/ds/arraylist.go
index 66490f3..5964b88 100644
--- a/ds/arraylist.go
+++ b/ds/arraylist.go
@@ -2,10 +2,41 @@ package ds
import (
"fmt"
+ "math/rand"
"strings"
)
-type ArrayList []Integer
+type ArrayList []int
+
+func NewRandomArrayList(length, max int) ArrayList {
+ a := make(ArrayList, length)
+ for i := 0; i < length; i++ {
+ if max > 0 {
+ a[i] = rand.Intn(max)
+ continue
+ }
+ a[i] = rand.Int()
+ }
+ return a
+}
+
+func NewAscendingArrayList(length int) ArrayList {
+ a := make(ArrayList, length)
+ for i := 0; i < length; i++ {
+ a[i] = i
+ }
+ return a
+}
+
+func NewDescendingArrayList(length int) ArrayList {
+ a := make(ArrayList, length)
+ j := length - 1
+ for i := 0; i < length; i++ {
+ a[i] = j
+ j--
+ }
+ return a
+}
func (a ArrayList) FirstN(n int) string {
var sb strings.Builder
@@ -29,7 +60,7 @@ func (a ArrayList) FirstN(n int) string {
func (a ArrayList) Sorted() bool {
for i := len(a) - 1; i > 0; i-- {
- if a[i].Lower(a[i-1]) {
+ if a[i] < a[i-1] {
return false
}
}
diff --git a/ds/integer.go b/ds/integer.go
deleted file mode 100644
index 1372624..0000000
--- a/ds/integer.go
+++ /dev/null
@@ -1,78 +0,0 @@
-package ds
-
-import (
- "math/rand"
-)
-
-type Integer int
-
-func RandomIntegers(length, max int) ArrayList {
- a := make(ArrayList, length)
- for i := 0; i < length; i++ {
- if max > 0 {
- a[i] = Integer(rand.Intn(max))
- continue
- }
- a[i] = Integer(rand.Int())
- }
- return a
-}
-
-func AscendingIntegers(length int) ArrayList {
- a := make(ArrayList, length)
- for i := 0; i < length; i++ {
- a[i] = Integer(i)
- }
- return a
-}
-
-func DescendingIntegers(length int) ArrayList {
- a := make(ArrayList, length)
- j := length
- for i := 0; i < length; i++ {
- a[i] = Integer(j)
- j--
- }
- return a
-}
-
-func (i Integer) Equal(j Integer) bool {
- return i == j
-}
-
-func (i Integer) Lower(j Integer) bool {
- return i < j
-}
-
-func (i Integer) LowerEqual(j Integer) bool {
- return i <= j
-}
-
-func (i Integer) Higher(j Integer) bool {
- return i > j
-}
-
-func (i Integer) HigherEqual(j Integer) bool {
- return i >= j
-}
-
-func (i Integer) Compare(j Integer) int {
- switch {
- case i < j:
- return -1
- case i > j:
- return 1
- }
- return 0
-}
-
-func (i Integer) CompareCB(j Integer, lowerCB, higherCB, equalsCB func()) {
- switch {
- case i < j:
- lowerCB()
- case i > j:
- higherCB()
- default:
- equalsCB()
- }
-}
diff --git a/sort/insertion.go b/sort/insertion.go
index 983edf2..6663bca 100644
--- a/sort/insertion.go
+++ b/sort/insertion.go
@@ -12,7 +12,7 @@ func Insertion(a ds.ArrayList) ds.ArrayList {
func insertion(a ds.ArrayList, lo, hi int) {
for i := lo; i <= hi; i++ {
for j := i; j > 0; j-- {
- if a[j].Higher(a[j-1]) {
+ if a[j] > a[j-1] {
break
}
a.Swap(j, j-1)
diff --git a/sort/merge.go b/sort/merge.go
index e57ea6d..9ee788d 100644
--- a/sort/merge.go
+++ b/sort/merge.go
@@ -38,7 +38,7 @@ func merge(a, aux ds.ArrayList, lo, mid, hi int) {
case j > hi:
a[k] = aux[i]
i++
- case aux[i].Higher(aux[j]):
+ case aux[i] > aux[j]:
a[k] = aux[j]
j++
default:
diff --git a/sort/quick3way.go b/sort/quick3way.go
index e55b60f..e6cb677 100644
--- a/sort/quick3way.go
+++ b/sort/quick3way.go
@@ -26,12 +26,12 @@ func quick3Way(a ds.ArrayList, lo, hi int) {
v := a[lo] // Partitioning item
for i <= gt {
- switch a[i].Compare(v) {
- case -1:
+ switch {
+ case a[i] < v:
a.Swap(lt, i)
lt++
i++
- case 1:
+ case a[i] > v:
a.Swap(i, gt)
gt--
default:
diff --git a/sort/selection.go b/sort/selection.go
index 66d221e..798fa27 100644
--- a/sort/selection.go
+++ b/sort/selection.go
@@ -10,7 +10,7 @@ func Selection(a ds.ArrayList) ds.ArrayList {
for i := 0; i < length; i++ {
min := i
for j := i + 1; j < length; j++ {
- if a[min].Higher(a[j]) {
+ if a[min] > a[j] {
min = j
}
}
diff --git a/sort/shell.go b/sort/shell.go
index 4132ca6..4a8fded 100644
--- a/sort/shell.go
+++ b/sort/shell.go
@@ -17,12 +17,10 @@ func Shell(a ds.ArrayList) ds.ArrayList {
for h >= 1 {
for i := h; i < length; i++ {
for j := i; j >= h; j -= h {
- if a[j-h].Lower(a[j]) {
+ if a[j-h] < a[j] {
break
}
- tmp := a[j]
- a[j] = a[j-h]
- a[j-h] = tmp
+ a.Swap(j, j-h)
}
}
diff --git a/sort/shuffle.go b/sort/shuffle.go
index 26db8c4..0c20a80 100644
--- a/sort/shuffle.go
+++ b/sort/shuffle.go
@@ -10,9 +10,7 @@ func Shuffle(a ds.ArrayList) ds.ArrayList {
for i := 0; i < length; i++ {
r := length - rand.Intn(length-i) - 1
- tmp := a[i]
- a[i] = a[r]
- a[r] = tmp
+ a.Swap(i, r)
}
return a
diff --git a/sort/sort_test.go b/sort/sort_test.go
index fa515d7..6f6bcc2 100644
--- a/sort/sort_test.go
+++ b/sort/sort_test.go
@@ -11,7 +11,7 @@ var benchResult ds.ArrayList
var benchResultInt []int
const minLength int = 1
-const maxLength int = 1000000
+const maxLength int = 100000
const maxSlowLength int = 100000
var arrayListCache map[string]ds.ArrayList
@@ -159,7 +159,7 @@ func test(sort sortAlgorithm, length int, t *testing.T) {
func testShuffle(sort sortAlgorithm, length int, t *testing.T) {
cb := func(t *testing.T) {
t.Parallel()
- a := sort(ds.AscendingIntegers(length))
+ a := sort(ds.NewAscendingArrayList(length))
if a.Sorted() {
t.Errorf("Array sorted: %v", a.FirstN(10))
}
@@ -176,7 +176,7 @@ func benchmark(sort sortAlgorithm, length int, b *testing.B) {
}
})
- a = ds.AscendingIntegers(length)
+ a = ds.NewAscendingArrayList(length)
b.Run(fmt.Sprintf("ascending(%d)", length), func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
@@ -184,7 +184,7 @@ func benchmark(sort sortAlgorithm, length int, b *testing.B) {
}
})
- a = ds.DescendingIntegers(length)
+ a = ds.NewDescendingArrayList(length)
b.Run(fmt.Sprintf("descending(%d)", length), func(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
@@ -204,8 +204,7 @@ func makeRandomIntegers(length, max int) ds.ArrayList {
if a, ok := arrayListCache[key]; ok {
return a
}
- a := ds.RandomIntegers(length, max)
- a = Shuffle(a)
+ a := ds.NewRandomArrayList(length, max)
arrayListCache[key] = a
return a
}