#include "stdafx.h"
#include <iostream>
using namespace std;
//希爾算法(升序)
void InsertSort(int a[], int n)
{
int h,i,j,temp;
for (h=n/2; h>0; h=h/2)
{
for (i=h; i<n; i++)
{
temp = a[i];
for (j=i-h; j>=0 && temp < a[j]; j-=h)
{
a[j+h] = a[j];
}
a[j+h] = temp;
}
}
}
//輸出數組 www.2cto.com
void Print_Arry(int a[], int n)
{
for (int i=0; i<n; i++)
{
cout<< a[i]<<" ";
}
cout<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
int a[9] = {7,3,5,8,9,1,2,4,6};
InsertSort(a, 9);
Print_Arry(a, 9);
return 0;
}
摘自 學無止境