
Take Control – Choose how to license your stuff and who can edit it, track every change
Join Siafoo Now
or
Learn More
Functional Implementation of Quicksort
0
Updated over 9 years ago (21 Jun 2009 at 02:05 PM)
recent activity
In Brief | A Scala implementation of Quick Sort using functional programming techniques. Compare this clean and delicious implementation to the considerably longer imperative version |
Language | Scala |
# 's
1/** Quick sort, functional style */
2object FunctionalQuickSort {
3
4 def sort(a: List[Int]): List[Int] = {
5 if (a.length < 2)
6 a
7 else {
8 val pivot = a(a.length / 2)
9 sort(a.filter(_ < pivot)) :::
10 a.filter(_ == pivot) :::
11 sort(a.filter(_ > pivot))
12 }
13 }
14
15 def main(args: Array[String]) {
16 val xs = List(6, 2, 8, 5, 1)
17 println(xs)
18 println(sort(xs))
19 }
20}
A Scala implementation of Quick Sort using functional programming techniques. Compare this clean and delicious implementation to the considerably longer imperative version
Add a Comment