File tree Expand file tree Collapse file tree 5 files changed +87
-0
lines changed
src/kotlin/sequential/sorting/selectionsort Expand file tree Collapse file tree 5 files changed +87
-0
lines changed Original file line number Diff line number Diff line change
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
+ )
Original file line number Diff line number Diff line change
1
+ # Selection Sort
2
+ ...
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ package sequential.sorting.selectionsort._test
2
+
You can’t perform that action at this time.
0 commit comments