A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Bubble Sort in Kotlin page! Here, you'll find the source code for this program as well as a description of how the program works.
fun main(args: Array<String>)
{
var nums: IntArray
try
{
nums = args[0].split(", ").map{ it.toInt() }.toIntArray()
if (nums.size < 2) {
throw Exception()
}
}
catch(e: Exception)
{
println("Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\"")
return
}
var swapped:Boolean = false
for(i in 0 until nums.count()-1)
{
swapped = false
for(j in 0 until nums.count()-i-1)
{
if(nums[j]>nums[j+1])
{
//swap
nums[j] = nums[j+1].also {nums[j+1] = nums[j]}
swapped = true
}
}
if (swapped==false)
{
break
}
}
for(i in 0 until nums.count())
{
if (i==nums.count()-1)
{
println("${nums[i]}")
return
}
print("${nums[i]}, ")
}
}
Bubble Sort in Kotlin was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.