Selection Sort is a simple sorting algorithm that repeatedly selects the next-smallest element from the unsorted portion of the array and places it at the end of the sorted portion. Here is an example implementation of selection sort in JavaScript:

function selectionSort(arr) {
  for (let i = 0; i < arr.length; i++) {
    let minIndex = i;
    for (let j = i+1; j < arr.length; j++) {
      if (arr[j] < arr[minIndex]) {
        minIndex = j;
      }
    }
    let temp = arr[i];
    arr[i] = arr[minIndex];
    arr[minIndex] = temp;
  }
  return arr;
}

In this example, the outer loop iterates through the entire array, while the inner loop finds the next-smallest element in the unsorted portion of the array. Once the next-smallest element is found, it is swapped with the current element in the outer loop. The process is repeated until the entire array is sorted.

Also Read:

Categorized in: