2.shell排序 #include <iostream> #include <vector> using namespace std; template <typename T> void shellSort(vector<T> &a) { for (int gap = a.size() / 2; gap > 0; gap /= 2) { for (int i = gap; i < a.size(); i++) { T tmp = a[i]; int j = i; for (; j >= gap && tmp < a[j - gap]; j -= gap) a[j] = a[j - gap]; a[j] = tmp; } } } int main() { vector<int> test = {190, 435, 834, 8954, 923, 56, 20 ,1, 934}; shellSort(test); for (auto i : test) cout << i << " "; cout << endl; return 0; } 运行结果: $ ./a.out 1 20 56 190 435 834 923 934 8954 我的个人主页:http://www.techping.cn/我的个人站点博客:http://blog.techping.cn/我的CSDN博客:http://blog.csdn.net/techping我的简书:http://www.jianshu.com/users/b2a36e431d5e/timeline我的GitHub:https://github.com/techping