SIGN IN SIGN UP
doocs / leetcode UNCLAIMED

🔥LeetCode solutions in any programming language | 多种编程语言实现 LeetCode、《剑指 Offer(第 2 版)》、《程序员面试金典(第 6 版)》题解

35846 0 13 Java
2021-09-08 10:45:39 +08:00
#include <iostream>
#include <vector>
using namespace std;
void printvec(const vector<int>& vec, const string& strbegin = "", const string& strend = "") {
2021-09-08 10:45:39 +08:00
cout << strbegin << endl;
for (auto val : vec) {
2021-09-08 10:45:39 +08:00
cout << val << "\t";
}
cout << endl;
cout << strend << endl;
}
void insertsort(vector<int>& vec) {
for (int i = 1; i < vec.size(); i++) {
2021-09-08 10:45:39 +08:00
int j = i - 1;
int num = vec[i];
for (; j >= 0 && vec[j] > num; j--) {
2021-09-08 10:45:39 +08:00
vec[j + 1] = vec[j];
}
vec[j + 1] = num;
}
return;
}
int main() {
2021-09-08 10:45:39 +08:00
vector<int> vec = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
printvec(vec);
insertsort(vec);
printvec(vec, "after insert sort");
return (0);
}