// shellSort.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
using namespace std;

/*
    算法思想:
    1,初始化增量大小,increment = 0;
    2,以增量大小变化为循环 依次减小增量 直到增量为1 停止循环
    3,在循环体内 设置增量变化公式,从start+增量的位置开始  依次比较大小 若前面的大于后面的,
       则:①首先把当前位置保存下来,然后用前面相隔相同增量的较小的覆盖,直到完毕后,在把最前面的赋值为保存的值。
*/
void ShellSort(int* a,int len)
{
    int increment = len;
    int i,j;
    int temp;
    do
    {
        increment = increment/3+1;
        for(i = increment; i<len; ++i)
        {
            if(a[i] < a[i-increment])
            {
                temp = a[i];
                for(j =i-increment; j >= 0 && a[j] >= temp; j = j - increment)
                {
                    a[j+increment] = a[j];
                }
                a[j+increment] = temp;
            }
        
        }

    }
    while(increment > 1);
}
void OutPut(int *a, int len)
{
    for(int i = 0; i <len; ++i)
    {
      cout<<a[i]<<" ";
    }
    cout<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
    int a[] = {4,5,6,7,3,2,1,5,8,9};
    ShellSort(a,10);
    OutPut(a,10);
    return 0;
}

 

posted on 2012-09-12 17:47  TianMG  阅读(159)  评论(0编辑  收藏  举报