fork download
  1. fun insertionSort(array: MutableList<Int>) {
  2. for (i in 1 until array.count()) {
  3. var j = i
  4. while (j > 0 && array[j] < array[j - 1]) {
  5. val tmp = array[j]
  6. array[j] = array[j - 1]
  7. array[j - 1] = tmp
  8. j--
  9. }
  10. }
  11. }
  12.  
  13. fun main() {
  14. val array = mutableListOf(7, 3, 0, 1, 5, 2, 5, 19, 10, 5)
  15.  
  16. insertionSort(array)
  17.  
  18. println(array)
  19. }
Success #stdin #stdout 0.09s 39420KB
stdin
Standard input is empty
stdout
[0, 1, 2, 3, 5, 5, 5, 7, 10, 19]