Go语言学习-练习1.冒泡法

reation · · 3472 次点击
/** * 快速排序 */ object QuickSort { //由于不是尾递归,会造成栈溢出 def sort[T](list: List[T])(implicit ord: Ordering[T]): List[T] = { //模式匹配用法 list match { case Nil => Nil case head :: tail => { val (left, right) = tail.partition { ord.compare(_, head) < 0 } sort(left) ::: head :: sort(right) } } } def main(args: Array[String]): Unit = { val list = List.range(1, 10) println(sort(list)) println(sort(list)(Ordering.Int.reverse)) } }
#1
更多评论
还是Scala的语法简洁,奈何我大Go语言编译部署快啊 ``` /** * 快速排序 */ object QuickSort { //由于不是尾递归,会造成栈溢出 def sort[T](list: List[T])(implicit ord: Ordering[T]): List[T] = { //模式匹配用法 list match { case Nil => Nil case head :: tail => { val (left, right) = tail.partition { ord.compare(_, head) < 0 } sort(left) ::: head :: sort(right) } } } def main(args: Array[String]): Unit = { val list = List.range(1, 10) println(sort(list)) println(sort(list)(Ordering.Int.reverse)) } } ```
#2