Skip to content

Instantly share code, notes, and snippets.

@MaksimDmitriev
Last active October 30, 2023 11:01
Show Gist options
  • Select an option

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

Select an option

Save MaksimDmitriev/bf84df0f480841e4df7438a04dc3cfae to your computer and use it in GitHub Desktop.
SelectionSort
package ru.maksim.sample
import org.junit.jupiter.api.Test
class SelectionSortTest {
@Test
fun foo() {
val arr = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
selectionSort(arr)
println(arr.contentToString())
}
@Test
fun foo_2() {
val arr = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
arr.shuffle()
selectionSort(arr)
println(arr.contentToString())
}
private fun selectionSort(arr: IntArray) {
for (i in 0 until arr.lastIndex) {
var minIndex = i
for (j in i + 1..arr.lastIndex) {
if (arr[j] < arr[minIndex]) {
minIndex = j
}
}
val temp = arr[i]
arr[i] = arr[minIndex]
arr[minIndex] = temp
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment