# Floyd-Rivest Algorithm

Last Updated : 26 Dec, 2022

The Floyd-Rivest algorithm is a selection algorithm used to find the kth smallest element in an array of distinct elements. It is similar to the QuickSelect algorithm but has a better running time in practice.
Like QuickSelect, the algorithm works on the idea of partitioning. After partitioning an array, the partition element ends up in the corrected sorted position. If the array has all distinct elements, retrieving the (k+1)th smallest element is the same as retrieving the (k+1)th element after sorting. Because a full sort is expensive(takes O(N log N) to compute), the Floyd-Rivest algorithm leverages partitioning to accomplish the same in O(N) time.

Algorithm:

1. If the size of the array S being considered is small enough, then the QuickSelect algorithm is applied directly to get the K-th smallest element. This size is an arbitrary constant of the algorithm, which the authors choose as 600.
2. Otherwise, 2 pivots are chosen- newLeftIndex and newRightIndex using random sampling such that they have the highest probability of containing the K-th largest element. Then, the function is called recursively with left and right boundaries of the array now set to newLeftIndex and newRightIndex.
3. Like QuickSelect, after partitioning the subarray, the left and right boundaries need to be set such that they contain the K-largest element.
After partitioning the array around K, the Kth element is in its correct sorted position. So, the left and right boundaries are set in such a way that the subarray being considered contains array[k]

Below is the implementation of the above approach.

## C++

 `// C++ implementation of the above approach.` `#include ` `#include ` `using` `namespace` `std;`   `// Function to return the` `// sign of a number` `int` `sign(``double` `x)` `{` `    ``if` `(x < 0)` `        ``return` `-1;` `    ``if` `(x > 0)` `        ``return` `1;` `    ``return` `0;` `}`   `// Function to swap` `// two numbers in an array.` `void` `swap(``int` `arr[], ``int` `i, ``int` `j)` `{` `    ``int` `temp = arr[i];` `    ``arr[i] = arr[j];` `    ``arr[j] = temp;` `}`   `int` `select(``int` `arr[], ``int` `left,` `           ``int` `right, ``int` `k)` `{` `    ``while` `(right > left) {` `        ``if` `(right - left > 600) {` `            ``// Choosing a small subarray` `            ``// S based on sampling.` `            ``// 600, 0.5 and 0.5` `            ``// are arbitrary constants` `            ``int` `n = right - left + 1;` `            ``int` `i = k - left + 1;` `            ``double` `z = ``log``(n);` `            ``double` `s = 0.5 * ``exp``(2 * z / 3);` `            ``double` `sd = 0.5 * ``sqrt``(z * s` `                                   ``* (n - s) / n)` `                        ``* sign(i - n / 2);`   `            ``int` `newLeft = max(left,` `                              ``(``int``)(k - i * s / n + sd));`   `            ``int` `newRight = min(right,` `                               ``(``int``)(k + (n - i) * s / n` `                                     ``+ sd));`   `            ``select(arr, newLeft, newRight, k);` `        ``}`   `        ``// Partition the subarray S[left..right]` `        ``// with arr[k] as pivot` `        ``int` `t = arr[k];` `        ``int` `i = left;` `        ``int` `j = right;` `        ``swap(arr, left, k);` `        ``if` `(arr[right] > t) {` `            ``swap(arr, left, right);` `        ``}`   `        ``while` `(i < j) {` `            ``swap(arr, i, j);` `            ``i++;` `            ``j--;`   `            ``while` `(arr[i] < t)` `                ``i++;` `            ``while` `(arr[j] > t)` `                ``j--;` `        ``}`   `        ``if` `(arr[left] == t)` `            ``swap(arr, left, j);` `        ``else` `{` `            ``j++;` `            ``swap(arr, right, j);` `        ``}`   `        ``// Adjust the left and right pointers` `        ``// to select the subarray having k` `        ``if` `(j <= k)` `            ``left = j + 1;` `        ``if` `(k <= j)` `            ``right = j - 1;` `    ``}` `    ``return` `arr[k];` `}`   `// Driver code` `int` `main()` `{` `    ``int` `arr[] = { 7, 3, 4, 0, 1, 6 };` `    ``int` `n = ``sizeof``(arr) / ``sizeof``(``int``);`   `    ``// k-th smallest element.` `    ``// In this we get the 2nd smallest element` `    ``int` `k = 2;` `    ``int` `res = select(arr, 0, n - 1, k - 1);` `    ``cout << ``"The "` `<< k << ``"-th smallest element is "` `         ``<< res << endl;` `    ``return` `0;` `}`

## Java

 `// Java implementation of the above approach.` `class` `GFG {`   `    ``// Function to return` `    ``// the sign of the number` `    ``int` `sign(``double` `x)` `    ``{` `        ``if` `(x < ``0``)` `            ``return` `-``1``;` `        ``if` `(x > ``0``)` `            ``return` `1``;` `        ``return` `0``;` `    ``}`   `    ``// Function to swap two numbers in an array` `    ``void` `swap(``int` `arr[], ``int` `i, ``int` `j)` `    ``{` `        ``int` `temp = arr[i];` `        ``arr[i] = arr[j];` `        ``arr[j] = temp;` `    ``}`   `    ``// Function to return kth smallest number` `    ``int` `select(``int` `arr[], ``int` `left,` `               ``int` `right, ``int` `k)` `    ``{` `        ``while` `(right > left) {` `            ``if` `(right - left > ``600``) {` `                ``// Choosing a small subarray` `                ``// S based on sampling.` `                ``// 600, 0.5 and 0.5 are` `                ``// arbitrary constants` `                ``int` `n = right - left + ``1``;` `                ``int` `i = k - left + ``1``;` `                ``double` `z = Math.log(n);` `                ``double` `s = ``0.5` `* Math.exp(``2` `* z / ``3``);`   `                ``double` `sd = ``0.5` `* Math.sqrt(z * s * (n - s) / n)` `                            ``* sign(i - n / ``2``);`   `                ``int` `newLeft = Math.max(left,` `                                       ``(``int``)(k - i * s / n` `                                             ``+ sd));`   `                ``int` `newRight = Math.min(right,` `                                        ``(``int``)(k + (n - i) * s / n` `                                              ``+ sd));`   `                ``select(arr, newLeft, newRight, k);` `            ``}`   `            ``// Partition the subarray S[left..right]` `            ``// with arr[k] as pivot` `            ``int` `t = arr[k];` `            ``int` `i = left;` `            ``int` `j = right;` `            ``swap(arr, left, k);` `            ``if` `(arr[right] > t) {` `                ``swap(arr, left, right);` `            ``}`   `            ``while` `(i < j) {` `                ``swap(arr, i, j);` `                ``i++;` `                ``j--;`   `                ``while` `(arr[i] < t)` `                    ``i++;` `                ``while` `(arr[j] > t)` `                    ``j--;` `            ``}`   `            ``if` `(arr[left] == t)` `                ``swap(arr, left, j);` `            ``else` `{` `                ``j++;` `                ``swap(arr, right, j);` `            ``}`   `            ``// Adjust the left and right` `            ``// pointers to select the subarray having k` `            ``if` `(j <= k)` `                ``left = j + ``1``;` `            ``if` `(k <= j)` `                ``right = j - ``1``;` `        ``}` `        ``return` `arr[k];` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int``[] arr = ``new` `int``[] { ``7``, ``3``, ``4``, ``0``, ``1``, ``6` `};`   `        ``// k-th smallest element.` `        ``// In this we get the 2nd smallest element` `        ``int` `k = ``2``;` `        ``FloydRivest f = ``new` `FloydRivest();` `        ``int` `res = f.select(arr, ``0``, arr.length - ``1``, k - ``1``);` `        ``System.out.println(``"The "` `+ k` `                           ``+ ``"-th smallest element is "` `+ res);` `    ``}` `}`

## Python3

 `# Python implementation of the above approach.` `import` `math` `import` `random`   `# Function to return the ` `# sign of the number` `def` `sign(x):` `    ``if` `x>``0``:` `        ``return` `1` `    ``elif` `x<``0``:` `        ``return` `-``1` `    ``return` `0`   `# Function to swap two ` `# numbers in an array` `def` `swap(arr, i, j):` `    ``temp ``=` `arr[i]` `    ``arr[i] ``=` `arr[j]` `    ``arr[j] ``=` `temp`   `# Function to return kth smallest number` `def` `select(arr: ``list``, left: ``int``, ` `right: ``int``, k: ``int``):` `    ``while` `right>left:`   `        ``# Choosing a small subarray` `        ``# S based on sampling.` `        ``# 600, 0.5 and 0.5 are` `        ``# arbitrary constants` `        ``if` `right``-``left > ``600``:` `            ``n ``=` `right ``-` `left ``+` `1` `            ``i ``=` `k ``-` `left ``+` `1` `            ``z ``=` `math.log(n)` `            ``s ``=` `0.5` `*` `math.exp(``2` `*` `z ``/` `3``)` `            ``sd ``=` `0.5` `*` `math.sqrt(z ``*` `s ``*` `(n``-``s)``/``n) ``*` `sign(i``-``n ``/` `2``)` `            ``newLeft ``=` `int``(``max``(left, k``-``i ``*` `s ``/` `n ``+` `sd))` `            ``newRight ``=` `int``(``min``(right, k ``+` `(n ``-` `i) ``*` `s ``/` `n ``+` `sd))` `            ``select(arr, newLeft, newRight, k)` `        ``t ``=` `arr[k]` `        ``i ``=` `left` `        ``j ``=` `right` `        ``swap(arr, left, k)` `        ``if` `arr[right] > t:` `            ``swap(arr, left, right)` `        ``while` `it:` `                ``j ``=` `j``-``1`   `        ``if` `arr[left] ``=``=` `t:` `            ``swap(arr, left, j)` `        ``else``:` `            ``j ``=` `j ``+` `1` `            ``swap(arr, right, j)`   `        ``# Updating the left and right indices ` `        ``# depending on position of k-th element ` `        ``if` `j<``=` `k:` `            ``left ``=` `j ``+` `1` `        ``if` `k<``=` `j:` `            ``right ``=` `j``-``1` `    ``return` `arr[k]`     `arr ``=` `[``7``, ``3``, ``4``, ``0``, ``1``, ``6``]` `# k-th smallest element. ` `# In this the 2nd smallest element is returned.` `k ``=` `2` `res ``=` `select(arr, ``0``, ``len``(arr)``-``1``, k``-``1``)` `print``(``'The {}-th smallest element is {}'``.``format``(k, res)) `

## C#

 `// C# implementation of the above approach. ` `using` `System;`   `class` `GFG ` `{ `   `    ``// Function to return ` `    ``// the sign of the number ` `    ``static` `int` `sign(``double` `x) ` `    ``{ ` `        ``if` `(x < 0) ` `            ``return` `-1; ` `        ``if` `(x > 0) ` `            ``return` `1; ` `        ``return` `0; ` `    ``} `   `    ``// Function to swap two numbers in an array ` `    ``static` `void` `swap(``int` `[]arr, ``int` `i, ``int` `j) ` `    ``{ ` `        ``int` `temp = arr[i]; ` `        ``arr[i] = arr[j]; ` `        ``arr[j] = temp; ` `    ``} `   `    ``// Function to return kth smallest number ` `    ``static` `int` `select``(``int` `[]arr, ``int` `left, ` `            ``int` `right, ``int` `k) ` `    ``{ ` `        ``int` `i;` `        ``while` `(right > left) ` `        ``{ ` `            ``if` `(right - left > 600)` `            ``{ ` `                ``// Choosing a small subarray ` `                ``// S based on sampling. ` `                ``// 600, 0.5 and 0.5 are ` `                ``// arbitrary constants ` `                ``int` `n = right - left + 1; ` `                ``i = k - left + 1; ` `                ``double` `z = Math.Log(n); ` `                ``double` `s = 0.5 * Math.Exp(2 * z / 3); `   `                ``double` `sd = 0.5 * Math.Sqrt(z * s * (n - s) / n) ` `                            ``* sign(i - n / 2); `   `                ``int` `newLeft = Math.Max(left, ` `                                    ``(``int``)(k - i * s / n ` `                                            ``+ sd)); `   `                ``int` `newRight = Math.Min(right, ` `                                        ``(``int``)(k + (n - i) * s / n ` `                                            ``+ sd)); `   `                ``select``(arr, newLeft, newRight, k); ` `            ``} `   `            ``// Partition the subarray S[left..right] ` `            ``// with arr[k] as pivot ` `            ``int` `t = arr[k]; ` `            ``i = left; ` `            ``int` `j = right; ` `            ``swap(arr, left, k); ` `            ``if` `(arr[right] > t)` `            ``{ ` `                ``swap(arr, left, right); ` `            ``} `   `            ``while` `(i < j)` `            ``{ ` `                ``swap(arr, i, j); ` `                ``i++; ` `                ``j--; `   `                ``while` `(arr[i] < t) ` `                    ``i++; ` `                ``while` `(arr[j] > t) ` `                    ``j--; ` `            ``} `   `            ``if` `(arr[left] == t) ` `                ``swap(arr, left, j); ` `            ``else` `            ``{ ` `                ``j++; ` `                ``swap(arr, right, j); ` `            ``} `   `            ``// Adjust the left and right ` `            ``// pointers to select the subarray having k ` `            ``if` `(j <= k) ` `                ``left = j + 1; ` `            ``if` `(k <= j) ` `                ``right = j - 1; ` `        ``} ` `        ``return` `arr[k]; ` `    ``} `   `    ``// Driver code ` `    ``public` `static` `void` `Main() ` `    ``{ ` `        ``int``[] arr = { 7, 3, 4, 0, 1, 6 }; `   `        ``// k-th smallest element. ` `        ``// In this we get the 2nd smallest element ` `        ``int` `k = 2; ` `        `  `        ``int` `res = ``select``(arr, 0, arr.Length - 1, k - 1); ` `        ``Console.WriteLine(``"The "` `+ k + ``"-th smallest element is "` `+ res); ` `    ``} ` `} `   `// This code is contributed by AnkitRai01`

## Javascript

 ``

Output:

`The 2-th smallest element is 1`

Time complexity: O(N)