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