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
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
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
- 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
Mr. Raj Kumar is a highly experienced Technical Content Engineer with 7 years of dedicated expertise in the intricate field of embedded systems. At Embedded Prep, Raj is at the forefront of creating and curating high-quality technical content designed to educate and empower aspiring and seasoned professionals in the embedded domain.
Throughout his career, Raj has honed a unique skill set that bridges the gap between deep technical understanding and effective communication. His work encompasses a wide range of educational materials, including in-depth tutorials, practical guides, course modules, and insightful articles focused on embedded hardware and software solutions. He possesses a strong grasp of embedded architectures, microcontrollers, real-time operating systems (RTOS), firmware development, and various communication protocols relevant to the embedded industry.
Raj is adept at collaborating closely with subject matter experts, engineers, and instructional designers to ensure the accuracy, completeness, and pedagogical effectiveness of the content. His meticulous attention to detail and commitment to clarity are instrumental in transforming complex embedded concepts into easily digestible and engaging learning experiences. At Embedded Prep, he plays a crucial role in building a robust knowledge base that helps learners master the complexities of embedded technologies.













