-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinsertionsort.cpp
More file actions
70 lines (52 loc) · 1.67 KB
/
insertionsort.cpp
File metadata and controls
70 lines (52 loc) · 1.67 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
Insertionsort:
Worst case: n^2 -> If the vector is sorted inverted, it will take n(n+1)/2 iterations, yielding n^2 complexity
Best case: n -> If vector already sorted, then it takes n comparisons
average case: n^2 ->
stable: -> Yes
Description:
Similar to selectionsort, it starts by assuming the left sided already sorted, then it takes a new
element from the right side (the most left one) and inserts it to the corresponding index on the left
side, doing h swaps (h = distance from index to boundary for left and right sides)
*/
#ifndef INSERTIONSORT
#define INSERTIONSORT
template <typename T, typename Func>
void insertionsort(T *v, int size, Func eval) {
T tmp;
// Each iteration sets an element on a sorted vector located on the left side of v
for (int s=1, j; s<size; s++) {
// By saving the element on a tmp var the insertion is made faster
tmp = v[s];
// We will move all the elements to the right, until we reach the begining or we find a element that doesn't satisfy the evaluator
for (j=s; j>0 && eval(tmp, v[j-1]); j--)
v[j] = v[j-1];
// The element is set on the sorted index
v[j] = tmp;
}
}
// Default order set on ascending
template <typename T>
void insertionsort(T *v, int size) {
insertionsort(v, size, [] (T a, T b) { return a<b; });
}
#ifndef VECTOR_TEST
// Testing
#include <iostream>
#include <cstdlib>
#include <ctime>
#include "vector_utility.cpp"
using namespace std;
int main() {
int size=50;
int *v = (int*)malloc(size*sizeof(int));
initRandomV(v, size, size, 0);
printV(v, size);
insertionsort(v, size, [] (int a, int b) {return a>b;});
printV(v, size);
insertionsort(v, size);
printV(v, size);
return 0;
}
#endif
#endif