Selection Sort in C++
Sort an array with selection sort in C++ by finding the minimum of the unsorted part and swapping it forward. At most n − 1 swaps, always O(n²) compares.
1#include <iostream>2using namespace std;34void selectionSort(int arr[], int n) {5 for (int i = 0; i < n - 1; i++) {6 int minIndex = i;78 // Find minimum element in unsorted portion9 for (int j = i + 1; j < n; j++) {10 if (arr[j] < arr[minIndex]) {11 minIndex = j;12 }13 }1415 // Swap found minimum with first element16 if (minIndex != i) {17 swap(arr[i], arr[minIndex]);18 }19 }20}2122void printArray(int arr[], int n) {23 for (int i = 0; i < n; i++) {24 cout << arr[i] << " ";25 }26 cout << endl;27}2829int main() {30 int arr[] = {64, 34, 25, 12, 22, 11, 90};31 int n = sizeof(arr) / sizeof(arr[0]);3233 cout << "Original array: ";34 printArray(arr, n);3536 selectionSort(arr, n);3738 cout << "Sorted array: ";39 printArray(arr, n);4041 return 0;42}
Sample Output
Sorted array: 11 12 22 25 34 64 90
All passes done.
In this one
- How the sorted prefix grows by one each pass
- How minIndex tracks the smallest value
- Why selection sort does few swaps
- Why it is not stable
How it works
- 1
i = 0: scan the rest for the smallest (11).
- 2
Swap it with arr[0].
- 3
i = 1: find the next smallest (12) and swap.
- 4
Continue until i = n − 2.
First two passes
Minimum found and swap made.
- 1.Pass 0: min 11 at index 5 → 11 34 25 12 22 64 90
- 2.Pass 1: min 12 at index 3 → 11 12 25 34 22 64 90
Edge cases
Already sorted
Use 1 2 3.
It still scans for the minimum every pass, so it does the full n²/2 comparisons. Only the swaps are skipped.
Duplicates
Use 2a 2b 1.
Swapping 2a with 1 gives 1 2b 2a, so equal elements change order. Selection sort is not stable.
What's going on
The program sorts an array of integers using the Selection Sort algorithm. For example:
Selection Sort divides the array into two parts: sorted (at the beginning) and unsorted (remaining elements). In each iteration, it finds the minimum from the unsorted part and adds it to the sorted part.
Header File Used
#include <iostream>
This header provides:
Understanding Selection Sort
Algorithm Concept:
Visual Example:
Initial: [64, 34, 25, 12, 22, 11, 90]
|sorted| |------unsorted------|
Pass 1: Find min (11) → swap with 64
[11, 34, 25, 12, 22, 64, 90]
|sorted| |----unsorted----|
Pass 2: Find min (12) → swap with 34
[11, 12, 25, 34, 22, 64, 90]
|--sorted--| |--unsorted--|
Continue until all sorted...
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:
Step-by-Step Algorithm
Pass 1 (i=0):
Pass 2 (i=1):
Continue for n-1 passes...
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]):
Swap Operation
if (minIndex != i) {
swap(arr[i], arr[minIndex]);
}
How it works:
Why Check minIndex != i?:
Time and Space Complexity
Time Complexity: O(n²) in all cases
Space Complexity: O(1)
Stability: Not Stable
When to Use Selection Sort
Best For:
Not Recommended For:
Important Considerations
Number of Passes:
Comparison Count:
Swap Count:
return 0;
This ends the program successfully.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
How many swaps at most for n elements?
Pick an answer to check it.
Question 2
After pass i, positions 0..i hold:
Pick an answer to check it.
Questions people ask
Why check minIndex != i?
It skips a useless self-swap.
When is it useful?
When writes are expensive, since it makes at most n − 1 swaps.
Selection vs insertion?
Insertion adapts to sorted input; selection does not.