Saturday, May 2, 2009

Shell sort algorithm

This is almost as fast as quicksort in most cases. Attempts to make this faster failed including a test and early out.

void ShellSort (int array[], int num)
{
int inc = num/2;
while (inc > 0)
{
for (int i=inc; i < num-1; i++)
{
int temp = array[i];
int j=i;
while (j>= inc && temp < array [j-inc])
{
array[j] = array[j-inc];
j -= inc;
}
array [j] = temp;
}
inc = inc/2.2; // note the subdivision value which could be 2, but 2.2 is better
}
}

No comments: