object InsertSort {
def main(args: Array[String]): Unit = {
val lst = List(2,1,3,4,5,10,6)
val newLst = insertSort(lst, Nil)
println(newLst._2)
}
case Nil => (from,to)
case head::tail => insertSort(tail,insertList(to, head))
}
case Nil => List(e)
case _ => lst.span { _ < e } match {
case (x,y) => (x :+ e) ++ y
}
}
}
def main(args: Array[String]): Unit = {
val lst = List(2,1,3,4,5,10,6)
val newLst = insertSort(lst, Nil)
println(newLst._2)
}
/*递归插入,依次将旧链表的首元素用插入排序法插入新链表
*当旧链表为空时,返回(nil,新链表)的元组。
*/
case Nil => (from,to)
case head::tail => insertSort(tail,insertList(to, head))
}
/**将一个元素用插入排序法插入链表,返回插入新元素后的链表*/
case Nil => List(e)
case _ => lst.span { _ < e } match {
case (x,y) => (x :+ e) ++ y
}
}
}