Skip to content

Commit 63f73c9

Browse files
committed
added Bubble Sort
1 parent 1a6ca5b commit 63f73c9

File tree

8 files changed

+139
-2
lines changed

8 files changed

+139
-2
lines changed

src/kotlin/sequential/sorting/_measurement/SortMeasurement.kt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,19 @@ abstract class MeasuredCollection<T : Comparable<T>> {
1919

2020
fun exch(i: Int, j: Int) {
2121
exchanges++
22-
this[i] = this[j].also { this[j] = this[i] }
22+
this[i, false] = this[j].also { this[j, false] = this[i] }
2323
}
2424

2525
fun onCompare() {
2626
comparisons++
2727
}
2828

2929
operator fun set(index: Int, value: MeasuredElement<T>) {
30-
halfExchanges++
30+
this[index, true] = value
31+
}
32+
33+
operator fun set(index: Int, registerHalfExch: Boolean, value: MeasuredElement<T>) {
34+
if (registerHalfExch) halfExchanges++
3135
setAt(index, value)
3236
}
3337

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")
2+
3+
kt_jvm_library(
4+
name = "bubble_sort_lib",
5+
srcs = glob([
6+
"*.kt",
7+
"_measurement/*.kt",
8+
]),
9+
deps = [
10+
"//sequential/sorting/_measurement:measurement",
11+
"//:utils",
12+
],
13+
visibility = ["//visibility:public"],
14+
)
15+
16+
java_binary(
17+
name = "bubble_sort",
18+
main_class = "sequential.sorting.bubblesort.BubbleSortKt",
19+
runtime_deps = [":bubble_sort_lib"],
20+
)
21+
22+
java_binary(
23+
name = "bubble_sort_ranged",
24+
main_class = "sequential.sorting.bubblesort.ranged.RangedBubbleSortKt",
25+
runtime_deps = [":bubble_sort_lib"],
26+
)
27+
28+
java_binary(
29+
name = "bubble_sort_measured",
30+
main_class = "sequential.sorting.bubblesort._measurement.MeasuredBubbleSortKt",
31+
runtime_deps = [":bubble_sort_lib"],
32+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package sequential.sorting.bubblesort
2+
3+
import _util.exch
4+
import _util.isSorted
5+
import _util.print
6+
import _util.randomIntArray
7+
8+
9+
fun IntArray.bubbleSort() {
10+
for (i in 0..lastIndex) {
11+
var exchanged = false
12+
for (j in 0 until lastIndex - i) {
13+
if (this[j + 1] < this[j]) {
14+
exch(j + 1, j)
15+
exchanged = true
16+
}
17+
}
18+
if (!exchanged) return
19+
}
20+
}
21+
22+
23+
fun main() {
24+
val array = randomIntArray(size = 20)
25+
array.print()
26+
27+
array.bubbleSort()
28+
29+
println("Sorting is successful: ${array.isSorted()}")
30+
array.print()
31+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Bubble Sort
2+
...
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package sequential.sorting.bubblesort.ranged
2+
3+
import _util.exch
4+
import _util.isSorted
5+
import _util.print
6+
import _util.randomIntArray
7+
8+
9+
fun IntArray.bubbleSort(start: Int, end: Int) {
10+
if (end - start < 1) return
11+
12+
for (i in start until end) {
13+
var exchanged = false
14+
for (j in start until (end - 1) - (i - start)) {
15+
if (this[j + 1] < this[j]) {
16+
exch(j + 1, j)
17+
exchanged = true
18+
}
19+
}
20+
if (!exchanged) return
21+
}
22+
}
23+
24+
25+
fun main() {
26+
val array = randomIntArray(size = 20)
27+
array.print()
28+
29+
array.bubbleSort(0, array.size)
30+
31+
println("Sorting is successful: ${array.isSorted()}")
32+
array.print()
33+
}

src/kotlin/sequential/sorting/bubblesort/Stub.kt

Whitespace-only changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package sequential.sorting.bubblesort._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.bubbleSort() = measured {
10+
for (i in 0..lastIndex) {
11+
var exchanged = false
12+
for (j in 0 until lastIndex - i) {
13+
if (this[j + 1] < this[j]) {
14+
exch(j + 1, j)
15+
exchanged = true
16+
}
17+
}
18+
if (!exchanged) return@measured
19+
}
20+
}
21+
22+
23+
fun main() {
24+
val array = randomIntArray(size = 20)
25+
array.print()
26+
27+
val measurement = array.bubbleSort()
28+
29+
println("Sorting is successful: ${array.isSorted()}")
30+
array.print()
31+
32+
println("$measurement")
33+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package sequential.sorting.bubblesort._test
2+

0 commit comments

Comments
 (0)