forked from priyankchheda/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsertionSort.cpp
42 lines (36 loc) · 996 Bytes
/
InsertionSort.cpp
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
/*
* Sorting Algorithms
* Insertion Sort (C++ implementation)
*
* Insertion sort is based on the idea that one element from the input
* elements is consumed in each iteration to find its correct position
* i.e, the position to which it belongs in a sorted array.
*/
#include <iostream>
/**
* Insertion Sort Template Function
* @param arr actual array to sort
* @param len size of array
*/
template <typename T>
void InsertionSort(T arr[], int len) {
for (int i = 0; i < len; i++) {
int index = i;
int value = arr[i];
while (index > 0 && arr[index-1] > value) {
arr[index] = arr[index-1];
index--;
}
arr[index] = value;
}
}
/* Main Driver Function */
int main() {
int arr[] = {229, 79, 46, 12, 58, 31, 34, 67, 89, 12, 67, 2};
int arr_size = sizeof(arr) / sizeof(arr[0]);
InsertionSort<int>(arr, arr_size);
for (int a: arr)
std::cout << a << " ";
std::cout << "\n";
return 0;
}