Skip to content

Instantly share code, notes, and snippets.

@MaksimDmitriev
Created October 28, 2023 08:47
Show Gist options
  • Select an option

  • Save MaksimDmitriev/c213720da7925ff0e584606859a4f5c0 to your computer and use it in GitHub Desktop.

Select an option

Save MaksimDmitriev/c213720da7925ff0e584606859a4f5c0 to your computer and use it in GitHub Desktop.
insertion sort
package ru.maksim.sample
import org.junit.jupiter.api.Test
class SampleKotlinTest {
@Test
fun foo() {
val arr = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
insertionSort(arr)
println(arr.contentToString())
}
@Test
fun foo_2() {
val arr = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
arr.shuffle()
insertionSort(arr)
println(arr.contentToString())
}
private fun insertionSort(arr: IntArray) {
for (i in 1..arr.lastIndex) {
val key = arr[i]
var j = i - 1
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j]
j--
}
arr[j + 1] = key
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment