Skip to content

Commit b79a7f1

Browse files
committed
added Selection Sort
1 parent 63f73c9 commit b79a7f1

File tree

5 files changed

+87
-0
lines changed

5 files changed

+87
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")
2+
3+
kt_jvm_library(
4+
name = "selection_sort_lib",
5+
srcs = glob([
6+
"*.kt",
7+
"_measurement/*.kt"
8+
]),
9+
deps = [
10+
"//sequential/sorting/_measurement:measurement",
11+
"//:utils",
12+
]
13+
)
14+
15+
java_binary(
16+
name = "selection_sort",
17+
main_class = "sequential.sorting.selectionsort.SelectionSortKt",
18+
runtime_deps = [":selection_sort_lib"],
19+
)
20+
21+
java_binary(
22+
name = "selection_sort_measured",
23+
main_class = "sequential.sorting.selectionsort._measurement.MeasuredSelectionSortKt",
24+
runtime_deps = [":selection_sort_lib"],
25+
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Selection Sort
2+
...
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package sequential.sorting.selectionsort
2+
3+
import _util.exch
4+
import _util.isSorted
5+
import _util.print
6+
import _util.randomIntArray
7+
8+
9+
fun IntArray.selectionSort() {
10+
for (i in 0..lastIndex) {
11+
var minIndex = i
12+
for (j in i + 1..lastIndex)
13+
if (this[j] < this[minIndex])
14+
minIndex = j
15+
exch(i, minIndex)
16+
}
17+
}
18+
19+
20+
fun main() {
21+
val array = randomIntArray(size = 20)
22+
array.print()
23+
24+
array.selectionSort()
25+
26+
println("Sorting is successful: ${array.isSorted()}")
27+
array.print()
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package sequential.sorting.selectionsort._measurement
2+
3+
import _util.isSorted
4+
import _util.print
5+
import _util.randomIntArray
6+
import sequential.sorting._measurement.measured
7+
8+
9+
fun IntArray.selectionSort() = measured {
10+
for (i in 0..lastIndex) {
11+
var minIndex = i
12+
for (j in i + 1..lastIndex)
13+
if (this[j] < this[minIndex])
14+
minIndex = j
15+
exch(i, minIndex)
16+
}
17+
}
18+
19+
20+
fun main() {
21+
val array = randomIntArray(size = 20)
22+
array.print()
23+
24+
val measurement = array.selectionSort()
25+
26+
println("Sorting is successful: ${array.isSorted()}")
27+
array.print()
28+
29+
println("$measurement")
30+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package sequential.sorting.selectionsort._test
2+

0 commit comments

Comments
 (0)