summaryrefslogtreecommitdiff
path: root/sort/selection.go
blob: e2a774cacb49f16d20cfd3fafe1fc30e3312973c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
}