summaryrefslogtreecommitdiff
path: root/sort
diff options
context:
space:
mode:
Diffstat (limited to 'sort')
-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
7 files changed, 14 insertions, 19 deletions
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
}