Partitioning Schemes
excerpt: A well known problem in computer science consists of partitioning a list. tags: partitioning
Partitioning a list consists of the following steps:
 Randomly select a pivot element from the list.
 Permute the input list where the first elements are smaller than or equal to the pivot and the last ones are greater than the pivot.
The diagram shows selecting the pivot and permuting the list. This partitioning scheme is used in the quicksort and quickselect algorithms.
Basic Partitioning Scheme
The list is scanned to build:
 A new list that contains the elements that are smaller than or equal to the pivot.
 Another list formed by the elements that are greater than the pivot.
 Concatenate the list together with the pivot element placed in between the two lists.
The inputs to the problem are a list ‘a’ of length n and some value x that plays the role of the pivot. Its size is the length of the list. The base case occurs when the list is empty, we return an empty list. For the recursive case we can decompose the problem by discarding the first element of the list, which reduces its size by a unit.
The recursive diagrams depends on whether the first element of the list is less than or equal to the pivot. If it is, recursive diagram is as shown below:
The first element of the list must be concatenated to the output of the subproblem. If the first element is greater than x, the diagram is as shown below:
In this case, we have to return the solution (list) to the subproblem. The code shows the linear recursive solution that solve both problems.


The get_smaller_than_or_equal_to and get_greater_than are auxiliary methods for partitioning a list.


In the recursive cases that perform the concatenation, the function call is not the last action of the method. The runtime is linear with respect to n.