Skip to content

Commit

Permalink
Merge pull request #206 from bhoomchai/insert-sort-kotlin
Browse files Browse the repository at this point in the history
Added InsertionSort in Kotlin (Issue #43)
  • Loading branch information
Anuja-19 authored Oct 29, 2020
2 parents 92ab618 + 5651bdd commit fd5722f
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Kotlin/InsertionSort.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
fun insertionSort(array: IntArray) {
for (i in array.indices) {
val tmp = array[i]
var j = i
while(j > 0 && array[j-1] > tmp){
array[j] = array[j-1]
j -= 1
}
array[j] = tmp
}
}

fun main() {
var input = intArrayOf(42,27,5,7,0,15,2,99,1)
print("Original list: ")
input.iterator().forEach { print("$it ") }
println()
print("Sorted list: ")
insertionSort(input)
input.iterator().forEach { print("$it ") }
}

0 comments on commit fd5722f

Please sign in to comment.