希爾排序還是不錯的,時間複雜度在O(n^1.3)
這是我自己寫出來的代碼,我什麼也沒看別人的代碼就是根據自己的理解自己敲出來的,大家看看:
#include <stdio.h>#include <string.h>#include <math.h>#include <iostream>#include <algorithm>#include <string>using namespace std;int a[10000];int N;void Shell (){int d = N;while (d > 0){d = d >> 1; // 這是分組的距離情況 for (int k = 0 ; k < d; k++) // 這是限制分組的比較次數 {for (int i = k + d; i < N; i += d) //這是直接插入排序,這裡一定要注意的是,這裡的距離是d {int j = i - d; // 注意這裡 int t = a[i];while (t < a[j] && j >= 0){a[j + d] = a[j]; //注意這裡 j -= d; //注意這裡 }a[j + d] = t; // 注意這裡 }for (int i = 0; i < N; i++){cout << a[i] << " ";}cout << endl;}}} int main(){cout << "Input the sorting number that you want: " << endl;cin >> N;cout << "Please input the numbers: " << endl;for (int i = 0; i < N; i++){cin >> a[i];}Shell();for (int i = 0; i < N; i++){cout << a[i] << " ";}cout << endl;system ("pause");return 0;}