Selection Sort Algorithm
Selection Sort algorithm is a simple comparison-based sorting technique that works by dividing the input list into two parts: sorted and unsorted. Initially, the sorted part is empty, and the unsorted part contains all the elements. The algorithm repeatedly selects the smallest (or largest, depending on the ordering) element from the unsorted part and moves it to the end of the sorted part. This process continues until the unsorted part becomes empty, and the sorted part contains all the elements in the desired order.
In each iteration of the selection sort algorithm, the minimum (or maximum) element is identified from the remaining unsorted elements, and its position is swapped with the first unsorted element. This results in the minimum (or maximum) element moving to its correct position in the sorted part. The algorithm's time complexity is O(n^2) as it requires two nested loops in its implementation, making it inefficient for large datasets. However, it has the advantage of performing fewer swaps compared to other sorting algorithms like bubble sort, which might be beneficial in situations where swapping elements is a costly operation.
package Sorts;
/**
* @author Varun Upadhyay (https://github.com/varunu28)
* @author Podshivalov Nikita (https://github.com/nikitap492)
* @see SortAlgorithm
*/
public class SelectionSort implements SortAlgorithm {
/**
* This method swaps the two elements in the array
* @param arr, i, j The array for the swap and
the indexes of the to-swap elements
*/
public void swap(T[] arr, int i, int j) {
T temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/**
* This method implements the Generic Selection Sort
*
* @param arr The array to be sorted
* Sorts the array in increasing order
**/
@Override
public <T extends Comparable<T>> T[] sort(T[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
// Initial index of min
int min = i;
for (int j = i + 1; j < n; j++) {
if (arr[min].compareTo(arr[j]) < 0) {
min = j;
}
}
// Swapping if index of min is changed
if (min != i) {
swap(arr, i, min);
}
}
return arr;
}
// Driver Program
public static void main(String[] args) {
Integer[] arr = {4, 23, 6, 78, 1, 54, 231, 9, 12};
SelectionSort selectionSort = new SelectionSort();
Integer[] sorted = selectionSort.sort(arr);
// Output => 1 4 6 9 12 23 54 78 231
SortUtils.print(sorted);
// String Input
String[] strings = {"c", "a", "e", "b", "d"};
String[] sortedStrings = selectionSort.sort(strings);
//Output => a b c d e
SortUtils.print(sortedStrings);
}
}