Bubble Sort Algorithm implementation in Kotlin

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

Solution Worked 0 UpvotesUpvote

        

Solution Didn't Worked 0 DownvotesDownvote



Comments



Search