Selection Sort repeatedly selects the smallest element and places it in its correct position.
Algorithm:
- Start from the first element of the array.
- Assume the current element is the smallest.
- Compare it with all the remaining elements in the unsorted part of the array.
- Find the smallest element.
- Swap the smallest element with the first unsorted element.
- Move to the next position and repeat the process until the array is sorted.
Pseudocode:
SelectionSort(arr, n)
for i = 0 to n - 2 do
min = i
for j = i + 1 to n - 1 do
if arr[j] < arr[min] then
min = j
end for
if min != i then
swap arr[i] and arr[min]
end if
end forSelection Sort Demo
Array Input
Comma or space separated · 2-16 numbers · 1-999 each
1. What is Selection Sort?
Selection Sort is a sorting algorithm that repeatedly:
- finds the minimum element from the unsorted part,
- places it at the beginning.
2. Dry Run Selection Sort.
Input: 96 43 74 72 46 Output: 43 46 72 74 96
arr[0] = 96 arr[1] = 43 arr[2] = 74 arr[3] = 72 arr[4] = 46
Pass 1:
- Minimum = 43
- Swaps arr[1] = 43 and arr[0] = 96
- Result:
4396 74 72 46
Pass 2:
- Minimum = 46
- Swaps arr[4] = 46 and arr[1] = 96
- Result:
43 4674 72 96
Pass 3:
- Minimum = 72
- Swaps arr[3] = 72 and arr[2] = 74
- Result:
43 46 7274 96
Pass 4:
- Minimum = 74
- Result:
43 46 72 74 96
3. Code Implementation
Example:
#include <iostream>
using namespace std;
void selectionSort(int arr[], int n)
{
for (int i = 0; i < n - 1; i++)
{
int idx = i;
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[idx])
{
idx = j;
}
}
swap(arr[i], arr[idx]);
}
}
void printArray(int arr[], int n, string msg = "")
{
cout << msg << endl;
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}
int main()
{
int arr[] = {5, 16, 4, 71, 2, 11};
int n = sizeof(arr) / sizeof(arr[0]);
printArray(arr, n, "Original array:");
selectionSort(arr, n);
printArray(arr, n, "Sorted array(asc):");
return 0;
} Output:
Original array: 5 16 4 71 2 11 Sorted array(asc): 2 4 5 11 16 71
4. What is the time complexity of Selection Sort?
Best, Average, Worst: O(n2)
Because:
- Two nested loops are always used.
- Number of comparisons does not depend on input order.
5. What is the space complexity?
O(1)
6. Is Selection Sort stable?
No, Equal elements may change their relative order after swapping.
Example:
2a 2b 1 After sorting: 1 2b 2a
7. Is Selection Sort adaptive?
No, Even if array is already sorted, it still performs all comparisons.
8. Why is Selection Sort called “Selection” Sort?
Because in every pass:
- it selects the minimum element
- and places it in correct position.
9. How many swaps occur in Selection Sort?
Maximum: n - 1
Unlike Bubble Sort, Selection Sort performs fewer swaps.
10. Why can Selection Sort be useful despite being slow?
Because:
- it minimizes swaps,
- useful when swapping cost is expensive.
Example:
- EEPROM memory
- Flash storage systems
11. Why is Selection Sort not preferred for large datasets?
Because:
- Time complexity is always quadratic.
O(n2) - Algorithms like Merge Sort and Quick Sort are much faster.
12. Does Selection Sort reduce comparisons if array is already sorted?
No, It always performs: n * (n - 1 ) / 2 comparisons.
13. Why is Selection Sort better than Bubble Sort in some cases?
Because:
- swaps are expensive,
- selection Sort minimizes writes/swaps.
14. Can Selection Sort be implemented recursively?
Yes
- place minimum element first,
- recursively sort remaining array.
15. Differences between bubble, insertion & selection sort.
| Feature | Bubble Sort | Insertion Sort | Selection Sort |
|---|---|---|---|
| Basic Idea | Repeatedly swap adjacent elements if they are in the wrong order. | Insert each element into its correct position in the sorted part. | Find the minimum element and place it at its correct position. |
| How It Works | Largest element "bubbles" to the end in each pass. | Builds a sorted portion one element at a time. | Selects the smallest element from the unsorted portion. |
| Best Case | O(n) | O(n) | O(n²) |
| Average Case | O(n²) | O(n²) | O(n²) |
| Worst Case | O(n²) | O(n²) | O(n²) |
| Space Complexity | O(1) | O(1) | O(1) |
| Stable? | ✅ Yes | ✅ Yes | ❌ No |
| Adaptive? | ✅ Yes | ✅ Yes | ❌ No |
| Number of Swaps | High | Low (mostly shifts) | Minimum |
| Best For | Learning sorting basics. | Small or nearly sorted arrays. | When minimizing swaps is important. |
| Real-Life Analogy | Bubbles rising to the surface. | Arranging playing cards in your hand. | Picking the smallest item repeatedly from a pile. |
Sorting Algorithms
Array Input
Comma or space separated · 2-16 numbers · 1-999 each
Selection Sort repeatedly selects the minimum element from the unsorted portion and places it at its correct position.