Insertion Sort in Kotlin

Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a time.

It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort.


fun <T:Comparable<T>> insertionsort(items:MutableList<T>):List<T>{
    if (items.isEmpty()){
        return items
    }
    for (count in 1..items.count() - 1){
        val item = items[count]
        var i = count
        while (i>0 && item < items[i - 1]){
            items[i] = items[i - 1]
            i -= 1
        }
        items[i] = item
    }
    return items
}
fun main(args: Array<String>) {
    val names = mutableListOf("John", "Tim", "Zack", "Daniel", "Adam")
    println(names)
    var ordered = insertionsort(names)
    println(ordered)
}			

aaaaaaaaaaaaa
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : We have closed registration for training

Please do email to chercher.tech@gmail.com for any queries

If you already registered then please do check your email for the Webex link for the training starting from 15th Nov 2018
 
Join My Facebook Group
Join Group