Hands-On Selection Sort in JavaScript

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the selection sort algorithm and learn how to use it to sort an array of numbers in JavaScript. This lab aims to provide a hands-on experience with selection sort and help you understand how it works under the hood. By the end of this lab, you will have a solid understanding of selection sort and be able to apply it to solve sorting problems in your future projects.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") javascript/BasicConceptsGroup -.-> javascript/loops("`Loops`") javascript/BasicConceptsGroup -.-> javascript/array_methods("`Array Methods`") javascript/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28609{{"`Hands-On Selection Sort in JavaScript`"}} javascript/data_types -.-> lab-28609{{"`Hands-On Selection Sort in JavaScript`"}} javascript/arith_ops -.-> lab-28609{{"`Hands-On Selection Sort in JavaScript`"}} javascript/comp_ops -.-> lab-28609{{"`Hands-On Selection Sort in JavaScript`"}} javascript/cond_stmts -.-> lab-28609{{"`Hands-On Selection Sort in JavaScript`"}} javascript/loops -.-> lab-28609{{"`Hands-On Selection Sort in JavaScript`"}} javascript/array_methods -.-> lab-28609{{"`Hands-On Selection Sort in JavaScript`"}} javascript/higher_funcs -.-> lab-28609{{"`Hands-On Selection Sort in JavaScript`"}} javascript/spread_rest -.-> lab-28609{{"`Hands-On Selection Sort in JavaScript`"}} end

Selection Sort Algorithm

To start coding, open the Terminal/SSH and type node.

The following function sorts an array of numbers using the selection sort algorithm:

const selectionSort = (arr) => {
  const a = [...arr];
  for (let i = 0; i < a.length; i++) {
    const min = 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;
};

To use the function, pass an array of numbers to selectionSort(), like this:

selectionSort([5, 1, 4, 2, 3]); // [1, 2, 3, 4, 5]

The function works by cloning the original array using the spread operator (...). It then iterates over the array using a for loop. Using Array.prototype.slice() and Array.prototype.reduce(), it finds the index of the minimum element in the subarray to the right of the current index. If necessary, it performs a swap.

Summary

Congratulations! You have completed the Selection Sort lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like