UNPKG

algorithmpool

Version:
24 lines (23 loc) 619 B
import { Compare, defaultCompare } from '../../utils/util'; export const insertionSort = (array, compareFn = defaultCompare) => { const { length } = array; let temp; for (let i = 1; i < length; i++) { let j = i; temp = array[i]; // console.log('to be inserted ' + temp); while (j > 0 && compareFn(array[j - 1], temp) === Compare.BIGGER_THAN) { // console.log('shift ' + array[j - 1]); array[j] = array[j - 1]; j--; } // console.log('insert ' + temp); array[j] = temp; } return array; };