希尔排序

来源:互联网 发布:scratch编程教程 pdf 编辑:程序博客网 时间:2024/05/21 00:53











#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
using namespace std;
void shellsort(int a[],int n)
{
        for(int gap=n/2;gap>0;gap/=2)
        {
                for(int i=gap;i<n;i++)
                {
                        for (int j=i-gap;j>=0&&a[j]>a[j+gap];j-=gap)
                        {
                                int temp=a[j];
                                a[j]=a[j+gap];
                                a[j+gap]=temp;
                        }
                }
        }
}
int main()
{
        int n;
        int a[1000];
        while(scanf("%d",&n)!=EOF)
        {
                for(int i=0;i<n;i++)
                        scanf("%d",&a[i]);
                shellsort(a,n);
                for(int i=0;i<n;i++)
                        printf("%d ",a[i]);
                printf("\n");
        }
        return 0;
}