zoukankan      html  css  js  c++  java
  • 希尔排序记录--最好写的排序

    N 元素个数

    第一个for循环控制的是间隔

    剩下的两个for循环用于直接插入排序

    for (gap = N / 2; gap > 0; gap /= 2)
            for (i = gap; i < N; i++)
                for (j = i - gap; j >= 0 && A[j] > A[j + gap]; j -= gap)
                {
                    temp=A[j];
                    A[j]=A[j+gap];
                    A[j+gap]=temp;
                }

    完整程序

    #include <stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #define MAXN 10
    typedef int ElementType;

    void shellsort( ElementType A[], int N );
    int main ()
    {
        ElementType A[MAXN];
        int N, i;

        scanf("%d", &N);
        for ( i=0; i<N; i++ )
            scanf("%d", &A[i]);
        shellsort(A, N);

        system("pause>nul");
        return 0;
    }


    void shellsort( ElementType A[], int N )
    {
        int i, j, gap;
        ElementType temp;
        for (gap = N / 2; gap > 0; gap /= 2)
            for (i = gap; i < N; i++)
                for (j = i - gap; j >= 0 && A[j] > A[j + gap]; j -= gap)
                {
                    temp=A[j];
                    A[j]=A[j+gap];
                    A[j+gap]=temp;
                }

        for ( i=0; i<N; i++ )
            printf("%d ", A[i]);
    }




  • 相关阅读:
    网络流练习
    Noip2018 游记
    4719: [Noip2016]天天爱跑步
    1875: [SDOI2009]HH去散步
    P2619 [国家集训队2]Tree I
    1493: [NOI2007]项链工厂
    P1710 地铁涨价
    P3694 邦邦的大合唱站队
    P1439 【模板】最长公共子序列
    P1132 数字生成游戏
  • 原文地址:https://www.cnblogs.com/lxzbky/p/12466322.html
Copyright © 2011-2022 走看看