diff --git a/sort/selection.jule b/sort/selection.jule new file mode 100644 index 0000000..8135cc8 --- /dev/null +++ b/sort/selection.jule @@ -0,0 +1,16 @@ +fn Selection[T: ordered](mut arr: []T): []T { + mut i := 0 + for i < len(arr); i++ { + mut minIdx := i + mut j := i + 1 + for j < len(arr); j++ { + if arr[j] < arr[minIdx] { + minIdx = j + } + } + if minIdx != i { + arr[i], arr[minIdx] = arr[minIdx], arr[i] + } + } + return arr +} \ No newline at end of file diff --git a/sort/testcases_test.jule b/sort/testcases_test.jule index b1140f0..a5bf832 100644 --- a/sort/testcases_test.jule +++ b/sort/testcases_test.jule @@ -140,6 +140,11 @@ fn testShell(t: &testing::T) { testGeneral(t, Shell[int]) } +#test +fn testSelection(t: &testing::T) { + testGeneral(t, Selection[int]) +} + #test fn testQuicksort(t: &testing::T) { testGeneral(t, Quicksort[int])