coding-interview|May 15, 2019|1 min read

Selection Sort Algorithm

TL;DR

Find the minimum element in the unsorted portion, swap it with the first unsorted position, repeat. O(n²) always, but simple and in-place.

Selection Sort Algorithm

It is one of a simple algorithm to study for a beginner to understanding sorting algorithms.

In this algorithm, we start with 0th index value. We compare it in whole array to find if any other number is smaller than this. So effectively, we find the minimum number through rest of the array from our starting index.

And, we swap the numbers of the initial number and the smalles number found in array.

Selection Sort Algorithm

  • We start with two loops. Outer loop gives inner loop the index of array to start with.
  • Inner array start with the index that outer loop gave.
  • Inner loop finds the minimum number
  • Swap the indexes, if found the smaller number
  • Inner loop breaks, Outer loop move ahead.
  • So, we left the smallest number found so far in beginning. And, sort rest of the array

See the code here:

public void sort(int[] arr) {
    int l = arr.length;
    for (int i=0; i<l-1; i++) {
        int smallestNumIndex = i;
        for (int j=i+1; j<l; j++) {
            if (arr[smallestNumIndex] > arr[j]) {
                smallestNumIndex = j;
            }
        }
        if (i != smallestNumIndex) {
            //swap
            int temp = arr[i];
            arr[i] = arr[smallestNumIndex];
            arr[smallestNumIndex] = temp;
        }
    }
}

Graphical Example

Selection Sort Example

Key Points

  • Its an in-place sorting algorithm
  • Performance is usually worse than Insertion sort
  • Applicable for small set of input only
  • Its very simple algorithm

Runtime complexity

The algorithm runs on O(n^2) in worst/average case.

Related Posts

Radix Sort Algorithm

Radix Sort Algorithm

A number consists of digits. Example: 843. Its a 3-digit number. Radix sort…

Counting Sort Algorithm

Counting Sort Algorithm

Counting sort runs on relatively smaller set of input. Counting sort calculates…

Heap Sort Algorithm

Heap Sort Algorithm

This is another very useful sorting algorithm based on Heap data structure. Read…

Quick Sort Algorithm

Quick Sort Algorithm

This algorithm is very useful for large input. And, is quite efficient one. It…

Merge Sort Algorithm

Merge Sort Algorithm

This algorithm is very efficient one, and is classic example of Divide and…

Bubble Sort Algorithm

Bubble Sort Algorithm

This is kind of preliminary technique of sorting. And, this is the first…

Latest Posts

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Claude Code Skills — Build a Better Engineering Workflow with AI-Powered Code Reviews, Security Scans, and More

Most developers use Claude Code like a search engine — ask a question, get an…

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Building an AI Voicebot for Visitor Check-In — A Practical Guide to Handling the Messy Parts

Every office lobby has the same problem: a visitor walks in, nobody’s at the…

Server Security Best Practices — Complete Hardening Guide for Production Systems

Server Security Best Practices — Complete Hardening Guide for Production Systems

Every breach post-mortem tells the same story: an unpatched service, a…

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

Staff Engineer Study Plan for MAANG Interviews — The Complete 12-Week Roadmap

If you’re a Senior Engineer (L5) preparing for Staff (L6+) roles at MAANG…

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF Explained — The Complete Guide with Real Attack Examples and Defenses

XSS and CSRF have been in the OWASP Top 10 for over a decade. They’re among the…

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

OWASP Top 10 (2021) — Every Vulnerability Explained with Code

The OWASP Top 10 is the industry standard for web application security risks. If…