const insertionSort = (arr: number[]) =>
        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;
        }, []);
      
      Typescript language logo

      Insertion Sort

      Codiga's TypeScript Recipes

      Sorts an array of numbers, using the insertion sort algorithm.

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.