fun findMin(array: MutableList<Int>, begin: Int): Int {
var minIndex = begin
for (i in begin + 1 until array.count()) {
if (array[i] < array[minIndex]) {
minIndex = i
}
}
return minIndex
}
fun selectionSort(array: MutableList<Int>) {
for (i in array.indices) {
val minIndex = findMin(array, i)
val tmp = array[i]
array[i] = array[minIndex]
array[minIndex] = tmp
}
}
fun main() {
val array = mutableListOf(7, 3, 0, 1, 5, 2, 5, 19, 10, 5)
selectionSort(array)
println(array)
}
ZnVuIGZpbmRNaW4oYXJyYXk6IE11dGFibGVMaXN0PEludD4sIGJlZ2luOiBJbnQpOiBJbnQgewogICAgdmFyIG1pbkluZGV4ID0gYmVnaW4KICAgIAogICAgZm9yIChpIGluIGJlZ2luICsgMSB1bnRpbCBhcnJheS5jb3VudCgpKSB7CiAgICAgICAgaWYgKGFycmF5W2ldIDwgYXJyYXlbbWluSW5kZXhdKSB7CiAgICAgICAgICAgIG1pbkluZGV4ID0gaQogICAgICAgIH0KICAgIH0KCiAgICByZXR1cm4gbWluSW5kZXgKfQoKZnVuIHNlbGVjdGlvblNvcnQoYXJyYXk6IE11dGFibGVMaXN0PEludD4pIHsKICAgIGZvciAoaSBpbiBhcnJheS5pbmRpY2VzKSB7CiAgICAgICAgdmFsIG1pbkluZGV4ID0gZmluZE1pbihhcnJheSwgaSkKICAgICAgICB2YWwgdG1wID0gYXJyYXlbaV0KICAgICAgICBhcnJheVtpXSA9IGFycmF5W21pbkluZGV4XQogICAgICAgIGFycmF5W21pbkluZGV4XSA9IHRtcAogICAgfQp9CgpmdW4gbWFpbigpIHsKICAgIHZhbCBhcnJheSA9IG11dGFibGVMaXN0T2YoNywgMywgMCwgMSwgNSwgMiwgNSwgMTksIDEwLCA1KQoKICAgIHNlbGVjdGlvblNvcnQoYXJyYXkpCgogICAgcHJpbnRsbihhcnJheSkKfQ==