希尔排序

 

#include <iostream>
#include <stdio.h>

using namespace std;

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

void main()
{
    int nLen = sizeof(g_szArray) / sizeof(g_szArray[0]);
    int nStep = nLen / 2;
    while (nStep >= 1)
    {
        for (int i = 0; i < nLen; i++)
        {
            if (i + nStep > nLen - 1)
            {
                break;
            }

            if (g_szArray[i] > g_szArray[i + nStep])
            {
                g_szArray[i] = g_szArray[i] + g_szArray[i + nStep];
                g_szArray[i + nStep] = g_szArray[i] - g_szArray[i + nStep];
                g_szArray[i] = g_szArray[i] - g_szArray[i + nStep];
            }
        }

        nStep = nStep / 2;
    }

    for (int i = 0; i < nLen; i++)
    {
        printf("%d ", g_szArray[i]);
    }
    printf("\n");
    system("pause");
}

 

posted @ 2019-11-24 10:13  _No.47  阅读(220)  评论(0编辑  收藏  举报