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

来源:互联网 发布:y2y的网络意思 编辑:程序博客网 时间:2024/05/16 04:50
package dataimport scala.collection.mutable.ListBufferobject 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 0
原创粉丝点击