Sorting

Insertion Sort

Is a sorting algorithm it go through the dataset and place the current element in its right sort position The video shows how the algorithm works

Big(O)

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

Implementation

function inserstionSort(data){
  for(let i=1;i<data.length;i++){
    // j is the current index
    let j = i-1;
    let temp = data[i];
    while(j>=0 && data[j]>temp){
      data[j+1] = data[j];
      j--;
    }
    data[j+1] = temp;
  }
  return data
}