希爾排序C++實現

gf67 9年前發布 | 930 次閱讀 C/C++

//希爾排序

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]); } } } } </pre>

 本文由用戶 gf67 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!