diff options
| author | mo khan <mo@mokhan.ca> | 2021-09-19 16:05:05 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2021-09-19 16:05:05 -0600 |
| commit | abfa0ef8b42197f33a4c1d437b4fa416aa124806 (patch) | |
| tree | e60c731f41443207d1a9c716ce585c9e8e6404c0 | |
| parent | 99b8e37803d8b64a0fb7d9a271e12bd02101a466 (diff) | |
write a selection sort
| -rw-r--r-- | exercises/2.2-2/selection_sort_test.go | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/exercises/2.2-2/selection_sort_test.go b/exercises/2.2-2/selection_sort_test.go new file mode 100644 index 0000000..5989f20 --- /dev/null +++ b/exercises/2.2-2/selection_sort_test.go @@ -0,0 +1,31 @@ +package main + +import "testing" + +func Sort(i *[]int) { + items := *i + size := len(items) + + for i := 0; i < size; i++ { + for j := i + 1; j < size; j++ { + if items[j] < items[i] { + items[i], items[j] = items[j], items[i] + } + } + } +} + +func TestSelectionSort(t *testing.T) { + t.Run("Worst case", func(t *testing.T) { + items := []int{10, 9, 8, 7, 6, 5, 4, 3, 2, 1} + + Sort(&items) + + for i := 0; i < len(items); i++ { + expected := i + 1 + if items[i] != expected { + t.Errorf("Expected '%d', got '%d'", expected, items[i]) + } + } + }) +} |
