const selectionSort = (arr: number[]) => {
        const a: number[] = [...arr];
        for (let i: number = 0; i < a.length; i++) {
          const min: number = a
            .slice(i + 1)
            .reduce((acc, val, j) => (val < a[acc] ? j + i + 1 : acc), i);
          if (min !== i) [a[i], a[min]] = [a[min], a[i]];
        }
        return a;
      };
      
      Typescript language logo

      Selection sort

      Codiga's TypeScript Recipes

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

      0 Comments

        Add Comment

        Log in to add a comment

        Codiga - All rights reserved 2022.