//希爾排序 #include #include using namespace std; template void shell_sort(T&, int); int main() { array 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 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]); } } } }