Master the Selection Sort Algorithm in 5 Simple Steps (2026)

0b63979cd9494aa401d1fce2d73bb002
On: April 13, 2025
Selection Sort Algorithm

If you’re diving into sorting algorithms, the selection sort algorithm is one of the simplest and most intuitive methods to start with. Despite being basic, it’s a great way to understand the fundamentals of sorting logic. In this blog post, we’ll walk through what selection sort is, how it works, its time complexity, and a sample code in C++.

What is selection sort ?

What is selection sort : Selection sort is a kind of sorting method based on comparisons technique. It separates the array into a sorted parts and an unsorted parts, then repeatedly selects the smallest element from the unsorted parts and places it at the end of the sorted parts

How the Selection Sort Algorithm Works

Here’s a step-by-step working techinuque of selection sort explanation:

  1. Start from the first element of the array.
  2. Search the smallest element in the unsorted portion.
  3. Swap the smallest element with the first element.
  4. Shift the dividing line between the sorted and unsorted parts one position forward (next).
  5. Repeat until the array is sorted.

Example of Selection Sort Algorithm

Let’s say we have an below array:

[45, 22, 89, 33, 10]

How selection sort works ?

Let’s gone through below steps how selection sort would organize it:

  • First, examine the entire list [45, 22, 89, 33, 10].
    The smallest value is 10. Swap it with the first element 45[10, 22, 89, 33, 45]
  • Next, look at the subarray [22, 89, 33, 45].
    The smallest number is 22, which is already in place. No swap needed.
  • Now move to [89, 33, 45].
    The minimum here is 33. Swap it with 89[10, 22, 33, 89, 45]
  • Then, in [89, 45], the smallest is 45.
    Swap it with 89[10, 22, 33, 45, 89]

The array is now sorted using selection sort algorithm.

Is selection sort stable ?

Selection sort is stable or not : Selection Sort is inherently unstable — but not because it swaps elements, rather because it does not respect the original order of equivalent elements when finding the minimum.

Let’s reimagine this through a theater analogy:

Imagine you’re organizing people (elements) by height (value) for a stage performance. You always pick the shortest person (minimum) and move them to the front — but without caring whether two people of the same height had different roles (original positions). You just swap whoever is found first in your search, even if it pushes someone with an earlier role to the back.

So in essence:

  • Selection Sort does not maintain “role memory” for equal values.
  • It simply grabs the minimum, regardless of earlier appearances.
  • This causes instability, because if two equal elements are separated by a swap, the relative order is broken.

However — and here’s the twist Google doesn’t tell you:

Selection Sort can be made stable with a simple tweak: instead of swapping, insert the minimum in-place while shifting others to the right.

But that tweak costs performance. So while basic Selection Sort is not stable, a thoughtful rewrite can make it so, at the cost of efficiency.so now we able to understand why selection sort is not stable .

What is selection sort in c ++?

#include 
using namespace std;

void selectionSort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        int minIdx = i;
        
        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIdx])
                minIdx = j;
        }

        swap(arr[minIdx], arr[i]);
    }
}

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++)
        cout << arr[i] << " ";
    cout << endl;
}

int main() {
    int arr[] = {45,22,89,33,10};
    int n = sizeof(arr) / sizeof(arr[0]);
    
    cout << "Original array of array is : ";
    printArray(arr, n);
    
    selectionSort(arr, n);

    cout << "Sorted array: ";
    printArray(arr, n);

    return 0;
}

Output :

Selection Sort Algorithm 1

💛 Support Embedded Prep

If you find our tutorials helpful and want to support our mission of sharing high-quality embedded system knowledge, you can contribute by buying us a coffee. Every small contribution helps us keep creating valuable content for learners like you. ☕

Thank you for your support — it truly keeps Embedded Prep growing. 💻✨

What is the best case and worst case complexity of selection sort ?

CaseTime Complexity
Best caseO(n²)
Avg caseO(n²)
Worst caseO(n²)

Space Complexity: O(1)
Because selection sort sorts elements directly within the original array, it maintains a constant space complexity throughout the process.

What is selection sort in c ?

#include 

int digitSum(int n) {
    int sum = 0;
    while(n) {
        sum += n % 10;
        n /= 10;
    }
    return sum;
}

void selectionSortByDigitSum(int arr[], int n) {
    for(int i = 0; i < n - 1; i++) {
        int min = i;
        for(int j = i + 1; j < n; j++) {
            if(digitSum(arr[j]) < digitSum(arr[min])) {
                min = j;
            }
        }
        int temp = arr[i];
        arr[i] = arr[min];
        arr[min] = temp;
    }
}

int main() {
    int arr[] = {91, 34, 23, 82, 17};
    int n = sizeof(arr)/sizeof(arr[0]);

    selectionSortByDigitSum(arr, n);

    for(int i = 0; i < n; i++)
        printf("%d ", arr[i]);
        
    return 0;
}

When to Choose Selection Sort?

Selection sort may not be the most efficient option for large datasets, but it excels in the following scenarios:

  • Simple to grasp
  • Ideal for small arrays
  • Excellent for teaching and learning purposes

Conclusion

The selection sort algorithm is a simple yet powerful concept that helps build a strong foundation in sorting logic. Though it’s not the fastest for large arrays, its ease of implementation and step-by-step nature make it ideal for beginners.

Thank you for exploring Selection sort algorithm tutorials ! Stay ahead in embedded systems with expert insights, hands-on projects, and in-depth guides. FollowEmbedded Prepfor the latest trends, best practices, and step-by-step tutorials to enhance your expertise. Keep learning, keep innovating!

What is Selection Sort?

Answer: Selection Sort is a simple sorting algorithm that works by repeatedly finding the smallest element and swapping it with the current element.

How does Selection Sort work?

Answer: The algorithm divides the array into two parts: sorted and unsorted. It selects the minimum element from the unsorted part and swaps it with the leftmost unsorted element.

What is the time complexity of Selection Sort?

Answer: The time complexity of Selection Sort is O(n^2) in all cases (best, worst, and average).

Is Selection Sort stable?

Answer: No, Selection Sort is not a stable sorting algorithm, as it may change the relative order of equal elements.

You can also Visit other tutorials of Embedded Prep

Special thanks to @embedded-prep for contributing to this article on Embedded Prep

Leave a Comment