排序_Shell_Sort(希爾排序)

來源:互聯網
上載者:User

希爾排序還是不錯的,時間複雜度在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;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.