Scala

Algorithm to sort elements in an Array using Scala

Algorithm/Program for sorting elements in an Array using Scala. The algorithm used is Bubble Sort. Bubble Sort is the simplest algorithm that works by repeatedly swapping the adjacent elements.

[code lang=”scala”]
object SortArray{
def main(args: Array[String]) {
val inputarray = Array(1,2,3,2,4,1,4)
println("Input")
println(inputarray.mkString(","))
for(i <- 0 until inputarray.length-1){
for(j<-0 until inputarray.length-i-1){ if(inputarray(j)>inputarray(j+1)){
var temp = inputarray(j)
inputarray(j)=inputarray(j+1)
inputarray(j+1)=temp
}
}
}
println("Sorted elements in Array")
println(inputarray.mkString(","))
}
}
[/code]

Output:
Input
1,2,3,2,4,1,4

Sorted elements in Array
1,1,2,2,3,4,4

Share This Post

An Ambivert, music lover, enthusiast, artist, designer, coder, gamer, content writer. He is Professional Software Developer with hands-on experience in Spark, Kafka, Scala, Python, Hadoop, Hive, Sqoop, Pig, php, html,css. Know more about him at www.24tutorials.com/sai

Lost Password

Register

Do NOT follow this link or you will be banned from the site!