insertion sort | JavaScript

December 24th, 2021 07:59:14 PM

					
const insertionSort = arr => arr.reduce((acc, x) => { if (!acc.length) return [x]; acc.some((y, j) => { if (x <= y) { acc.splice(j, 0, x); return true; } if (x > y && j === acc.length - 1) { acc.splice(j + 1, 0, x); return true; } return false; }); return acc; }, []); insertionSort([6, 3, 4, 1]); // [1, 3, 4, 6]

Featured Posts