-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection-sorting.cpp
More file actions
49 lines (45 loc) · 1.58 KB
/
selection-sorting.cpp
File metadata and controls
49 lines (45 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* @file SelectionSort.cpp
* @brief Template implementation of the Selection Sort algorithm.
* * Selection sort works by repeatedly finding the minimum element from the
* unsorted part and putting it at the beginning.
*/
#include <iostream>
using namespace std;
/**
* @brief Sorts a fixed-size array using the Selection Sort algorithm.
* * This implementation utilizes template argument deduction to handle
* various data types and retrieve the array size 'n' automatically.
* * @tparam T The data type of the array elements (must support comparison operators).
* @tparam n The size of the array, deduced by the compiler via reference.
* @param arr A reference to the array to be sorted.
*/
template <typename T, size_t n>
void selectionSort(T (&arr)[n])
{
// One by one move the boundary of the unsorted subarray
for (size_t i = 0; i < n - 1; i++)
{
// Find the minimum element in the unsorted part
size_t min_idx = i;
for (size_t j = i + 1; j < n; j++)
{
if (arr[j] < arr[min_idx])
{
min_idx = j;
}
}
// Swap the found minimum element with the first element of the unsorted part
if (min_idx != i)
{
swap(arr[i], arr[min_idx]);
}
}
}
/**
* Technical Implementation Details:
* 1. Pass-by-reference 'T (&arr)[n]' prevents array-to-pointer decay.
* 2. Time Complexity: O(n^2) for all cases (Best, Average, Worst).
* 3. Space Complexity: O(1) as it is an in-place sorting algorithm.
* 4. Stability: Selection sort is generally unstable.
*/