希尔排序(缩小增量排序)

 1 #include<iostream>
 2 #include<vector>
 3 using namespace std;
 4 
 5 
 6 struct Sort {
 7     
 8      inline void operator()(vector<int>&vec){
 9          for (int N = vec.size() / 2+1; N > 0; N/=2) {
10              cout << N<<endl;
11              for (size_t i = N; i < vec.size(); ++i) {
12                  if(vec[i-N]>vec[i])std::swap(vec[i - N], vec[i]);
13                  for (auto& i : vec)
14                  cout << i << ends;
15              cout << endl;
16              }
17              
18          }
19      }
20 };
21 Sort s;
22 int main() {
23     vector<int>vec{ 11,10,9,8,7,6,5,4,3,2,1,0 };
24     for (auto& i : vec)
25         cout << i << ends;
26     cout << endl;
27     s(vec);
28     for (auto& i : vec)
29         cout << i << ends;
30 }

 经过测试,有点问题

所以最后需要希尔排序的最后一趟退化成直接插入排序

posted @ 2020-10-16 10:04  世界を変える御宅族  阅读(167)  评论(0编辑  收藏  举报