//希尔排序
#include<iostream>
#include<array>
using namespace std;
template<class T>
void shell_sort(T&, int);
int main()
{
array<int, 10> arr = {1,2,3,5,4,6,7,8,9,0};
shell_sort(arr, arr.size());
for(auto i:arr)
{
cout << i << endl;
}
return 0;
}
template<class T>
void shell_sort(T& arr, int cont)
{
for(int increment = cont/2; increment > 0; increment/=2)
{
for(int j = 0; j < cont; j++) //切记是向已经排好序的数中再进行比较
{
for(int k = j; k-increment >= 0 && arr[k] > arr[k-increment]; k -= increment)
{
swap(arr[k], arr[k-increment]);
}
}
}
}
时间: 2024-10-13 10:51:20