归并排序


//提供的int排序的统一方法
val intSort = msort((x:Int,y:Int)=>x<y) _
//逆序
val reverseIntSort = msort((x:Int,y:Int)=>x>y) _


def msort[T](less:(T,T)=>Boolean) (xs:List[T]):List[T]={ def merge(xs:List[T],ys:List[T]):List[T]= (xs,ys) match { case (Nil,_)=>ys case (_,Nil)=>ys case (x::xsl,y::ysl)=> if(less(x,y)) x::merge(xsl,ys) else y::merge(xs,ysl) } val n=xs.length / 2 if(n==0) xs else{ val (ys,zs)=xs splitAt n merge(msort(less)(ys),msort(less)(zs)) } }

  

原文地址:https://www.cnblogs.com/huiandong/p/9574020.html