scala实现快速排序


scala> def qSort(a: List[Int]): List[Int] = {
     | if (a.length < 2 ) a
     | else qSort( a.filter(a.head > _ )) ++
     | a.filter(a.head == _ ) ++
     | qSort(a.filter(a.head < _ ))
     | }
qSort: (a: List[Int])List[Int]

scala>

scala> qSort(List(8,5,6,4,3,9,11,1))
res25: List[Int] = List(1, 3, 4, 5, 6, 8, 9, 11)

这是函数式编程的一个优点,代码简洁明了。

posted @ 2016-04-05 17:45  西塘.娟娟  阅读(490)  评论(0编辑  收藏  举报