AlgoPlus v0.1.0
Loading...
Searching...
No Matches
selection_sort.h
1#ifndef SELECTION_SORT_H
2#define SELECTION_SORT_H
3
4#ifdef __cplusplus
5#include <cstdint>
6#include <iostream>
7#include <vector>
8#endif
9
14template <typename T> void selection_sort(std::vector<T>& arr) {
15 int64_t min_idx = 0;
16 for (int64_t i = 0; i < arr.size() - 1; i++) {
17 min_idx = i;
18 for (int64_t j = i + 1; j < arr.size(); j++) {
19 if (arr[j] < arr[min_idx]) {
20 min_idx = j;
21 }
22 }
23 if (min_idx != i) {
24 std::swap(arr[min_idx], arr[i]);
25 }
26 }
27}
28
29#endif