### Scala language implementation of Bubble Sort

Introduction
Bubble sort is the simplest sorting algorithm. It has the following steps

a) Iterates through the list
c) Swap the adjacent elements if they are in not in proper sorting order
d) The list is traversed repeatedly unless the list is sorted

Worst Complexity: O(n*n)
Best Complexity   : O(n)
Alternate name     : Sinking Sort

Implementation

``````package main.scala

object BubbleSort {

val array  = Array(0,5,2,6,3,1)

def main (args : Array[String] ) {
print("Unsorted List ")
println(array.toList)
print("Ascending sorted List ")
bubbleSortAscending;
println(array.toList)

bubbleSortDescending
print("Descending sorted List ")
println(array.toList)
}

def bubbleSortAscending {
//in scala you can combine inner and outer loops, until does not include the last number
for( i <- 0 until array.length; j <- 1 until array.length - i  ){
// for asceding greater than sign is used
if( array.apply( j - 1)  > array.apply( j ) ){
var temp = array.apply( j - 1)
array.update(j - 1, array.apply(j) )
array.update(j, temp)
}
}
}

def bubbleSortDescending {
for( i <- 0 until array.length; j <- 1 until array.length - i  ){
// for asceding less than sign is used
if( array.apply( j - 1)  < array.apply( j ) ){
var temp = array.apply( j - 1)
array.update(j - 1, array.apply(j) )
array.update(j, temp)
}
}
}
}
``````

Input   : 0,5,2,6,3,1
Output: 0 1 2 3 5 6 (Ascending)
Output: 6 5 3 2 1 0 (Descending)

References
https://en.wikipedia.org/wiki/Sorting_algorithm#Bubble_sort