DEV Community

Cover image for Selection Sort in JavaScript — A Clean, Simple Guide
Alok Kumar
Alok Kumar

Posted on • Edited on

Selection Sort in JavaScript — A Clean, Simple Guide

Selection Sort is one of the simplest sorting algorithms. It may not be the fastest, but it's extremely useful for learning how sorting logic works under the hood. In this blog, we'll walk through Selection Sort step-by-step, implement it in JavaScript, sort in descending order, and finally understand how the ES6 destructuring swap works.


Why learn Selection Sort?
  • It helps you understand algorithmic thinking.
  • It’s commonly asked in interviews.
  • It teaches core concepts like finding minimum/maximum, nested loops, and swapping.

What is Selection Sort?

Selection Sort divides the array into two parts:

  1. Sorted part (starts empty)
  2. Unsorted part (the entire array initially)

At each step:

  • Find the minimum element in the unsorted part.
  • Swap it with the first element of the unsorted section.
  • Expand the sorted section by one.

Think of it like selecting the smallest card from a deck and placing it at the beginning, again and again.


Step-by-Step Example

Given this array:

[11, 43, 2, 12, 6, 3, 7]

Iteration 1:

Minimum is 2 → swap with 11

[2, 43, 11, 12, 6, 3, 7]

Iteration 2:

Next minimum is 3 → swap with 43

[2, 3, 11, 12, 6, 43, 7]

Iteration 3:

Next minimum is 6 → swap with 11

[2, 3, 6, 12, 11, 43, 7]

…and so on until the array becomes fully sorted.


JavaScript Implementation (Ascending Order)

const selection = (arr) => {
  for (let i = 0; i < arr.length; i++) {
    let minIndex = i;

    for (let j = i + 1; j < arr.length; j++) {
      if (arr[j] < arr[minIndex]) {
        minIndex = j;
      }
    }

    // ES6 destructuring swap
    [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
  }

  return arr;
};

console.log(selection([11,43,2,12,6,3,7]));
Enter fullscreen mode Exit fullscreen mode

Sorting in Descending Order

To make Selection Sort descend instead of ascend:

  • Find the maximum instead of minimum.
  • Change < to > in the comparison.
const selectionDesc = (arr) => {
  for (let i = 0; i < arr.length; i++) {
    let maxIndex = i;

    for (let j = i + 1; j < arr.length; j++) {
      if (arr[j] > arr[maxIndex]) {
        maxIndex = j;
      }
    }

    [arr[i], arr[maxIndex]] = [arr[maxIndex], arr[i]];
  }

  return arr;
};

console.log(selectionDesc([11,43,2,12,6,3,7]));
Enter fullscreen mode Exit fullscreen mode

Understanding the ES6 Destructuring Swap

If you've seen this line:

[arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
Enter fullscreen mode Exit fullscreen mode

You might wonder: How does this magically swap values without a temporary variable?

This is a concise way to swap two elements without an explicit temporary variable. Here’s why it’s safe:

  1. Right-hand side is evaluated first. If arr[i] is 11 and arr[minIndex] is 2, the RHS becomes the temporary array [2, 11].

  2. The engine then assigns values left-to-right:

  • arr[i] = 2
  • arr[minIndex] = 11

Even though arr[i] is overwritten during the operation, the original values were already captured in the RHS, so nothing is lost.

Equivalent traditional swap:

let tmp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = tmp;
Enter fullscreen mode Exit fullscreen mode

Why use destructuring?

  • Shorter and more readable
  • Avoids naming a temp variable
  • Modern idiom commonly used in JS codebases and interviews


Summary

Selection Sort is simple but not very efficient compared to modern sorting algorithms. Here's a quick breakdown of its performance:

Time Complexity

  • Best Case: O(n²)
  • Average Case: O(n²)
  • Worst Case: O(n²)

Reason:
Regardless of whether the array is sorted or not, Selection Sort always scans the remaining elements to find the minimum or maximum.

Space Complexity

  • O(1) — It sorts the array in-place without using extra memory (other than a few variables).

Stability

  • Not stable by default. A stable sort preserves the relative order of equal elements; Selection Sort does not guarantee that because it swaps elements far apart.

When to Use It

  • When simplicity matters more than performance.
  • When working with very small arrays.
  • When teaching or learning how sorting works under the hood.

Not Ideal For

  • Large datasets — better choices like Merge Sort, Quick Sort, or TimSort (used by modern JS engines) are much faster.

Top comments (0)