# What is Selection Sort - Random

Selection sort is a simple sorting algorithm that works by repeatedly finding the minimum element from an unsorted list and placing it at the beginning of the list. The algorithm maintains two sublists: the sorted sublist and the unsorted sublist.

The algorithm works by iterating through the unsorted sublist, finding the smallest element, and swapping it with the first element of the unsorted sublist. This places the smallest element in the correct position in the sorted sublist. Then, the algorithm repeats this process with the remaining unsorted sublist until the entire list is sorted.

Randomized selection sort is a variation of the selection sort algorithm that adds an element of randomness to the sorting process. Instead of always selecting the minimum or maximum element from the unsorted sublist, the algorithm randomly selects an element from the unsorted sublist and swaps it with the first element. This element then becomes the pivot, and the algorithm proceeds as usual by partitioning the list into two sublists, one containing elements smaller than the pivot and one containing elements greater than the pivot.

The randomized selection sort algorithm has the advantage of being more resistant to certain types of input data that can cause the traditional selection sort algorithm to perform poorly. For example, if the input list is already partially sorted or contains many duplicate elements, the traditional selection sort algorithm may waste time searching for the minimum or maximum element multiple times. The randomized selection sort algorithm, on the other hand, has a greater chance of selecting a pivot that is closer to the median of the unsorted sublist, which can lead to better overall performance.

The algorithm works by iterating through the unsorted sublist, finding the smallest element, and swapping it with the first element of the unsorted sublist. This places the smallest element in the correct position in the sorted sublist. Then, the algorithm repeats this process with the remaining unsorted sublist until the entire list is sorted.

Randomized selection sort is a variation of the selection sort algorithm that adds an element of randomness to the sorting process. Instead of always selecting the minimum or maximum element from the unsorted sublist, the algorithm randomly selects an element from the unsorted sublist and swaps it with the first element. This element then becomes the pivot, and the algorithm proceeds as usual by partitioning the list into two sublists, one containing elements smaller than the pivot and one containing elements greater than the pivot.

The randomized selection sort algorithm has the advantage of being more resistant to certain types of input data that can cause the traditional selection sort algorithm to perform poorly. For example, if the input list is already partially sorted or contains many duplicate elements, the traditional selection sort algorithm may waste time searching for the minimum or maximum element multiple times. The randomized selection sort algorithm, on the other hand, has a greater chance of selecting a pivot that is closer to the median of the unsorted sublist, which can lead to better overall performance.

## Who invented it?

The invention of randomized selection sort is usually attributed to Robert W. Floyd, a renowned computer scientist who made significant contributions to the field of algorithms and programming languages. Floyd introduced the algorithm in a 1971 paper titled "The Algorithm Selection Problem," where he proposed using randomization to improve the efficiency of certain algorithms, including selection sort.

Floyd's idea was to randomly select a pivot element from the unsorted sublist, rather than always selecting the first or last element as in the traditional selection sort algorithm. This randomized selection process had the effect of reducing the likelihood of selecting a particularly bad pivot that could cause the algorithm to perform poorly.

The randomized selection sort algorithm was later refined and popularized by other researchers, including Robert Sedgewick and Jon Bentley, who developed a variant of the algorithm known as QuickSelect that used a similar randomized partitioning approach. Today, randomized selection algorithms are widely used in various fields of computer science, including machine learning, data analysis, and cryptography.

Floyd's idea was to randomly select a pivot element from the unsorted sublist, rather than always selecting the first or last element as in the traditional selection sort algorithm. This randomized selection process had the effect of reducing the likelihood of selecting a particularly bad pivot that could cause the algorithm to perform poorly.

The randomized selection sort algorithm was later refined and popularized by other researchers, including Robert Sedgewick and Jon Bentley, who developed a variant of the algorithm known as QuickSelect that used a similar randomized partitioning approach. Today, randomized selection algorithms are widely used in various fields of computer science, including machine learning, data analysis, and cryptography.

## Pseudocode

```
function randomized_selection_sort(A):
n = length(A)
for i from 0 to n-1:
// randomly select an index j from i to n-1
j = random(i, n-1)
// swap elements A[i] and A[j]
swap(A[i], A[j])
// perform selection sort on the remaining unsorted sublist
for k from i+1 to n-1:
if A[k] < A[i]:
swap(A[i], A[k])
return A
```

- In this algorithm, A is the input array to be sorted, and n is the length of the array. The random(i, n-1) function selects a random integer between i and n-1, inclusive. The swap(a, b) function swaps the values of a and b.
- The outer loop of the algorithm selects a random pivot element from the unsorted sublist and swaps it with the first element. The inner loop then performs the selection sort algorithm on the remaining unsorted sublist, starting from the second element. The algorithm selects the smallest element from the unsorted sublist and swaps it with the first element of the unsorted sublist until the entire array is sorted.

## Sample Code

```
// C++ code snippet
//Change this header file include syntax to external directory.
#include "iostream"
#include "algorithm"
#include "random"
#include "ctime"
using namespace std;
void randomized_selection_sort(int arr[], int n) {
mt19937 rng(time(nullptr)); // initialize random number generator with current time
uniform_int_distribution
``` dist(0, n-1); // define uniform distribution over [0, n-1]
for (int i = 0; i < n; i++) {
int j = dist(rng); // randomly select an index j from i to n-1
swap(arr[i], arr[j]); // swap elements arr[i] and arr[j]
// perform selection sort on the remaining unsorted sublist
for (int k = i+1; k < n; k++) {
if (arr[k] < arr[i]) {
swap(arr[i], arr[k]);
}
}
}
}
int main() {
int arr[] = {4, 2, 6, 1, 7, 3, 5};
int n = sizeof(arr) / sizeof(arr[0]);
randomized_selection_sort(arr, n);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}

```
# Python code snippet
import random
def randomized_selection_sort(arr):
n = len(arr)
for i in range(n):
j = random.randint(i, n-1) # randomly select an index j from i to n-1
arr[i], arr[j] = arr[j], arr[i] # swap elements arr[i] and arr[j]
# perform selection sort on the remaining unsorted sublist
for k in range(i+1, n):
if arr[k] < arr[i]:
arr[i], arr[k] = arr[k], arr[i]
return arr
arr = [4, 2, 6, 1, 7, 3, 5]
print(randomized_selection_sort(arr))
```

```
import java.util.Arrays;
import java.util.Random;
public class RandomizedSelectionSort {
public static void randomizedSelectionSort(int[] arr) {
int n = arr.length;
Random rng = new Random(); // initialize random number generator
for (int i = 0; i < n; i++) {
int j = i + rng.nextInt(n-i); // randomly select an index j from i to n-1
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp; // swap elements arr[i] and arr[j]
// perform selection sort on the remaining unsorted sublist
for (int k = i+1; k < n; k++) {
if (arr[k] < arr[i]) {
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = {4, 2, 6, 1, 7, 3, 5};
randomizedSelectionSort(arr);
System.out.println(Arrays.toString(arr));
}
}
```

## Time and Space Complexity

The time complexity of random selection algorithms is O(n), where n is the number of elements in the input array. This is because algorithm use a partitioning step to split the array into two parts based on a pivot element, and the partitioning step takes O(n) time in the worst case.

The space complexity of random selection is O(1) in the worst case, since the algorithm does not create any auxiliary arrays and only uses a constant amount of additional memory for storing indices and the pivot element. However, the algorithm is randomized and has a non-zero probability of taking O(n^2) time in the worst case if the pivot is consistently chosen poorly. This is a rare occurrence, and the average case time complexity is still O(n).

The space complexity of random selection is O(1) in the worst case, since the algorithm does not create any auxiliary arrays and only uses a constant amount of additional memory for storing indices and the pivot element. However, the algorithm is randomized and has a non-zero probability of taking O(n^2) time in the worst case if the pivot is consistently chosen poorly. This is a rare occurrence, and the average case time complexity is still O(n).

## Advantages

- Random selection sort, also known as randomized selection, has several advantages and disadvantages.
- The main advantage of random selection sort is that it has a worst-case time complexity of O(n), which is faster than many other sorting and selection algorithms that have worst-case time complexity of O(nlogn). This is because the algorithm randomly chooses the pivot element, which reduces the probability of encountering the worst-case scenario where the pivot element is always chosen poorly.
- Random selection sort also has a relatively low space complexity, especially compared to some divide-and-conquer algorithms like merge sort and quicksort that require O(n) additional memory.

- The main advantage of random selection sort is that it has a worst-case time complexity of O(n), which is faster than many other sorting and selection algorithms that have worst-case time complexity of O(nlogn). This is because the algorithm randomly chooses the pivot element, which reduces the probability of encountering the worst-case scenario where the pivot element is always chosen poorly.

## Disadvantages

- The main disadvantage of random selection sort is that its worst-case time complexity is only probabilistic, and in rare cases, it can take O(n^2) time. This can happen if the pivot element is chosen poorly at each step, which can happen with a small probability.
- Another disadvantage is that the algorithm can be slower in practice than simpler algorithms like quicksort and heapsort, especially for small input sizes. This is because the overhead of the random selection step and the recursive calls can add up, and the algorithm requires more comparisons and memory accesses than simpler algorithms.
- Random selection sort is also not stable, which means that it does not preserve the relative order of equal elements in the input array.
- In summary, random selection sort is a good choice for selecting the kth smallest element in an unsorted array, especially for large input sizes. However, it may not be the best choice for sorting an entire array, especially for small input sizes, where simpler algorithms like quicksort and heapsort may be faster.