Insertion Sort algorithm to sort data.
Insertion sort is a simple sorting algorithm that works similar to the way you sort playing cards in your hands. The array is virtually split into a sorted and an unsorted part. Values from the unsorted part are picked and placed at the correct position in the sorted part.
function insertionSort(arr) { let n = arr.length; for (let i = 1; i < n; i++) { let current = arr[i]; let j = i-1; while ((j > -1) && (current < arr[j])) { arr[j+1] = arr[j]; j--; } arr[j+1] = current; } return arr; }