希尔排序(Shell Sort)
C/C++代码:
void ShellSort( int r[], int n )
{
int temp, i, j;
for ( int d = n / 2; d > 0; d = d / 2)
{
for ( i = d; i < n; i++ )
{
temp = r[i];
for( j = i - d; j >= 0 && temp < r[j]; j = j - d)
{
r[j + d] = r[j];
}
r[j + d] = temp;
}
}
}
void Print( int r[], int n )
{
for ( int i = 0; i < n; i++ )
{
cout << r[i] << " ";
}
cout << endl;
}
int main()
{
int r[] = { 96, 61, 18, 26, 55, 42, 4, 72, 83 };
int n = sizeof( r ) / sizeof( int );
Print( r, n );
ShellSort( r, n );
Print( r, n );
return 0;
}