forked from harsh2102/C
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_insertion_sort.cpp
More file actions
39 lines (32 loc) · 856 Bytes
/
binary_insertion_sort.cpp
File metadata and controls
39 lines (32 loc) · 856 Bytes
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
#include<iostream>
#include<algorithm>
#include<vector>
template<typename T>
/* A function to print values inside a vector */
std::ostream& operator<<(std::ostream& out, std::vector<T>& ls)
{
out<<'[';
for(auto i = ls.begin(); i != ls.end(); ++i)
out<<*i<<(i + 1 == ls.end() ? "" : ", ");
out<<']';
return out;
}
template<typename Container>
/* The main insertion sort function */
void binary_insertion_sort(Container& ctx)
{
for(auto a = ctx.begin(); a != ctx.end(); ++a)
std::rotate(
std::upper_bound(ctx.begin(), a, *a),
//determines the maximum element in the range
a,
a + 1
); //pushes the element to the last
}
int main()
{
std::vector<int> vec = {90, 1, 20, 45, 0, 18, 3};
std::cout<<"The vector before sorting "<<vec<<std::endl;
binary_insertion_sort(vec);
std::cout<<"After sorting "<<vec<<std::endl;
}