logo
SEE ALGORITHMS
SORTING
    Bubble Sort
    Insertion Sort
    Selection Sort
    Heap Sort
    Radix Sort
    Merge Sort
    Quick Sort

Selection Sort

Selection Sort is another comparison-based algorithm that sorts an array by repeatedly finding the minimum element from the unsorted part and moving it to its correct position. It minimizes the number of swaps needed compared to Bubble Sort, which makes it useful when the cost of moving items is high, but finding the smallest item is easy.


Pseudocode

for i = 0 to (n - 1):
    min = i
    for j = i + 1 to (n - 1):
        if arr[j] < arr[min]:
            min = j
    if min != i: swap(i, min)

Visualizer

Select number of elements:  


How It Works

Selection Sort divides the array into two logical parts: a sorted region at the beginning and an unsorted region at the end. In each iteration, the algorithm scans the entire unsorted region to find the smallest element. Once found, that element is swapped with the first element of the unsorted region, effectively expanding the sorted region by one. This process repeats until the entire array is sorted. Unlike Bubble Sort, which may perform multiple swaps per pass, Selection Sort performs a single swap only after finding the final minimum for that pass.

When to Use

Selection Sort is best suited for small arrays or situations where memory writes are significantly more expensive than reads (such as writing to flash memory). Due to its quadratic time complexity, Selection Sort is impractical for large datasets. More advanced divide-and-conquer algorithms are generally the industry standard for efficiency.

Time & Space Complexity

Metric / Operation
Complexity
Description
Best CaseO(n²)Even if the array is already sorted, Selection Sort still scans the entire unsorted portion in each pass to confirm the minimum.
Average CaseO(n²)The number of comparisons is always n(n-1)/2 regardless of input order.
Worst CaseO(n²)Same as average case — the algorithm always performs the same number of comparisons.
Space ComplexityO(1)Selection Sort is an in-place algorithm requiring only a constant amount of extra memory for the swap variable.

Curious to Learn More?

Hand-picked resources to deepen your understanding

Beginner Friendly
Coding Interview Bootcamp: Algorithms + Data Structures

Learn essential data structures and algorithms step-by-step with practical JavaScript examples.

Practical Guide
JavaScript Algorithms & Data Structures Masterclass

Master DSA fundamentals, problem-solving techniques, and advanced structures using JavaScript.

Deep Dive
Master the Coding Interview: Data Structures + Algorithms

Prepare for top tech interviews with advanced DSA concepts and real-world coding challenges.

Learn DSA on Udemy
Learn DSA on Udemy
As an Udemy Associate, I earn from qualifying purchases.

© 2025 See Algorithms. Code licensed under MIT, content under CC BY-NC 4.0.