package algorithmimport scala.collection.mutable.ListBuffer  BubbleSort {  def main(args: Array[String]): Unit = {    val a = ListBuffer(1, 2, 3, 10, 100, 19999, -1998, 9, 234, 234, 9, 43)    val res: ListBuffer[Int] = BubbleSort[Int](_>_)(a)    println(res)  }  """    |时间复杂度o(N)~ o(N)^2.    |冒泡排序:稳定的排序,    | 每一趟排序相邻的两两比较,最后找出这一趟的最大或者最小    | 下一趟依旧如此,只是比较区间变小了  """.stripMargin  def BubbleSort[T](comparator:(T,T)=>Boolean)(list:ListBuffer[T]):ListBuffer[T]={    for(i<- 0 until(list.size -1)){      for(j<- 0 until(list.size-i -1)){        if(comparator(list(j),list(j+1))){          var temp = list(j)          list(j) = list(j+1)          list(j+1) =temp        }      }    }    list  }}
收藏 打印