Sorting

Selection Sort

The algorithm finds the lowest element of the dataset and moves it to the front of the dataset and keeps doing this process over and over until the dataset is sorted. video shows how the algorithm works

Big(O)

| | speed | | -------- | ------- | | Best | O(n^2) | | Average | O(n^2) | | Worst | O(n^2) |

Implementation

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