License Public Domain
Lines 18
Keywords
algorithm (8) functional programming (1) JVM languages (3) quicksort (5) sort (5)
Implements this Algorithm
Permissions
Owner: Stou S.
Viewable by Everyone
Editable by All Siafoo Users
Hide
Free your code from a slow death on your hard drive Join Siafoo Now or Learn More

Functional Implementation of Quicksort Atom Feed 0

In Brief A Scala implementation of Quick Sort using functional programming techniques. Compare this clean and delicious implementation to the considerably longer imperative version
# '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