bubble sort scala

/** blue sort*/
def Bluesort(array : Array[Int]): Array[Int] = {
  var flag = false
  for(i<- 0 until array.length -1)
    if(array(i+1) > array(i)){  //tang dan,giam dan >
      val tmp     = array(i)
       array(i)   = array(i+1)
       array(i+1) = tmp
       flag = true
    }
  // Repeat until we don't have anymore swaps
  if(flag)
    Bluesort(array)
  else
    array
}
def main(args: Array[String]): Unit = {
  val myList = Array(5,1,6,2,4,3)
  for ( x <- Bluesort(myList) ) {
    println( x )
  }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s