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));
}
}