Skip to content

Latest commit

 

History

History
34 lines (22 loc) · 1.35 KB

2013-02-26-scala-review-problem.md

File metadata and controls

34 lines (22 loc) · 1.35 KB

Scala Practice Review Problem

QuickSort

To demonstrate the expressiveness and power of functional programming, we are going to ask you to implement quicksort Here's a quick definition of quicksort:

For a given list, if the list is not empty, choose one element as a pivot element. It may be easiest to pick the first element as the pivot. Partition the remainder of the elements into two lists, one that contains elements that are less than or equal to the pivote, and one that contains elements that are greater than the pivot. Recursively call quicksort on the left and right partitions; finally, concatenate the left, pivot, and right together.

The signature you should use is:

def qs[T <% Ordered[T]](list: List[T]): List[T]

Example:

scala> qs(List(1, 34, 65, 3, 3, -1))
res1: List[Int] = List(-1, 1, 3, 3, 34, 65)

scala> qs(List("a", "b", "a", "abc", "bcd","c"))
res2: List[String] = List(a, a, abc, b, bcd, c)

Hint