C++实现插入排序
自学视频:算法时空(极简数据结构)#include <iostream>
#include <vector>
using namespace std;
class Solution{
public:
void sorted(vector<int>* nums){
for(int i = 0; i < nums -> size(); i++){
int key = (*nums);
std::vector<int>::iterator position = lower_bound(nums -> begin(), nums -> begin() + i, key);
for(std::vector<int>::iterator iter = nums -> begin() + i; iter > position; iter--){
(*iter) = *(iter - 1);
}
*position = key;
}
}
};
int main()
{
int num;
cout << "send number" << endl;
vector<int> list;
while(cin >> num)
{
list.push_back(num);
}
Solution test;
test.sorted(&list);
vector<int>::iterator it;
for(it = list.begin(); it != list.end(); it++)
{
cout << *it <<" ";
}
return 0;
} 有意思
页:
[1]