Bubble Sort Algorithm implementation in Kotlin


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) } }

Struggling to Understand Algorithm and Flowchart? Try our Notes

Want to Test Your Knowledge on Algorithm and Flowchart?


Recommended Deals End in

Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Search Tags