From 4edf2dd20b300c27dcc96829e262aedcd438291a Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sun, 12 Jul 2020 11:35:03 +0100 Subject: have selection sort --- sort/selection.go | 24 ++++++++++++++++++++++++ sort/selection_test.go | 12 ++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 sort/selection.go create mode 100644 sort/selection_test.go (limited to 'sort') diff --git a/sort/selection.go b/sort/selection.go new file mode 100644 index 0000000..e2a774c --- /dev/null +++ b/sort/selection.go @@ -0,0 +1,24 @@ +package sort + +import ( + "algorithms/ds" +) + +func Selection(a []ds.Comparer) []ds.Comparer { + length := len(a) + for i := 0; i < length; i++ { + max := i + for j := i + 1; j < length; j++ { + if a[max].HigherThan(a[j]) { + max = j + } + } + if max == i { + continue + } + tmp := a[i] + a[i] = a[max] + a[max] = tmp + } + return a +} diff --git a/sort/selection_test.go b/sort/selection_test.go new file mode 100644 index 0000000..3881850 --- /dev/null +++ b/sort/selection_test.go @@ -0,0 +1,12 @@ +package sort + +import ( + "algorithms/ds" + "testing" +) + +func BenchmarkSelection1000(b *testing.B) { + a := ds.RandomIntegers(1000, 1000) + b.ResetTimer() + Selection(a) +} -- cgit v1.2.3