From 2d7bfe806d7ca1497336d226f7908a5977470955 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Wed, 27 Nov 2024 21:05:58 +0100 Subject: [PATCH 1/4] initial commit --- algorithms/selection.go | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 algorithms/selection.go diff --git a/algorithms/selection.go b/algorithms/selection.go new file mode 100644 index 0000000..c6905d5 --- /dev/null +++ b/algorithms/selection.go @@ -0,0 +1,5 @@ +package algorithms + +func SelectionSort(arr []int) []int { + return arr +} From c2e44bf60d7e90eb8f4ac3140c0f271bcf0072ac Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Wed, 27 Nov 2024 21:10:33 +0100 Subject: [PATCH 2/4] add setup to SelectionSort --- algorithms/selection.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/algorithms/selection.go b/algorithms/selection.go index c6905d5..74d6eec 100644 --- a/algorithms/selection.go +++ b/algorithms/selection.go @@ -1,5 +1,8 @@ package algorithms func SelectionSort(arr []int) []int { + n := len(arr) + result := make([]int, n) + copy(result, arr) return arr } From 72cfae5a218d1e82a3c4e91ed7dce2d2ad07c514 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Wed, 27 Nov 2024 21:14:58 +0100 Subject: [PATCH 3/4] add initial implementation --- algorithms/selection.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/algorithms/selection.go b/algorithms/selection.go index 74d6eec..578433c 100644 --- a/algorithms/selection.go +++ b/algorithms/selection.go @@ -4,5 +4,14 @@ func SelectionSort(arr []int) []int { n := len(arr) result := make([]int, n) copy(result, arr) - return arr + for i := 0; i < n; i++ { + selected := i + for j := i + 1; j < n; j++ { + if result[selected] > result[j] { + selected = j + } + } + result[i], result[selected] = result[selected], result[i] + } + return result } From b9d041cdea92950efde3b6ccc5c115c55da6bb83 Mon Sep 17 00:00:00 2001 From: dimitrijjedich Date: Wed, 27 Nov 2024 21:16:12 +0100 Subject: [PATCH 4/4] add tests for SelectionSort --- algorithms_test/selection_test.go | 33 +++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 algorithms_test/selection_test.go diff --git a/algorithms_test/selection_test.go b/algorithms_test/selection_test.go new file mode 100644 index 0000000..b788f77 --- /dev/null +++ b/algorithms_test/selection_test.go @@ -0,0 +1,33 @@ +package algorithms_test + +import ( + "github.com/dimitrijjedich/go-algorithms/algorithms" + "reflect" + "testing" +) + +func TestSelectionSort(t *testing.T) { + testCases := []struct { + name string + input []int + expected []int + }{ + {"already sorted", []int{1, 2, 3, 4, 5}, []int{1, 2, 3, 4, 5}}, + {"reverse sorted", []int{5, 4, 3, 2, 1}, []int{1, 2, 3, 4, 5}}, + {"unsorted array", []int{5, 3, 8, 6, 2}, []int{2, 3, 5, 6, 8}}, + {"non consecutive array", []int{9, 3, 5, 1, 7}, []int{1, 3, 5, 7, 9}}, + {"empty array", []int{}, []int{}}, + {"single element", []int{42}, []int{42}}, + {"duplicates", []int{3, 1, 2, 3, 1}, []int{1, 1, 2, 3, 3}}, + } + + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + result := algorithms.SelectionSort(testCase.input) + + if !reflect.DeepEqual(result, testCase.expected) { + t.Errorf("Failed %s: expected %v, got %v", testCase.name, testCase.expected, result) + } + }) + } +}