Skip to content

Instantly share code, notes, and snippets.

@abdivasiyev
Created April 17, 2022 21:27
Show Gist options
  • Save abdivasiyev/3fe252721a408e38de1a4d1330d0047c to your computer and use it in GitHub Desktop.
Save abdivasiyev/3fe252721a408e38de1a4d1330d0047c to your computer and use it in GitHub Desktop.
Selection sort algorithm implementation
package sorting
// Selection sorts given list
func Selection(list []int) []int {
// return list if empty or has single element
if len(list) <= 1 {
return list
}
// iterate list elements
for i := 0; i < len(list)-1; i++ {
// get current index as minimum element index
var minIndex = i
// find minimum value from right side elements
for j := i + 1; j < len(list); j++ {
if list[j] < list[minIndex] {
minIndex = j
}
}
// swap minimum element with current element
list[i], list[minIndex] = list[minIndex], list[i]
}
return list
}
package sorting
import (
"reflect"
"testing"
)
func TestSelectionSort(t *testing.T) {
testCases := []struct {
name string
list []int
expected []int
}{
{
name: "Sort with empty list",
list: []int{},
expected: []int{},
},
{
name: "Sort with sorted list",
list: []int{1, 2, 3, 4, 5, 6, 7, 8},
expected: []int{1, 2, 3, 4, 5, 6, 7, 8},
},
{
name: "Sort with small list",
list: []int{3, 4, 1},
expected: []int{1, 3, 4},
},
{
name: "Sort with medium list",
list: []int{1, 9, 0, 2, 3, 5, 7, 8},
expected: []int{0, 1, 2, 3, 5, 7, 8, 9},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := Selection(tc.list)
if !reflect.DeepEqual(result, tc.expected) {
t.Fatalf("got %v, expected %v", result, tc.expected)
}
})
}
}
func BenchmarkSelectionSort(b *testing.B) {
testCases := []struct {
name string
numbers []int
}{
{
name: "benchmark for 5 elements",
numbers: []int{3, 4, 2, 1, 5},
},
{
name: "benchmark for 10 elements",
numbers: []int{10, 20, 1, 39, 100, -1, -10, -92, -100, -1000},
},
{
name: "benchmark for 20 elements",
numbers: []int{
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
},
},
{
name: "benchmark for 1000 elements",
numbers: []int{
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000,
10, 20, 1,
},
}}
for _, tt := range testCases {
b.Run(tt.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
Selection(tt.numbers)
}
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment