Skip to content

Commit

Permalink
Sorting: unifying tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arnauddri committed Jan 30, 2015
1 parent 6e11106 commit 592c3d3
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 175 deletions.
24 changes: 3 additions & 21 deletions algorithms/maths/permutations-count/permutations_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package permutations

import (
"bufio"
"os"
"strconv"
"github.com/arnauddri/algorithms/algorithms/sorting/utils"
"testing"
)

Expand All @@ -26,37 +24,21 @@ func TestRecursive(t *testing.T) {
}

func BenchmarkRecursive(b *testing.B) {
array := _getArray()
array := dummy.GetArrayOfSize(10000)

for i := 0; i < b.N; i++ {
recursiveCount(array)
}
}

func BenchmarkIterative(b *testing.B) {
array := _getArray()
array := dummy.GetArrayOfSize(10000)

for i := 0; i < b.N; i++ {
iterativeCount(array)
}
}

func _getArray() []int {
f, err := os.Open("./IntegerArray.txt")
defer f.Close()
check(err)

numbers := make([]int, 0)
scanner := bufio.NewScanner(f)

for scanner.Scan() {
s, _ := strconv.Atoi(scanner.Text())
numbers = append(numbers, s)
}

return numbers
}

func check(e error) {
if e != nil {
panic(e)
Expand Down
8 changes: 4 additions & 4 deletions algorithms/sorting/bubble-sort/bubble.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
//"fmt"
)

func sort(a []int) {
for itemCount := len(a) - 1; ; itemCount-- {
func sort(arr []int) {
for itemCount := len(arr) - 1; ; itemCount-- {
swap := false
for i := 1; i <= itemCount; i++ {
if a[i-1] > a[i] {
a[i-1], a[i] = a[i], a[i-1]
if arr[i-1] > arr[i] {
arr[i-1], arr[i] = arr[i], arr[i-1]
swap = true
}
}
Expand Down
24 changes: 6 additions & 18 deletions algorithms/sorting/bubble-sort/bubble_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@ package bubble

import (
"fmt"
"github.com/arnauddri/algorithms/algorithms/sorting/utils"
"testing"
)

func TestBubbleSort(t *testing.T) {
list := []int{1, 3, 2, 8, 4, 10, 54, 49, 6}
list := dummy.GetArrayOfSize(100)

sort(list)
sorted_list := []int{1, 2, 3, 4, 6, 8, 10, 49, 54}

if !deepEqual(list, sorted_list) {
fmt.Println(list)
t.Error()
}
}

func deepEqual(a, b []int) bool {
if len(a) != len(b) {
return false
}

for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
for i := 0; i < len(list)-2; i++ {
if list[i] > list[i+1] {
fmt.Println(list)
t.Error()
}
}

return true
}
14 changes: 6 additions & 8 deletions algorithms/sorting/heap-sort/heap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ import (
"github.com/arnauddri/algorithms/data-structures/heap"
)

func sort(a []int) []int {
func sort(arr []int) []int {
h := heap.NewMin()
for i := 0; i < len(a); i++ {
h.Insert(heap.Int(a[i]))
for i := 0; i < len(arr); i++ {
h.Insert(heap.Int(arr[i]))
}

sorted := make([]int, 0)

for i := 0; i < len(a); i++ {
sorted = append(sorted, int(h.Extract().(heap.Int)))
for i := 0; i < len(arr); i++ {
arr[i] = int(h.Extract().(heap.Int))
}

return sorted
return arr
}
26 changes: 7 additions & 19 deletions algorithms/sorting/heap-sort/heap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@ package heap

import (
"fmt"
"github.com/arnauddri/algorithms/algorithms/sorting/utils"
"testing"
)

func TestHeapSort(t *testing.T) {
list := []int{1, 3, 2, 8, 4, 10, 54, 49, 6}
list := dummy.GetArrayOfSize(10)

sorted := sort(list)
sorted_list := []int{1, 2, 3, 4, 6, 8, 10, 49, 54}
sort(list)

if !deepEqual(sorted, sorted_list) {
fmt.Println(sorted)
t.Error()
}
}

func deepEqual(a, b []int) bool {
if len(a) != len(b) {
return false
}

for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
for i := 0; i < len(list)-2; i++ {
if list[i] > list[i+1] {
fmt.Println(list)
t.Error()
}
}

return true
}
12 changes: 6 additions & 6 deletions algorithms/sorting/insertion-sort/insertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package insertion

import ()

func sort(a []int) {
for i := 1; i < len(a); i++ {
value := a[i]
func sort(arr []int) {
for i := 1; i < len(arr); i++ {
value := arr[i]
j := i - 1
for j >= 0 && a[j] > value {
a[j+1] = a[j]
for j >= 0 && arr[j] > value {
arr[j+1] = arr[j]
j = j - 1
}
a[j+1] = value
arr[j+1] = value
}
}
24 changes: 6 additions & 18 deletions algorithms/sorting/insertion-sort/insertion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@ package insertion

import (
"fmt"
"github.com/arnauddri/algorithms/algorithms/sorting/utils"
"testing"
)

func TestInsertionSort(t *testing.T) {
list := []int{1, 3, 2, 8, 4, 10, 54, 49, 6}
list := dummy.GetArrayOfSize(100)

sort(list)
sorted_list := []int{1, 2, 3, 4, 6, 8, 10, 49, 54}

if !deepEqual(list, sorted_list) {
fmt.Println(list)
t.Error()
}
}

func deepEqual(a, b []int) bool {
if len(a) != len(b) {
return false
}

for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
for i := 0; i < len(list)-2; i++ {
if list[i] > list[i+1] {
fmt.Println(list)
t.Error()
}
}

return true
}
4 changes: 1 addition & 3 deletions algorithms/sorting/merge-sort/merge.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package merge

var arr = []int{20, 43, 52, -1, 43, 29, 34}
var s = make([]int, len(arr)/2+1)

func sort(arr []int) {
var s = make([]int, len(arr)/2+1)
if len(arr) < 2 {
return
}
Expand Down
24 changes: 6 additions & 18 deletions algorithms/sorting/merge-sort/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@ package merge

import (
"fmt"
"github.com/arnauddri/algorithms/algorithms/sorting/utils"
"testing"
)

func TestMergeSort(t *testing.T) {
list := []int{1, 3, 2, 8, 4, 10, 54, 49, 6}
list := dummy.GetArrayOfSize(100)

sort(list)
sorted_list := []int{1, 2, 3, 4, 6, 8, 10, 49, 54}

if !deepEqual(list, sorted_list) {
fmt.Println(list)
t.Error()
}
}

func deepEqual(a, b []int) bool {
if len(a) != len(b) {
return false
}

for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
for i := 0; i < len(list)-2; i++ {
if list[i] > list[i+1] {
fmt.Println(list)
t.Error()
}
}

return true
}
24 changes: 6 additions & 18 deletions algorithms/sorting/quick-sort/quick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@ package quick

import (
"fmt"
"github.com/arnauddri/algorithms/algorithms/sorting/utils"
"testing"
)

func TestQuickSort(t *testing.T) {
list := []int{1, 3, 2, 8, 4, 10, 54, 49, 6}
list := dummy.GetArrayOfSize(100)

sort(list)
sorted_list := []int{1, 2, 3, 4, 6, 8, 10, 49, 54}

if !deepEqual(list, sorted_list) {
fmt.Println(list)
t.Error()
}
}

func deepEqual(a, b []int) bool {
if len(a) != len(b) {
return false
}

for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
for i := 0; i < len(list)-2; i++ {
if list[i] > list[i+1] {
fmt.Println(list)
t.Error()
}
}

return true
}
12 changes: 6 additions & 6 deletions algorithms/sorting/selection-sort/selection.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package selection

func sort(a []int) []int {
for i := 0; i < len(a); i++ {
func sort(arr []int) []int {
for i := 0; i < len(arr); i++ {
min := i
for j := i + 1; j < len(a); j++ {
if a[j] < a[min] {
for j := i + 1; j < len(arr); j++ {
if arr[j] < arr[min] {
min = j
}
}
if min != i {
a[i], a[min] = a[min], a[i]
arr[i], arr[min] = arr[min], arr[i]
}
}
return a
return arr
}
24 changes: 6 additions & 18 deletions algorithms/sorting/selection-sort/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,19 @@ package selection

import (
"fmt"
"github.com/arnauddri/algorithms/algorithms/sorting/utils"
"testing"
)

func TestSelectionSort(t *testing.T) {
list := []int{1, 3, 2, 8, 4, 10, 54, 49, 6}
list := dummy.GetArrayOfSize(100)

sort(list)
sorted_list := []int{1, 2, 3, 4, 6, 8, 10, 49, 54}

if !deepEqual(list, sorted_list) {
fmt.Println(list)
t.Error()
}
}

func deepEqual(a, b []int) bool {
if len(a) != len(b) {
return false
}

for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
for i := 0; i < len(list)-2; i++ {
if list[i] > list[i+1] {
fmt.Println(list)
t.Error()
}
}

return true
}
Loading

0 comments on commit 592c3d3

Please sign in to comment.