0
点赞
收藏
分享

微信扫一扫

scala数据结构和算法-05-插入排序实现


 

package data

import scala.collection.mutable.ListBuffer

object InsertSort {
def insertSort[T<%Ordered[T]](source:ListBuffer[T]):ListBuffer[T]={
for(i<-1 until source.length){
for(j<-(1 to i).reverse){
val current=source(j);
val prev=source(j-1);
if(current<prev){
source(j-1)=current;
source(j)=prev;
}
}
}
source
}
def main(args: Array[String]): Unit = {
val source=ListBuffer(3,5,4,9,1,8,7,6);
println(insertSort(source).mkString(","))
}
}

 

举报

相关推荐

0 条评论