-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
insertion_sort.h
38 lines (35 loc) · 929 Bytes
/
insertion_sort.h
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
/*******************************************************************************
* DANIEL'S ALGORITHM IMPLEMENTAIONS
*
* /\ | _ _ ._ o _|_ |_ ._ _ _
* /--\ | (_| (_) | | |_ | | | | | _>
* _|
*
* INSERTION SORT
*
* 1. sort array in O(n^2) time.
*
* http://en.wikipedia.org/wiki/Insertion_sort
*
******************************************************************************/
#ifndef ALGO_INSERTION_SORT_H__
#define ALGO_INSERTION_SORT_H__
namespace alg {
/**
* insertion sort an array
*/
template<typename T>
static void insertion_sort(T *array , int number_of_elements) {
int iter,jter;
for(iter=1;iter<number_of_elements;iter++) {
T current_element = array[iter];
jter = iter-1;
while(jter>=0 && array[jter] > current_element) {
array[jter+1] = array[jter];
jter--;
}
array[jter+1] = current_element;
}
}
}
#endif //