Bubble Sort Algorithm implementation in Kotlin

[4317 views]




In this article, we will see a simple implementation of Bubble Sort algorithm in Kotlin.

Simple Bubble Sort Algorithm implementation in Kotlin language
fun ArrayList<Int>.bubbleSort() : ArrayList<Int>{ var swap = true while(swap){ swap = false for(i in 0 until this.indices.last){ if(this[i] > this[i+1]){ val temp = this[i] this[i] = this[i+1] this[i + 1] = temp swap = true } } } return this } fun main(args: Array<String>) { val list = arrayListOf(5,4,2,8,1) .bubbleSort() list.forEach{ println(it) } }
                 






Comments










Search Anything:

Sponsored Deals ends in





Technical Quizzes Specially For You:

Search Tags