Skip to content

Commit 0e7f309

Browse files
authored
sort: add Selection (#54)
1 parent 72160b4 commit 0e7f309

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

sort/selection.jule

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
fn Selection[T: ordered](mut arr: []T): []T {
2+
mut i := 0
3+
for i < len(arr); i++ {
4+
mut minIdx := i
5+
mut j := i + 1
6+
for j < len(arr); j++ {
7+
if arr[j] < arr[minIdx] {
8+
minIdx = j
9+
}
10+
}
11+
if minIdx != i {
12+
arr[i], arr[minIdx] = arr[minIdx], arr[i]
13+
}
14+
}
15+
return arr
16+
}

sort/testcases_test.jule

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,11 @@ fn testShell(t: &testing::T) {
140140
testGeneral(t, Shell[int])
141141
}
142142

143+
#test
144+
fn testSelection(t: &testing::T) {
145+
testGeneral(t, Selection[int])
146+
}
147+
143148
#test
144149
fn testQuicksort(t: &testing::T) {
145150
testGeneral(t, Quicksort[int])

0 commit comments

Comments
 (0)