I decided to try Kotlin because of its indulgent nature for long time Java users. I've implemented some introductory sorting algorithms and would like to see them again, not only to make sure that I apply Kotlin correctly, but also to improve my style and eliminate any bugs.

I've used some extension functions to make the code more readable and all relevant source codes will be posted in this issue.

**Note:** I also called it Java, because a person familiar with Java can easily access Kotlin if she sees how easy it is to read Kotlin. Anything that is applicable in Java can almost always be applied directly to the Kotlin code.

## Main files

```
+ - meta
| + - annotations
| | + - ComparisonSort
| | + - StableSort
| + - Complexity
+ - sort
| + - AbstractSort
| + - BubbleSort
| + - InsertionSort
| + - MergeSort
| + - QuickSort
| + - SelectionSort
+ - util
+ - ArrayHelper
```

## Tests

```
+ - sort
| + - AbstractSortTest
| + - BubbleSortTest
| + - InsertionSortTest
| + - MergeSortTest
| + - QuickSortTest
| + - SelectionSortTest
+ - util
+ - ArrayHelperTest
```

In addition to being undocumented (I have not added comments because I think the code is already readable enough), I think my code might be a little better organized. If I forgot to include some test cases, thank you for letting me know. General comments on all potential problems, preferences are always welcome.

## ArrayHelper.kt

Note that all functions here are extension functions. These methods are added to the existing methods in the `Array.kt`

class provided in the library.

```
package com.github.hungrybluedev.util
import kotlin.random.Random
fun Board.swap (i: Int, j: Int) {
val tmp = this[i]
this[i] = this[j]
this[j] = tmp
}
fun Board.shuffle () {
shuffle (0, size)
}
fun Board. Shuffle (bottom: Int, up: Int) {
for (index down + 2 up to high) {
swap (index, Random.nextInt (index))
}
}
fun <T: comparable> Table.isSorted (): Boolean {
return isSorted (0, size)
}
fun <T: comparable> Table.isSorted (
lower: Int,
Above: Int
): Boolean {
for (index down + 1 up to high) {
if this[index - 1] > this[index]) {
false returns
}
}
returns true
}
fun Board.isSorted (
comparator: comparator,
lower: Int,
Above: Int
): Boolean {
for (index down + 1 up to high) {
if (comparator.compare (this[index - 1], this[index])> 0) {
false returns
}
}
returns true
}
```

## AbstractSort.kt

```
package com.github.hungrybluedev.sort
import com.github.hungrybluedev.meta.Complexity
AbstractSort abstract class (complexity of val: complexity) {
fun <T: comparable> so (arr: Array) {
sort (arr, naturalOrder (), 0, arr.size)
}
fun <T: comparable> so (arr: Array, lower: Int, upper: Int) {
sort (arr, naturalOrder (), lower, upper)
}
fun spell (arr: Array, comparator: comparator) {
sort (arr, comparator, 0, arr.size)
}
abstract fun Sort(
arr: Array,
comparator: comparator,
lower: Int,
Above: Int
)
}
```

## BubbleSort.kt

```
package com.github.hungrybluedev.sort
import com.github.hungrybluedev.meta.Complexity
import com.github.hungrybluedev.meta.annotatioins.ComparisonSort
import com.github.hungrybluedev.meta.annotatioins.StableSort
import com.github.hungrybluedev.util.swap
@ComparisonSort
@StableSort
BubbleSort class: AbstractSort (Complexity.QUADRATIC) {
to override the pleasure Sort(
arr: Array,
comparator: comparator,
lower: Int,
Above: Int
) {
var sorted = 0
make {
permuted var = false
for (i down to high - 1 - sorted) {
if (comparator.compare (arr[i]arr[i + 1])> 0) {
arr.swap (i, i + 1)
permuted = true
}
}
sorted ++
} while (traded)
}
}
```

## InsertionSort.kt

```
package com.github.hungrybluedev.sort
import com.github.hungrybluedev.meta.Complexity
import com.github.hungrybluedev.meta.annotatioins.ComparisonSort
import com.github.hungrybluedev.meta.annotatioins.StableSort
@StableSort
@ComparisonSort
class InsertionSort: AbstractSort (Complexity.QUADRATIC) {
to override the pleasure Sort(
arr: Array,
comparator: comparator,
lower: Int,
Above: Int
) {
for (at the bottom + 1 until high) {
val = arr[j]
var i = j - 1
while (i> = lower && comparator.compare (key, arr[i]) <0) {
arr[i + 1] = arr[i--]
}
arr[i + 1] = key
}
}
}
```

## MergeSort.kt

```
package com.github.hungrybluedev.sort
import com.github.hungrybluedev.meta.Complexity
import com.github.hungrybluedev.meta.annotatioins.ComparisonSort
import com.github.hungrybluedev.meta.annotatioins.StableSort
@StableSort
@ComparisonSort
MergeSort Class: AbstractSort (Complexity.LINEARITHMIC) {
private pleasure merge(
from: Array,
to: table,
p: Int,
q: Int,
r: Int,
comparator: comparator
) {
var i = p
var j = p
var k = q
while (i <q && k <r) {
at[j++] = if (comparator.compare (from[i], from[k]) <0)
of[i++]
other
of[k++]
}
while (i <q) {
at[j++] = of[i++]
}
while (k <r) {
at[j++] = of[k++]
}
}
private pleasure internalSort (
arrA: painting,
arrB: painting,
comparator: comparator,
p: Int,
r: Int
) {
if (r - p <= 1) {
return
}
val q = p + ((r - p) shr 1)
internalSort (arrB, arrA, comparator, p, q)
internalSort (arrB, arrA, comparator, q, r)
fusion (arrB, arrA, p, q, r, comparator)
}
to override the pleasure Sort(
arr: Array,
comparator: comparator,
lower: Int,
Above: Int
) {
val copy = arr.clone ()
internalSort (arr, copy, comparator, lower, upper)
}
}
```

## QuickSort.kt

```
package com.github.hungrybluedev.sort
import com.github.hungrybluedev.meta.Complexity
import com.github.hungrybluedev.meta.annotatioins.ComparisonSort
import com.github.hungrybluedev.util.swap
import kotlin.random.Random
@ComparisonSort
class QuickSort: AbstractSort (Complexity.LINEARITHMIC) {
private pleasure partition(
arr: Array,
lower: Int,
Above: Int
comparator: comparator
): Int {
val pivot = arr[lower + Random.nextInt(upper - lower + 1)]
var p = lower - 1
var q = higher + 1
while (true) {
while (comparator.compare (arr[++p], pivot) < 0);
while (comparator.compare(arr[--q], pivot) > 0);
if (p> = q) {
back q
}
arr.swap (p, q)
}
}
private pleasure internalSort (
arr: Array,
comparator: comparator,
lower: Int,
Above: Int
) {
if (lower> = higher) {
return
}
val pivot = partition (arr, lower, upper, comparator)
internalSort (arr, comparator, inferior, pivot)
internalSort (arr, comparator, pivot + 1, upper)
}
to override the pleasure Sort(
arr: Array,
comparator: comparator,
lower: Int,
Above: Int
) {
internalSort (arr, comparator, lower, upper - 1)
}
}
```

## Complexity.kt

```
package com.github.hungrybluedev.meta
Enum class Complexity {
lOGARITHMIC,
LINEAR,
LINEARITHMIQUE,
QUADRATIC,
CUBIC
}
```

## ComparisonSort.kt

```
com.github.hungrybluedev.meta.annotatioins package
ComparisonSort annotation class
```

## StableSortSort.kt

```
com.github.hungrybluedev.meta.annotatioins package
StableSort annotation class
```

## AbstractSortTest.kt

```
package com.github.hungrybluedev.sort
import com.github.hungrybluedev.meta.Complexity
import com.github.hungrybluedev.util.isSorted
import com.github.hungrybluedev.util.shuffle
import org.junit.jupiter.api.Assertions.assertArrayEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import kotlin.random.Random
AbstractSortTest abstract class(private yard sorter: T) {
size of the private value = when (sorter.complexity) {
Complexity.QUADRATIC -> 20_000
Complexity.LINEARITHMIC -> 1_000_000
otherwise -> 10_000
}
@Test
internal fun emptyTest () {
val arr = arrayOf()
sorter.sort (arr)
assertArrayEquals (arrayOf(), arr)
}
@Test
internal fun singleElementTest () {
val arr = arrayOf (1)
sorter.sort (arr)
assertArrayEquals (arrayOf (1), arr)
}
@Test
internal fun sortElementsTest () {
val arr = arrayOf (1, 2, 5, 7)
sorter.sort (arr)
assertArrayEquals (arrayOf (1, 2, 5, 7), arr)
}
@Test
internal fun unsortedElementsTest () {
val arr = arrayOf (7, 2, 5, 1)
sorter.sort (arr)
assertArrayEquals (arrayOf (1, 2, 5, 7), arr)
}
@Test
internal fun reverseOrderTest () {
val arr = arrayOf (9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
sorter.sort (arr)
assertArrayEquals (arrayOf (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), arr)
}
@Test
internal fun partialSortTest () {
val arr = arrayOf (9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
sorter.sort (arr, 4, 9)
assertArrayEquals (arrayOf (9, 8, 7, 6, 1, 2, 3, 4, 5, 0), arr)
}
@Test
Internal Fun ComparatorDescendingTest () {
val arr = arrayOf (9, 3, 4, 6, 2, 1, 0, 5, 7, 8)
sorter.sort (arr, reverseOrder ())
assertArrayEquals (arrayOf (9, 8, 7, 6, 5, 4, 3, 2, 1, 0), arr)
}
@Test
Internal Fun ComparatorPartialDescendingTest () {
val arr = arrayOf (3, 0, 1, 2, 4, 8, 5, 9, 7, 6)
sorter.sort (arr, 0, 5)
sorter.sort (arr, reverseOrder (), 5, 10)
assertArrayEquals (arrayOf (0, 1, 2, 3, 4, 9, 8, 7, 6, 5), arr)
}
@Test
internal fun repeatElementsTest () {
val arr = arrayOf (0, 0, 1, 2, 3, 3, 4, 4, 4, 4, 4, 5, 6, 7, 8, 8, 9, 9, 9, 9)
val cpy = arr.clone ()
cpy.shuffle ()
sorter.sort (cpy)
assertArrayEquals (arr, cpy)
}
@Test
internal fun randomDistinctArrayTest () {
val arr = Tableau (size) {x -> x}
sorter.sort (arr)
assertTrue (arr.isSorted ())
}
@Test
internal randomRepeatedTest () {
val arr = Array (size) {Random.nextInt (size)}
sorter.sort (arr)
assertTrue (arr.isSorted ())
}
@Test
internal fun descendingTest () {
val arr = Tableau (size) {x -> size - x}
sorter.sort (arr)
assertTrue (arr.isSorted ())
}
}
```

## BubbleSortTest.kt

```
package com.github.hungrybluedev.sort
BubbleSortTest internal class: AbstractSortTest(BubbleSort ())
```

## InsertionSortTest.kt

```
package com.github.hungrybluedev.sort
internal class InsertionSortTest: AbstractSortTest(Insert Sort ())
```

## MergeSortTest.kt

```
package com.github.hungrybluedev.sort
MergeSortTest internal class: AbstractSortTest(Merge sort ())
```

## QuickSortTest.kt

```
package com.github.hungrybluedev.sort
QuickSortTest internal class: AbstractSortTest(Fast sort ())
```

## SelectionSortTest.kt

```
package com.github.hungrybluedev.sort
SelectionSortTest internal class: AbstractSortTest(SelectionSort ())
```

## ArrayHelperTest.kt

```
package com.github.hungrybluedev.util
import org.junit.jupiter.api.Assertions.assertFalse
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
internal class ArrayHelperTest {
@Test
internal fun smallAscendingTest () {
val arr = arrayOf (1, 2, 3, 4, 5)
assertTrue (arr.isSorted ())
assertTrue (arr.isSorted (0, arr.size))
assertTrue (arr.isSorted (naturalOrder (), 0, arr.size))
}
@Test
internal fun smallDescendingTest () {
val arr = arrayOf (9, 7, 6, 4, 3, 2)
assertFalse (arr.isSorted ())
assertFalse (arr.isSorted (0, arr.size))
assertTrue (arr.isSorted (reverseOrder (), 0, arr.size))
}
}
```