Selection Sort
Selection Sort Algorithm in C++ (Complete Implementation)
C++ Selection Sort Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
// Find minimum element in unsorted portion
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap found minimum with first element
if (minIndex != i) {
swap(arr[i], arr[minIndex]);
}
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Original array: ";
printArray(arr, n);
selectionSort(arr, n);
cout << "Sorted array: ";
printArray(arr, n);
return 0;
}Original array: 64 34 25 12 22 11 90 Sorted array: 11 12 22 25 34 64 90
Understanding Selection Sort
This program teaches you how to implement the Selection Sort algorithm in C++. Selection Sort works by repeatedly finding the minimum element from the unsorted portion and placing it at the beginning. It's simple to understand and implement, making it a good learning algorithm, though it's not efficient for large datasets.
---
1. What This Program Does
The program sorts an array of integers using the Selection Sort algorithm. For example:
---
2. Header File Used
This header provides:
---
#include <iostream>3. Understanding Selection Sort
Algorithm Concept
:
Visual Example
:
|sorted| |------unsorted------|
[11, 34, 25, 12, 22, 64, 90]
|sorted| |----unsorted----|
[11, 12, 25, 34, 22, 64, 90]
|--sorted--| |--unsorted--|
Continue until all sorted...
---
4. Function: selectionSort()
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
swap(arr[i], arr[minIndex]);
}
}
}
How it works
:
---
5. Step-by-Step Algorithm
Pass 1 (i=0)
:
Pass 2 (i=1)
:
Continue for n-1 passes...
---
6. Finding Minimum Element
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
How it works
:
Example
(finding min in [34, 25, 12, 22, 64, 90]):
---
7. Swap Operation
if (minIndex != i) {
swap(arr[i], arr[minIndex]);
}
How it works
:
Why Check minIndex != i?
:
---
8. Time and Space Complexity
Time Complexity
: O(n²) in all cases
Space Complexity
: O(1)
Stability
: Not Stable
---
9. When to Use Selection Sort
Best For
:
Not Recommended For
:
---
10. Important Considerations
Number of Passes
:
Comparison Count
:
Swap Count
:
---
11. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for beginners learning sorting algorithms, understanding minimum finding, and preparing for more efficient sorting algorithms in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Selection Sort
This C++ program is part of the "Sorting & Searching Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.