forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.cpp
More file actions
36 lines (29 loc) · 729 Bytes
/
Solution.cpp
File metadata and controls
36 lines (29 loc) · 729 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
#include <iostream>
#include <vector>
using namespace std;
void printvec(const vector<int>& vec, const string& strbegin = "", const string& strend = "") {
cout << strbegin << endl;
for (auto val : vec) {
cout << val << "\t";
}
cout << endl;
cout << strend << endl;
}
void insertsort(vector<int>& vec) {
for (int i = 1; i < vec.size(); i++) {
int j = i - 1;
int num = vec[i];
for (; j >= 0 && vec[j] > num; j--) {
vec[j + 1] = vec[j];
}
vec[j + 1] = num;
}
return;
}
int main() {
vector<int> vec = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
printvec(vec);
insertsort(vec);
printvec(vec, "after insert sort");
return (0);
}