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

Deep Dive on Elasticsearch: A System Design Interview Perspective

Deep Dive on Elasticsearch: A System Design Interview Perspective

“If you’re searching, filtering, or aggregating over large volumes of semi…

Deep Dive on Apache Kafka: A System Design Interview Perspective

Deep Dive on Apache Kafka: A System Design Interview Perspective

“Kafka is not a message queue. It’s a distributed commit log that happens to be…

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

Deep Dive on Redis: Architecture, Data Structures, and Production Usage

“Redis is not just a cache. It’s a data structure server that happens to be…

Deep Dive on API Gateway: A System Design Interview Perspective

Deep Dive on API Gateway: A System Design Interview Perspective

“An API Gateway is the front door to your microservices. Every request walks…

REST API Design: Pagination, Versioning, and Best Practices

REST API Design: Pagination, Versioning, and Best Practices

Every time two systems need to talk, someone has to design the contract between…

Efficient Data Modelling: A Practical Guide for Production Systems

Efficient Data Modelling: A Practical Guide for Production Systems

Most engineers learn data modelling backwards. They draw an ER diagram…