ITeye博客

Scala实现快速排序

cakin24 · 2017-08-22 20:49 · Scala · 编程语言 · Scala · 快速排序 · Markdown

代码

  1. scala>:paste
  2. //Entering paste mode (ctrl-D to finish)
  3. def qSort(a:List[Int]):List[Int]=
  4. if(a.length<2) a
  5. else
  6. qSort(a.filter(_<a.head))++
  7. a.filter(_ == a.head)++
  8. qSort(a.filter(_>a.head))
  9. //Exiting paste mode, now interpreting.
  10. qSort:(a:List[Int])List[Int]
  11. scala> qSort(List(3,1,2))
  12. res47:List[Int]=List(1,2,3)
  13. scala> qSort(List(3,8,5,31,1,2))
  14. res48:List[Int]=List(1,2,3,5,8,31)

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。