希尔排序
由插入排序改编而来,以间隔g进行插入排序
#include<iostream>
using namespace std;
void insertSort(int A[], int N, int g)
{
for (int i = g; i < N; i++)
{
int v = A[i];
int j = i - g;
while (A[j] > v && j >= 0)
{
A[j + g] = A[j];
j -= g;
}
A[j + g] = v;
}
}
void shellSort(int A[], int N)
{
int G[] = { 5,3,1 };
for (int i = 0; i < 3; i++)
{
insertSort(A, N, G[i]);
}
}
int main()
{
int A[] = { 5,1,4,3,2,5,6,3,2,2,22,99,67,43 };
shellSort(A, 14);
for (int i = 0; i < 14; i++)
{
cout << A[i] << " ";
}
}