DECMATRIX

Mathematical Tools

Selection Sort
i
Enter up to 21 integer numbers from -999 to 999, separated by commas.
5
0
1
-3
8
2
2
3
4
-2
2
5

Steps:

Run the algorithm to see the recorded steps.

No steps recorded yet.
SlowFast

Code

Selection Sort is a simple sorting algorithm that divides the array into two parts: a sorted part and an unsorted part. In each iteration, it scans the unsorted part to find the smallest element and swaps it with the first element of the unsorted part, effectively moving it to the sorted part. This process is repeated until the entire array is sorted.

Complexity: Unlike Bubble Sort and Insertion Sort, Selection Sort is not adaptive. It does not know if the list is already sorted and will always perform a complete scan. Therefore, its time complexity is O(n²) in all scenarios (worst, average, and best case). Its only significant advantage over the other two algorithms is the reduced number of swaps in memory (at most O(n) swaps). It is an in-place algorithm with O(1) space complexity.

1const selectionSort = (arr) => {
2    for (let i = 0; i < arr.length - 1; i++) {
3        let minIndex = i;
4        
5        // Scan for the smallest element
6        for (let j = i + 1; j < arr.length; j++) {
7            if (arr[j] < arr[minIndex]) {
8                minIndex = j;
9            }
10        }
11        
12        // If needed, swap it into position
13        if (minIndex !== i) {
14            [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
15        }
16    }
17    
18    return arr;
19};

Definition of Selection Sort

Selection Sort divides the list into two parts: a sorted part (on the left) and an unsorted part (on the right). On each iteration, it finds the smallest element in the unsorted part and moves it to the end of the sorted part. This process repeats until the entire list is sorted.

How Selection Sort Works

  1. Consider the first element of the unsorted part as the current minimum.
  2. Scan all elements to the right, comparing each one against the current minimum.
  3. If a smaller value is found, it becomes the new current minimum.
  4. After the scan, perform a single swap: place the smallest value found at the first position of the unsorted part.
  5. Advance the sorted boundary one position and repeat until the end of the array.

Applications and Real-World Usage

Like Bubble Sort, Selection Sort is rarely used in production due to its quadratic complexity. Its main advantage is performing the minimum possible number of swaps — at most N−1. This makes it useful in constrained hardware environments, such as flash memory (EEPROM), where each write operation consumes processing power and physically degrades the chip.

Properties of Selection Sort

  1. Time complexity: O(n²) in all cases — best, average, and worst. Because it is not adaptive, the algorithm always scans the entire list on every pass, even if it is already sorted.
  2. Space complexity: O(1). It is an in-place algorithm: elements are reordered within the original array, using only a single temporary variable for the swap.
  3. Stability: Not stable. The long-distance swap can reverse the relative order of two elements with equal values.

FAQ

01. What is the main difference between Selection Sort and Insertion Sort?

02. If it is always O(n²), why learn this algorithm?

03. Why is classic Selection Sort not stable?

04. Does Selection Sort require extra memory?

Decmatrix - Your open-source mathematical toolkit for calculations, conversions, and more.
Terms of Use
Privacy Policy
Contact: decimatrix25@gmail.com