<strike id="cakm0"></strike>
  • <button id="cakm0"><dl id="cakm0"></dl></button>
  • <samp id="cakm0"><tbody id="cakm0"></tbody></samp>
    <samp id="cakm0"><pre id="cakm0"></pre></samp><ul id="cakm0"></ul>
    <strike id="cakm0"></strike>
    <li id="cakm0"></li>
  • <ul id="cakm0"></ul>
  • 更多精彩內容,歡迎關注:

    視頻號
    視頻號

    抖音
    抖音

    快手
    快手

    微博
    微博

    希爾排序算法c語言代碼

    文檔

    希爾排序算法c語言代碼

    希爾排序,也稱遞減增量排序算法,是插入排序的一種更高效的改進版本。但希爾排序是非穩定排序算法。
    推薦度:
    導讀希爾排序,也稱遞減增量排序算法,是插入排序的一種更高效的改進版本。但希爾排序是非穩定排序算法。
    .example-btn{color:#fff;background-color:#5cb85c;border-color:#4cae4c}.example-btn:hover{color:#fff;background-color:#47a447;border-color:#398439}.example-btn:active{background-image:none}div.example{width:98%;color:#000;background-color:#f6f4f0;background-color:#d0e69c;background-color:#dcecb5;background-color:#e5eecc;margin:0 0 5px 0;padding:5px;border:1px solid #d4d4d4;background-image:-webkit-linear-gradient(#fff,#e5eecc 100px);background-image:linear-gradient(#fff,#e5eecc 100px)}div.example_code{line-height:1.4em;width:98%;background-color:#fff;padding:5px;border:1px solid #d4d4d4;font-size:110%;font-family:Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;word-break:break-all;word-wrap:break-word}div.example_result{background-color:#fff;padding:4px;border:1px solid #d4d4d4;width:98%}div.code{width:98%;border:1px solid #d4d4d4;background-color:#f6f4f0;color:#444;padding:5px;margin:0}div.code div{font-size:110%}div.code div,div.code p,div.example_code p{font-family:"courier new"}pre{margin:15px auto;font:12px/20px Menlo,Monaco,Consolas,"Andale Mono","lucida console","Courier New",monospace;white-space:pre-wrap;word-break:break-all;word-wrap:break-word;border:1px solid #ddd;border-left-width:4px;padding:10px 15px}

    排序算法是《數據結構與算法》中最基本的算法之一。排序算法可以分為內部排序和外部排序,內部排序是數據記錄在內存中進行排序,而外部排序是因排序的數據很大,一次不能容納全部的排序記錄,在排序過程中需要訪問外存。常見的內部排序算法有:插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序、基數排序等。以下是希爾排序算法:

    希爾排序,也稱遞減增量排序算法,是插入排序的一種更高效的改進版本。但希爾排序是非穩定排序算法。

    希爾排序是基于插入排序的以下兩點性質而提出改進方法的:

    插入排序在對幾乎已經排好序的數據操作時,效率高,即可以達到線性排序的效率;但插入排序一般來說是低效的,因為插入排序每次只能將數據移動一位;

    希爾排序的基本思想是:先將整個待排序的記錄序列分割成為若干子序列分別進行直接插入排序,待整個序列中的記錄"基本有序"時,再對全體記錄進行依次直接插入排序。

    1. 算法步驟

    選擇一個增量序列 t1,t2,……,tk,其中 ti > tj, tk = 1;

    按增量序列個數 k,對序列進行 k 趟排序;

    每趟排序,根據對應的增量 ti,將待排序列分割成若干長度為 m 的子序列,分別對各子表進行直接插入排序。僅增量因子為 1 時,整個序列作為一個表來處理,表長度即為整個序列的長度。

    2. 動圖演示

    代碼實現JavaScript實例 function shellSort(arr) {? ? var len = arr.length,? ? ? ? temp,? ? ? ? gap = 1;? ? while(gap < len/3) { ? ? ? ? ?//動態定義間隔序列? ? ? ? gap =gap*3+1;? ? }? ? for (gap; gap > 0; gap = Math.floor(gap/3)) {? ? ? ? for (var i = gap; i < len; i++) {? ? ? ? ? ? temp = arr[i];? ? ? ? ? ? for (var j = i-gap; j >= 0 && arr[j] > temp; j-=gap) {? ? ? ? ? ? ? ? arr[j+gap] = arr[j];? ? ? ? ? ? }? ? ? ? ? ? arr[j+gap] = temp;? ? ? ? }? ? }? ? return arr;}Python實例 def shellSort(arr):? ? import math? ? gap=1? ? while(gap < len(arr)/3):? ? ? ? gap = gap*3+1? ? while gap > 0:? ? ? ? for i in range(gap,len(arr)):? ? ? ? ? ? temp = arr[i]? ? ? ? ? ? j = i-gap? ? ? ? ? ? while j >=0 and arr[j] > temp:? ? ? ? ? ? ? ? arr[j+gap]=arr[j]? ? ? ? ? ? ? ? j-=gap? ? ? ? ? ? arr[j+gap] = temp? ? ? ? gap = math.floor(gap/3)? ? return arrGo實例 func shellSort(arr []int) []int {? ? ? ? length := len(arr)? ? ? ? gap := 1? ? ? ? for gap < length/3 {? ? ? ? ? ? ? ? gap = gap*3 + 1? ? ? ? }? ? ? ? for gap > 0 {? ? ? ? ? ? ? ? for i := gap; i < length; i++ {? ? ? ? ? ? ? ? ? ? ? ? temp := arr[i]? ? ? ? ? ? ? ? ? ? ? ? j := i - gap? ? ? ? ? ? ? ? ? ? ? ? for j >= 0 && arr[j] > temp {? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? arr[j+gap] = arr[j]? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? j -= gap? ? ? ? ? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? ? ? ? ? arr[j+gap] = temp? ? ? ? ? ? ? ? }? ? ? ? ? ? ? ? gap = gap / 3? ? ? ? }? ? ? ? return arr}Java實例 public static void shellSort(int[] arr) {? ? int length = arr.length;? ? int temp;? ? for (int step = length / 2; step >= 1; step /= 2) {? ? ? ? for (int i = step; i < length; i++) {? ? ? ? ? ? temp = arr[i];? ? ? ? ? ? int j = i - step;? ? ? ? ? ? while (j >= 0 && arr[j] > temp) {? ? ? ? ? ? ? ? arr[j + step] = arr[j];? ? ? ? ? ? ? ? j -= step;? ? ? ? ? ? }? ? ? ? ? ? arr[j + step] = temp;? ? ? ? }? ? }}PHP實例 function shellSort($arr){? ? $len = count($arr);? ? $temp = 0;? ? $gap = 1;? ? while($gap < $len / 3) {? ? ? ? $gap = $gap * 3 + 1;? ? }? ? for ($gap; $gap > 0; $gap = floor($gap / 3)) {? ? ? ? for ($i = $gap; $i < $len; $i++) {? ? ? ? ? ? $temp = $arr[$i];? ? ? ? ? ? for ($j = $i - $gap; $j >= 0 && $arr[$j] > $temp; $j -= $gap) {? ? ? ? ? ? ? ? $arr[$j+$gap] = $arr[$j];? ? ? ? ? ? }? ? ? ? ? ? $arr[$j+$gap] = $temp;? ? ? ? }? ? }? ? return $arr;}C實例 void shell_sort(int arr[], int len) {? ? ? ? int gap, i, j;? ? ? ? int temp;? ? ? ? for (gap = len >> 1; gap > 0; gap >>= 1)? ? ? ? ? ? ? ? for (i = gap; i < len; i++) {? ? ? ? ? ? ? ? ? ? ? ? temp = arr[i];? ? ? ? ? ? ? ? ? ? ? ? for (j = i - gap; j >= 0 && arr[j] > temp; j -= gap)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? arr[j + gap] = arr[j];? ? ? ? ? ? ? ? ? ? ? ? arr[j + gap] = temp;? ? ? ? ? ? ? ? }}C++實例 templatevoid shell_sort(T array[], int length) {? ? int h = 1;? ? while (h < length / 3) {? ? ? ? h = 3 * h + 1;? ? }? ? while (h >= 1) {? ? ? ? for (int i = h; i < length; i++) {? ? ? ? ? ? for (int j = i; j >= h && array[j] < array[j - h]; j -= h) {? ? ? ? ? ? ? ? std::swap(array[j], array[j - h]);? ? ? ? ? ? }? ? ? ? }? ? ? ? h = h / 3;? ? }}

    參考地址:

    https://github.com/hustcc/JS-Sorting-Algorithm/blob/master/4.shellSort.md

    https://zh.wikipedia.org/wiki/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F

    以下是熱心網友對希爾排序算法的補充,僅供參考:

    熱心網友提供的補充1:

    我看這個沒把 C# 版本寫出來,我寫了一下,下面是 C# 版本:

    static void ShellSort(int[] arr)
    {
        int gap = 1;
    
        while (gap < arr.Length)
        {
            gap = gap * 3 + 1;
        }
    
        while (gap > 0)
        {
            for (int i = gap; i < arr.Length; i++)
            {
                int tmp = arr[i];
                int j = i - gap;
                while (j >= 0 && arr[j] > tmp)
                {
                    arr[j + gap] = arr[j];
                    j -= gap;
                }
                arr[j + gap] = tmp;
            }
            gap /= 3;
        }
    }
    以上為希爾排序算法詳細介紹,插入排序、希爾排序、選擇排序、冒泡排序、歸并排序、快速排序、堆排序、基數排序等排序算法各有優缺點,用一張圖概括:

    關于時間復雜度

    平方階 (O(n2)) 排序 各類簡單排序:直接插入、直接選擇和冒泡排序。

    線性對數階 (O(nlog2n)) 排序 快速排序、堆排序和歸并排序;

    O(n1+§)) 排序,§ 是介于 0 和 1 之間的常數。 希爾排序

    線性階 (O(n)) 排序 基數排序,此外還有桶、箱排序。

    關于穩定性

    穩定的排序算法:冒泡排序、插入排序、歸并排序和基數排序。

    不是穩定的排序算法:選擇排序、快速排序、希爾排序、堆排序。

    名詞解釋:

    n:數據規模

    k:"桶"的個數

    In-place:占用常數內存,不占用額外內存

    Out-place:占用額外內存

    穩定性:排序后 2 個相等鍵值的順序和排序之前它們的順序相同

    文檔

    希爾排序算法c語言代碼

    希爾排序,也稱遞減增量排序算法,是插入排序的一種更高效的改進版本。但希爾排序是非穩定排序算法。
    推薦度:
    為你推薦
    資訊專欄
    熱門視頻
    相關推薦
    選擇排序算法的基本思想 簡述冒泡排序的過程 歸并排序遞歸算法具體解析 希爾排序算法流程圖 選擇排序法的規則 冒泡排序的口訣 歸并排序算法代碼 希爾排序算法圖解 簡單選擇排序例題 java實現冒泡排序 歸并排序算法思想 希爾排序怎么取增量 選擇排序法代碼 冒泡排序算法復雜度 合并排序和歸并排序 數據結構希爾排序例子 c語言選擇法排序講解 java冒泡排序算法代碼 歸并排序算法偽代碼 希爾排序圖解 歸并排序算法復雜度 冒泡排序算法為什么問題 選擇排序詳解 希爾排序代碼 歸并排序又叫什么排序 冒泡排序法代碼 簡單選擇排序c語言 希爾排序原理 歸并排序 java數組冒泡排序 直接選擇排序舉例子 數據結構希爾排序 歸并排序原理 冒泡排序流程圖表示 選擇排序法流程圖 希爾排序代碼怎么解釋 java歸并排序 冒泡排序算法步驟 簡單選擇排序算法圖解 希爾排序例子
    Top 香港三级精品三级在线专区| 9久9久热精品视频在线观看| 久久精品亚洲综合| 国产自产拍精品视频免费看| 久久国产精品一国产精品金尊 | 最新日韩精品中文字幕| 亚洲日韩精品无码专区网址| 国产亚洲美女精品久久久久| 国产精品久久久久无码av| 国产精品久久久久久久午夜片| 精品久久久久久成人AV| 久久国产精品-久久精品| 四虎8848精品永久在线观看| 国产精品亚洲片夜色在线| 亚洲欧洲成人精品香蕉网| 天天视频国产精品| 久久精品女人天堂AV免费观看| 亚洲精品免费视频| 日韩精品无码免费专区午夜| 精品在线一区二区三区| 国产精品自在在线午夜蜜芽tv在线| 国产精品国产三级国产av品爱网| 大伊香蕉精品视频在线导航| 国产精品民宅偷窥盗摄| 亚洲欧洲久久精品| 亚洲∧v久久久无码精品| 久久国产成人亚洲精品影院| 国产精品午夜爆乳美女| 亚洲av永久中文无码精品综合| 99国产精品热久久久久久夜夜嗨| 亚洲精品成人片在线播放 | 日韩精品无码一区二区中文字幕 | 久久久久99这里有精品10| 99热这里只有精品免费播放| 国精无码欧精品亚洲一区| 国产精品综合专区中文字幕免费播放| 久久精品国产导航| 久久亚洲精品无码网站| 2020国产精品亚洲综合网| 久久亚洲精品无码aⅴ大香 | 久久永久免费人妻精品|