Selection sort

    0

    0

    Giovanny Gongora

    Codiga's TypeScript Recipes

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

    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;
    };
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Explore
    • Cookbooks
    • Playground
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.