Skip to content

Commit

Permalink
Create Sorts.scala
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchuz authored Oct 23, 2018
1 parent 8063394 commit 3675bbc
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions scala/11_sorts/Sorts.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import scala.util.control.Breaks._
object Sorts {
def main(args: Array[String]): Unit ={
println(bubbleSort(Array(0, 1, 2, 3, 4, 5, 6, 7)).mkString(", "))
}

def bubbleSort(arr: Array[Int]): Array[Int] = {
val l = arr.length
breakable {
for(i <- (l-1) to (1, -1)){
var flag = false
for(j <- 0 until i){
if(arr(j) < arr(j+1)){
val tmp = arr(j)
arr(j) = arr(j+1)
arr(j+1) = tmp
flag = true
}
}
if(!flag){
break
}
}
}
arr
}
}

0 comments on commit 3675bbc

Please sign in to comment.