-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinsert_sort.cc
43 lines (37 loc) · 878 Bytes
/
insert_sort.cc
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
#include <bits/stdc++.h>
using namespace std;
/*void InsertSort(vector<int>& input) {
if (input.size() <= 1) {
return ;
}
for (int i=1; i<input.size(); i++) {
for (int j=i; j>0 && input[j]<input[j-1]; j--) {
int tmp = input[j-1];
input[j-1] = input[j];
input[j] = tmp;
}
}
return;
}
*/
template <class RandomAccessIterator>
void InsertSort(RandomAccessIterator first,
RandomAccessIterator last) {
for (RandomAccessIterator i = first;
i != last;
i++) {
for (RandomAccessIterator j=i;
j != first && *j < *(j-1); j--) {
swap(*(j-1), *j);
}
}
return;
}
int main() {
std::vector<int> v = {3, 2, 1};
InsertSort(v.begin(), v.end());
for (auto i : v) {
cout << i << endl;
}
return 0;
}