shell sort
// ShellSort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
void ShellSort(int* a, int iLen);
void PrintArray(int a[], int iLen);
int _tmain(int argc, _TCHAR* argv[])
{
int arrayToSort[] = {20, 7, 3, 4, 25, 15, 29, 12, 4, 1};
int n = sizeof(arrayToSort)/sizeof(int);
PrintArray(arrayToSort, n);
ShellSort(arrayToSort, n);
PrintArray(arrayToSort, n);
return 0;
}
void ShellSort(int* a, int iLen)
{
if(a==NULL)
return;
if(iLen<=0)
return;
for(int increament=iLen/2;increament>=1;increament--)
{
for(int startIndex = 0; startIndex<increament;startIndex++)
{
int directSortIndex = startIndex + increament;
while(directSortIndex<iLen)
{
int temp = a[directSortIndex];
int innerIndex = directSortIndex - increament;
while(innerIndex>=0&&temp<a[innerIndex])
{
a[innerIndex+increament]=a[innerIndex];
innerIndex-=increament;
}
a[innerIndex+increament] = temp;
directSortIndex += increament;
}
}
}
}
void PrintArray(int a[], int iLen)
{
for(int i=1; i< iLen; i++)
{
std::cout<<a[i]<<' ';
}
std::cout<<std::endl;
}