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:
- Start from the first element of the array.
- Search the smallest element in the unsorted portion.
- Swap the smallest element with the first element.
- Shift the dividing line between the sorted and unsorted parts one position forward (next).
- 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 is10. Swap it with the first element45→[10, 22, 89, 33, 45] - Next, look at the subarray
[22, 89, 33, 45].
The smallest number is22, which is already in place. No swap needed. - Now move to
[89, 33, 45].
The minimum here is33. Swap it with89→[10, 22, 33, 89, 45] - Then, in
[89, 45], the smallest is45.
Swap it with89→[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 <iostream>
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 :

💛 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 ?
| Case | Time Complexity |
|---|---|
| Best case | O(n²) |
| Avg case | O(n²) |
| Worst case | O(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 <stdio.h>
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. Follow Embedded Prep for the latest trends, best practices, and step-by-step tutorials to enhance your expertise. Keep learning, keep innovating!
What is Selection Sort?
How does Selection Sort work?
What is the time complexity of Selection Sort?
Is Selection Sort stable?
You can also Visit other tutorials of Embedded Prep
- What is eMMC (Embedded MultiMediaCard) memory ?
- Top 30+ I2C Interview Questions
- Bit Manipulation Interview Questions
- Structure and Union in c
- Little Endian vs. Big Endian: A Complete Guide
- Merge sort algorithm
Special thanks to @embedded-prep for contributing to this article on Embedded Prep

Leave a Reply