Scala实现快速排序

阅读更多

代码

  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)

你可能感兴趣的:(Scala,快速排序)