Skip to content

Commit

Permalink
Added InsertionSort in Kotlin (Issue #43)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhoomchai L committed Oct 27, 2020
1 parent a724f1a commit 5bbba1f
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: Array<Int>) {
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 = arrayOf(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 5bbba1f

Please sign in to comment.