-
Notifications
You must be signed in to change notification settings - Fork 376
/
selection_sort_test.go
58 lines (49 loc) · 1.13 KB
/
selection_sort_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
Problem:
- Implement selection sort.
Approach:
- Repeatedly select the next smallest element from the unsorted array and
move it to the front.
Cost:
- O(n^2) time and O(1) space.
*/
package lab
import (
"testing"
"github.com/hoanhan101/algo/common"
)
func TestSelectionSort(t *testing.T) {
tests := []struct {
in []int
expected []int
}{
{[]int{}, []int{}},
{[]int{1}, []int{1}},
{[]int{1, 2}, []int{1, 2}},
{[]int{2, 1}, []int{1, 2}},
{[]int{2, 1, 3}, []int{1, 2, 3}},
{[]int{1, 1, 1}, []int{1, 1, 1}},
{[]int{2, 1, 2}, []int{1, 2, 2}},
{[]int{1, 2, 4, 3, 6, 5}, []int{1, 2, 3, 4, 5, 6}},
{[]int{6, 2, 4, 3, 1, 5}, []int{1, 2, 3, 4, 5, 6}},
{[]int{6, 5, 4, 3, 2, 1}, []int{1, 2, 3, 4, 5, 6}},
}
for _, tt := range tests {
selectionSort(tt.in)
common.Equal(t, tt.expected, tt.in)
}
}
func selectionSort(in []int) {
minIndex := 0
for i := 0; i < len(in)-1; i++ {
minIndex = i
// find the minimum in the rest of the array.
for j := i + 1; j < len(in); j++ {
if in[j] < in[minIndex] {
minIndex = j
}
}
// swap the minimum value with the first value.
common.Swap(in, i, minIndex)
}
}