summaryrefslogtreecommitdiff
path: root/sort/selection.go
diff options
context:
space:
mode:
Diffstat (limited to 'sort/selection.go')
-rw-r--r--sort/selection.go24
1 files changed, 24 insertions, 0 deletions
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
+}