Skip to content

Commit e8a1e0d

Browse files
committed
resolved dependencies
1 parent 537c03c commit e8a1e0d

File tree

22 files changed

+300
-24
lines changed

22 files changed

+300
-24
lines changed

src/kotlin/BUILD

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "define_kt_toolchain")
1+
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "define_kt_toolchain", "kt_jvm_library")
22

33
JVM_VERSION = "1.6"
44

@@ -10,3 +10,13 @@ define_kt_toolchain(
1010
jvm_target = JVM_VERSION,
1111
language_version = KOTLIN_VERSION,
1212
)
13+
14+
kt_jvm_library(
15+
name = "utils",
16+
srcs = [
17+
"_util/Utils.kt",
18+
"//sequential/shuffling/fisheryates:FisherYatesShuffle.kt",
19+
20+
],
21+
visibility = ["//:__subpackages__"]
22+
)

src/kotlin/_util/BUILD

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/kotlin/_util/Stub.kt

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/kotlin/_util/Utils.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package _util
2+
3+
import sequential.shuffling.fisheryates.fisherYatesShuffle
4+
5+
6+
fun IntArray.shuffle() {
7+
fisherYatesShuffle()
8+
}
9+
10+
fun IntArray.swap(i: Int, j: Int) {
11+
this[i] = this[j].also { this[j] = this[i] }
12+
}

src/kotlin/kotlin.bzl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
22

3+
34
rules_kotlin_version = "legacy-1.3.0"
45
rules_kotlin_sha = "4fd769fb0db5d3c6240df8a9500515775101964eebdf85a3f9f0511130885fde"
56

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library", "kt_jvm_binary")
2+
3+
exports_files(
4+
["FisherYatesShuffle.kt"],
5+
visibility = ["//:__pkg__"]
6+
)
7+
8+
kt_jvm_library(
9+
name = "fisheryates_shuffle_lib",
10+
srcs = ["FisherYatesShuffle.kt"],
11+
deps = ["//:utils"],
12+
)
13+
14+
java_binary(
15+
name = "fisheryates_shuffle",
16+
main_class = "sequential.shuffling.fisheryates.FisherYatesShuffleKt",
17+
runtime_deps = [":fisheryates_shuffle_lib"],
18+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package sequential.shuffling.fisheryates
2+
3+
import _util.swap
4+
import kotlin.random.Random
5+
6+
7+
fun IntArray.fisherYatesShuffle() {
8+
for (i in lastIndex downTo 1) {
9+
val j = Random.nextInt(i + 1)
10+
swap(i, j)
11+
}
12+
}
13+
14+
15+
fun main() {
16+
println("FisherYatesShuffle")
17+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Fisher Yates Modern Shuffling
2+
...
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_binary")
2+
3+
kt_jvm_binary(
4+
name = "sattolo_shuffle",
5+
srcs = ["SattoloShuffle.kt"],
6+
main_class = "sequential.shuffling.sattolo.SattoloShuffleKt",
7+
deps = ["//_util:utils"],
8+
)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package sequential.shuffling.sattolo
2+
3+
import sequential.sorting._util.swap
4+
import kotlin.random.Random
5+
6+
7+
fun IntArray.sattoloShuffle() {
8+
for (i in lastIndex downTo 1) {
9+
val j = Random.nextInt(i)
10+
swap(i, j)
11+
}
12+
}
13+
14+
15+
fun main() {
16+
println("SattoloShuffle")
17+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")
2+
3+
package(default_visibility = ["//sequential/sorting:__subpackages__"])
4+
5+
kt_jvm_library(
6+
name = "sort_analysis",
7+
srcs = glob(["*.kt"]),
8+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package sequential.sorting._analysis
2+
3+
typealias PartitioningFunction = IntArray.(start: Int, end: Int) -> Int
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package sequential.sorting._utils
2+
3+
4+
fun IntArray.exch(i: Int, j: Int) {
5+
this[i] = this[j].also { this[j] = this[i] }
6+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")
2+
3+
kt_jvm_library(
4+
name = "insertion_sort_lib",
5+
srcs = [
6+
"InsertionSort.kt",
7+
"ranged/InsertionSort.kt",
8+
],
9+
deps = ["//sequential/sorting/_analysis:sort_analysis"],
10+
visibility = ["//visibility:public"],
11+
)
12+
13+
java_binary(
14+
name = "insertion_sort",
15+
main_class = "sequential.sorting.insertionsort.InsertionSortKt",
16+
runtime_deps = [":insertion_sort_lib"],
17+
)
18+
19+
java_binary(
20+
name = "insertion_sort_ranged",
21+
main_class = "sequential.sorting.insertionsort.ranged.InsertionSortKt",
22+
runtime_deps = [":insertion_sort_lib"],
23+
)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package sequential.sorting.insertionsort
2+
3+
import sequential.sorting._utils.exch
4+
5+
6+
fun IntArray.insertionSort() {
7+
8+
// Setting sentinel
9+
var exchanges = 0
10+
for (i in lastIndex downTo 1) {
11+
if (this[i] < this[i - 1]) {
12+
exch(i, i - 1)
13+
exchanges++
14+
}
15+
}
16+
if (exchanges == 0) return
17+
18+
// Actual insertion sort
19+
for (i in 2..lastIndex) {
20+
var j = i
21+
val arri = this[i]
22+
while (arri < this[j - 1]) {
23+
this[j] = this[j - 1]
24+
j--
25+
}
26+
this[j] = arri
27+
}
28+
}
29+
30+
31+
fun main() {
32+
println("InsertionSort")
33+
}

src/kotlin/sequential/sorting/insertionsort/README.md

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package sequential.sorting.insertionsort.ranged
2+
3+
import sequential.sorting._utils.exch
4+
5+
6+
fun IntArray.insertionSort(start: Int, end: Int) {
7+
if (end - start < 1) return
8+
9+
// Setting sentinel
10+
var exchanges = 0
11+
for (i in end - 1 downTo start + 1) {
12+
if (this[i] < this[i - 1]) {
13+
exch(i, i - 1)
14+
exchanges++
15+
}
16+
}
17+
if (exchanges == 0) return
18+
19+
// Actual insertion sort
20+
for (i in (start + 2) until end) {
21+
var j = i
22+
val arri = this[i]
23+
while (arri < this[j - 1]) {
24+
this[j] = this[j - 1]
25+
j--
26+
}
27+
this[j] = arri
28+
}
29+
}
30+
31+
32+
fun main() {
33+
34+
}
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_binary")
1+
load("@io_bazel_rules_kotlin//kotlin:kotlin.bzl", "kt_jvm_library")
22

3-
kt_jvm_binary(
3+
kt_jvm_library(
4+
name = "quicksort_lib",
5+
srcs = ["QuickSort.kt"] + glob(["partition/*.kt"]),
6+
deps = [
7+
"//sequential/sorting/insertionsort:insertion_sort_lib",
8+
"//sequential/sorting/_analysis:sort_analysis",
9+
"//:utils"
10+
]
11+
)
12+
13+
java_binary(
414
name = "quicksort",
5-
srcs = ["QuickSort.kt"],
615
main_class = "sequential.sorting.quicksort.QuickSortKt",
7-
deps = ["//_util:utils"],
16+
runtime_deps = [":quicksort_lib"],
817
)
Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,57 @@
11
package sequential.sorting.quicksort
22

3-
import _util.testFun
3+
import sequential.sorting.insertionsort.ranged.insertionSort
4+
import sequential.sorting.quicksort.partition.lomutoPartition
5+
import sequential.sorting.quicksort.partition.hoarePartition
6+
import _util.shuffle
47

58

9+
/**
10+
* Minimum threshold for which quick sort algorithm will be applied. Otherwise, insertion sort will be used.
11+
*/
12+
private const val INSERTION_SORT_THRESHOLD = 47
13+
14+
15+
/**
16+
* In-place quick sort for the given array.
17+
*/
618
fun IntArray.quickSort() {
7-
// TODO
19+
shuffle()
20+
quickSort(0, size)
821
}
922

23+
/**
24+
* In-place quick sort for the given range.
25+
*
26+
* It is possible to partition an array by the following algorithms:
27+
* [hoarePartition], [lomutoPartition].
28+
*
29+
* @param start start of the sorting range inclusive.
30+
* @param end end of the sorting range exclusive.
31+
*/
32+
private fun IntArray.quickSort(start: Int, end: Int) {
33+
if (end - start < 2) return
34+
35+
if (end - start <= INSERTION_SORT_THRESHOLD) {
36+
insertionSort(start, end)
37+
return
38+
}
39+
40+
val pivot = partition(start, end)
41+
quickSort(start, pivot)
42+
quickSort(pivot + 1, end)
43+
}
44+
45+
46+
/**
47+
* Currently selected partitioning algorithm which is used in [quickSort].
48+
* Can be [lomutoPartition] or [hoarePartition].
49+
*/
50+
var partition: PartitioningFunction = IntArray::hoarePartition
51+
52+
typealias PartitioningFunction = IntArray.(start: Int, end: Int) -> Int
53+
1054

1155
fun main() {
12-
println("Hey main + ${testFun()}")
56+
println("QuickSort")
1357
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package sequential.sorting.quicksort.partition
2+
3+
import sequential.sorting._utils.exch
4+
5+
6+
/**
7+
* Partitioning algorithm for quick sort introduced by Tony Hoare.
8+
*/
9+
fun IntArray.hoarePartition(start: Int, end: Int): Int {
10+
val pivotPosition = (start + end - 1) / 2
11+
exch(pivotPosition, start)
12+
13+
val pivot = this[start]
14+
var i = start
15+
var j = end
16+
while (true) {
17+
do i++
18+
while (i < end && this[i] < pivot)
19+
20+
do j--
21+
while (this[j] > pivot)
22+
23+
if (i >= j) break
24+
exch(i, j)
25+
}
26+
exch(start, j)
27+
return j
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package sequential.sorting.quicksort.partition
2+
3+
import sequential.sorting._utils.exch
4+
5+
6+
/**
7+
* Partitioning algorithm for quick sort introduced by Nico Lomuto.
8+
*/
9+
fun IntArray.lomutoPartition(start: Int, end: Int): Int {
10+
val pivot = this[end - 1]
11+
var i = start
12+
for (j in start until end) {
13+
if (this[j] < pivot) {
14+
exch(i++, j)
15+
}
16+
}
17+
exch(i, end - 1)
18+
return i
19+
}

0 commit comments

Comments
 (0)