# QuickSelect Selection Algorithm (kth smallest item/kth order statistic)

## Algorithm goal

The QuickSelect Selection algorithm finds the kth smallest item in a collection. It it related to QuickSort.

## Explanation

The implementation is similar in principle to QuickSort, however we get a simplification because we do not need to care about certain values once we know where our minimum/maximum are after partitioning.

In this algorithm, we pick a pivot (the most straightforward is the first element), and then partition the remaining list by elements lower and higher than the pivot. (this is © from www.scala-algorithms.com)

After partitioning by the pivot, if the number of elements we need is higher than the number provided in the smaller partition, then we immediately look at the higher partition, eliminating a large number of elements.

## Scala Concepts & Hints

Option Type

The 'Option' type is used to describe a computation that either has a result or does not. In Scala, you can 'chain' Option processing, combine with lists and other data structures. For example, you can also turn a pattern-match into a function that return an Option, and vice-versa!

assert(Option(1).flatMap(x => Option(x + 2)) == Option(3))

assert(Option(1).flatMap(x => None) == None)


Ordering

In Scala, the 'Ordering' type is a 'type class' that contains methods to determine an ordering of specific types.

assert(List(3, 2, 1).sorted == List(1, 2, 3))

assert(List(3, 2, 1).sorted(Ordering[Int].reverse) == List(3, 2, 1))

assert(Ordering[Int].lt(1, 2))

assert(!Ordering[Int].lt(2, 1))


Pattern Matching

Pattern matching in Scala lets you quickly identify what you are looking for in a data, and also extract it.

assert("Hello World".collect {
case character if Character.isUpperCase(character) => character.toLower
} == "hw")


Stack Safety

Stack safety is present where a function cannot crash due to overflowing the limit of number of recursive calls.

This function will work for n = 5, but will not work for n = 2000 (crash with java.lang.StackOverflowError) - however there is a way to fix it :-)

In Scala Algorithms, we try to write the algorithms in a stack-safe way, where possible, so that when you use the algorithms, they will not crash on large inputs. However, stack-safe implementations are often more complex, and in some cases, overly complex, for the task at hand.

def sum(from: Int, until: Int): Int =
if (from == until) until else from + sum(from + 1, until)

def thisWillSucceed: Int = sum(1, 5)

def thisWillFail: Int = sum(1, 300)


Tail Recursion

In Scala, tail recursion enables you to rewrite a mutable structure such as a while-loop, into an immutable algorithm.

def fibonacci(n: Int): Int = {
@scala.annotation.tailrec
def go(i: Int, previous: Int, beforePrevious: Int): Int =
if (i >= n) previous else go(i + 1, previous + beforePrevious, previous)

go(i = 1, previous = 1, beforePrevious = 0)
}

assert(fibonacci(8) == 21)


## Algorithm in Scala

16 lines of Scala (version 2.13), showing how concise Scala can be!

## Test cases in Scala

assert(quickSelect(List(1, 2, 3, 4, 5), 3) == Some(3))
assert(quickSelect(List(5, 4, 3, 2, 1), 3) == Some(3))
assert(quickSelect(List(5, 4, 3, 2, 1), 1) == Some(1))
assert(quickSelect(List.empty[Int], 1) == None)
assert(quickSelect(List(1), 1) == Some(1))
assert(quickSelect(List(1), 2) == None)

def quickSelect[A](list: List[A], k: Int)(implicit o: Ordering[A]): Option[A] =
???