sort.c
//shell sort
#include <stdio.h>
static int iAddArray[] = {5, 3, 2, 1};
static int iArrLength;
void ShellInsert(int iList[], int addSize)//using insert directly sort
{
int i, j;
int iSaveInsNum;//used for saving number of insertion
for(i = addSize; i < iArrLength; i++)
{
if(iList[i] < iList[i - addSize])
{
iSaveInsNum = iList[i];
for(j = i - addSize; j >= 0 &&(iSaveInsNum < iList[j]); j -= addSize)//compare starting from the end
{
iList[j + addSize] = iList[j];//backword for finding insertion place
}
iList[j + addSize] = iSaveInsNum;
}
}
}
void ShellSort(int iList[], int iLen)
{
int i;
int iTimes = sizeof(iAddArray) / sizeof(iAddArray[0]);
iArrLength = iLen;
for(i = 0; i < iTimes; i++)
{
ShellInsert(iList, iAddArray[i]);
}
}
main.c
//shell sort
#include <stdio.h>
int iList[] = {34, 22, 11, 68, 234, 21};
int main(void)
{
void ShellSort(int iList[], int iLen);
int iLen;
int i;
iLen = sizeof(iList) / sizeof(iList[0]);
ShellSort(iList, iLen);
for(i = 0; i < iLen; i++)
{
printf(i == iLen - 1 ? "%d\n" : "%d,", iList[i]);
}
return 0;
}