• 排序算法门外汉理解-Shell排序


    #include <stdio.h>


    /* 希尔排序

     

     基本思想:希尔排序又称为缩小增量排序,对简单插入排序的优化。

    (外部分组gap,组内部插入排序!

     特点:一种不稳定的排序

     */


    void ShellSort(int array[],int len){

        int i,j;

        int gap;// gap

        int temp;

        for (gap = len/2 ; gap >0 ; gap = gap/2){ //核心就是 array[j+gap] array[j]比較。直到gap1

            

            for (i = gap; i < len ; i++){          // i: gap ~ <len(i++)   4321...

                temp = array[i];

                for (j = i - gap; j >=0; j-=gap) {// j: i-gap ~ >=0(j=j-gap)

                    if (temp < array[j]) {

                        array[j+gap] = array[j];    // 假设待插入元素<前面序列,则后移元素

                    }

                    else

                        break;

                }

                array[j+gap] = temp;                // 空位填上待插入元素就可以

            }

        }

        


    }


    int main(int argc,constchar * argv[])

    {

        int i =0;

        int a[] = {5,4,9,8,7,6,0,1,3,2};

        int len =sizeof(a)/sizeof(a[0]);

        

        // 希尔排序

        ShellSort(a, len);

        

        for (i =0; i < len; i++) {

            printf("%d ",a[i]);

        }

        printf(" ");

        

        return0;

    }

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    hdu 1214 圆桌会议(规律)
    hdu 1280 前m大的数
    hdu 2114 Calculate S(n)
    hdu 1210 Eddy's 洗牌问题
    hdu 1423 Greatest Common Increasing Subsequence(最长公共递增子序列lcis)
    7.30前端之Html简介
    8.3前端之Html列表
    7.30前端之Html头部
    7.30前端之Html元素
    7.23Java之递归练习
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4623448.html
Copyright © 2020-2023  润新知