Selection Sort
Find the smallest value in the entire array and then place the smallest value first into its sorted position.
Pseudocode
- Loop through the array from index 0 to the second-last index:
- Set 
lowestto the current indexi - Loop through the remaining elements (from 
j=i+1to the last index):- Compare the 
element[lowest]with the current element:- If the current element is smaller, update 
lowestto the current indexj 
 - If the current element is smaller, update 
 
 - Compare the 
 - After the inner loop, if 
lowestis not the same asi:- Swap the element at 
iwith the element atlowest 
 - Swap the element at 
 
 - Set 
 - Return the sorted array
 
Time Complexity
- Best, Average and Worst Case is O(n2), where n is the size of the array.
 
Selection Sort
function selectionSort(arr) {
  for (let i = 0; i < arr.length; i++) {
    let lowest = i;
    for (let j = i + 1; j < arr.length; j++) {
      if (arr[lowest] > arr[j]) {
        lowest = j;
      }
    }
    if (i !== lowest) {
      let temp = arr[i];
      arr[i] = arr[lowest];
      arr[lowest] = temp;
    }
  }
  return arr;
}