Popular sorting and searching algorithms implemented in Kotlin. Most of them
are written as generic extension functions (for example
fun <E: Comparable<E>> Array<E>.quickSort()), so they sort numbers, strings,
or anything else that is comparable. Each file has its own main() with a
small demo that prints its result.
| File | Algorithm | Average time |
|---|---|---|
Bubble sort.kt |
Bubble sort | O(n²) |
Selection sort.kt |
Selection sort | O(n²) |
Insertion sort.kt |
Insertion sort | O(n²) |
Merge sort.kt |
Merge sort | O(n log n) |
Quick sort.kt |
Quick sort (Lomuto partition) | O(n log n) |
Heap sort.kt |
Heap sort | O(n log n) |
Linear search.kt |
Linear search | O(n) |
Binary search.kt |
Binary search on a sorted array | O(log n) |
git clone https://github.com/NinjaShadowBoy/AlgorithmsWithKotlin.git
cd AlgorithmsWithKotlinIn IntelliJ IDEA: open the folder, then click the run icon next to the
main() of the file you want to try.
From the command line, with the Kotlin compiler installed, compile and run one file at a time:
kotlinc "Quick sort.kt" -include-runtime -d quicksort.jar
java -jar quicksort.jarExample output:
Ascending Order
[2, 12, 12, 23, 43, 76, 89]
[C, C++, Java, Kotlin, Matlab, Python, R]
The out/ folder is IntelliJ's compiled output and can be ignored.