Update Sorts.scala

This commit is contained in:
yangchuz 2018-10-23 09:54:51 +08:00 committed by GitHub
parent 182dee6e92
commit b06c99632b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,7 @@
import scala.util.control.Breaks._ import scala.util.control.Breaks._
object Sorts { object Sorts {
def main(args: Array[String]): Unit ={ def main(args: Array[String]): Unit ={
println(bubbleSort(Array(0, 1, 2, 3, 4, 5, 6, 7)).mkString(", ")) println(bubbleSort(Array(0, 3, 7, 6, 4, 5, 1, 2)).mkString(", "))
} }
def bubbleSort(arr: Array[Int]): Array[Int] = { def bubbleSort(arr: Array[Int]): Array[Int] = {
@ -10,7 +10,7 @@ object Sorts {
for(i <- (l-1) to (1, -1)){ for(i <- (l-1) to (1, -1)){
var flag = false var flag = false
for(j <- 0 until i){ for(j <- 0 until i){
if(arr(j) < arr(j+1)){ if(arr(j) > arr(j+1)){
val tmp = arr(j) val tmp = arr(j)
arr(j) = arr(j+1) arr(j) = arr(j+1)
arr(j+1) = tmp arr(j+1) = tmp