列表2中插入排序

 //插入排序
 def isort(xs:List[Int]):List[Int]=
   if(xs.isEmpty)Nil
   else insert(xs.head,isort(xs.tail))
 def insert(x:Int,xs:List[Int]):List[Int]=
   if(xs.isEmpty || x<=xs.head) x::xs
   else xs.head :: insert(x,xs.tail)
  //模式匹配插入排序
  def isort1(xs:List[Int]):List[Int]=xs match{
     case List() =>List()
     case x::xsl=>insert(x,isort(xsl))
   }
 def insert2(x:Int,xs:List[Int]):List[Int]=xs match{
   case List() =>List(x)
   case y::ys=>if(x<=y) x:: xs
   else y:: insert(x,ys)
 }

  

posted @ 2018-09-02 13:30  uuhh  阅读(254)  评论(0编辑  收藏  举报