Selection Sort in Java
Selection sort is also a simple sorting algorithm that works by repeatedly finding the minimum element from the unsorted portion of the input array, and placing it in the sorted portion of the array. Thus, selection sort maintains two subarrays, one is sorted and another one is not sorted. In every iteration, the size of the sorted subarray increases, and the size of the unsorted subarray decreases.
Algorithm and Pseudo Code
selectionSort(arr, size)
for( int i -> 0 to (size — 1) times )
Assume that the first element encountered in each iteration, arr[i], as the current minimum element.
Store the index of the current minimum element, say minIndex
for( int j -> i + 1 to size times)
if arr[j] < arr[minIndex]
update the minIndex as j
end if
end for
swap the value present at the minIndex with first unsorted position, i.e, arr[i]
end for
end selectionSort