diff --git a/sorting/int.go b/sorting/int.go new file mode 100644 index 0000000..c85df5a --- /dev/null +++ b/sorting/int.go @@ -0,0 +1,15 @@ +package sorting + +type IntSort []int + +func (items IntSort) Len() int { + return len(items) +} + +func (items IntSort) Swap(i, j int) { + items[i], items[j] = items[j], items[i] +} + +func (items IntSort) Less(i, j int) bool { + return items[i] < items[j] +} diff --git a/sorting/selection.go b/sorting/selection.go new file mode 100644 index 0000000..70ca66a --- /dev/null +++ b/sorting/selection.go @@ -0,0 +1,20 @@ +package sorting + +type selection struct{} + +func (*selection) Sort(items Sortable) { + len := items.Len() + for i := 0; i < len; i++ { + min := i + for j := i + 1; j < len; j++ { + if items.Less(j, min) { + min = j + } + } + items.Swap(min, i) + } +} + +func NewSelection() Sorter { + return &selection{} +} diff --git a/sorting/sort.go b/sorting/sort.go new file mode 100644 index 0000000..970a9e2 --- /dev/null +++ b/sorting/sort.go @@ -0,0 +1,13 @@ +package sorting + +type Sortable interface { + Len() int + Swap(i, j int) + Less(i, j int) bool +} + +type Sorter interface { + Sort(Sortable) + // TODO: add generic slice sort when type variables are landed + // SortSlice[T any](T, func(i, j int) bool) +} diff --git a/sorting/sort_test.go b/sorting/sort_test.go new file mode 100644 index 0000000..24b6d0e --- /dev/null +++ b/sorting/sort_test.go @@ -0,0 +1,9 @@ +package sorting + +import ( + "testing" +) + +func TestSelection(t *testing.T) { + CheckSorter(NewSelection()) +} diff --git a/sorting/testing.go b/sorting/testing.go new file mode 100644 index 0000000..b5ec50b --- /dev/null +++ b/sorting/testing.go @@ -0,0 +1,37 @@ +package sorting + +import ( + "log" + "math/rand" + "sort" + "time" +) + +func SameInts(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 + } + } + + return true +} + +func CheckSorter(s Sorter) { + rand.Seed(time.Now().Unix()) + + actual := rand.Perm(100) + expected := make([]int, len(actual)) + copy(expected, actual) + + s.Sort(IntSort(actual)) + sort.Sort(IntSort(expected)) + + if !SameInts(actual, expected) { + log.Fatalf("wrong order:\n actual:\t%v\n expected:\t%v\n", actual, expected) + } +}