Monday, 30 April 2012

Snippet of Shell Sort in C++


void ShellSort(unsigned long item[], long lowerbound, long upperbound)
{
    long n, i, j, t, h;
    n = upperbound - lowerbound + 1;
    h = 1;
    if (n < 14)
        h = 1;
    else
    {
        while (h < n)
        h = 3*h + 1;
        h /= 3;
        h /= 3;

    }
    while (h > 0)
    {
        for (i = lowerbound + h; i <= upperbound; i++)
        {
            t = item[i];
            for (j = i-h; j >= lowerbound && (item[j] > t); j -= h)
                item[j+h] = item[j];
            item[j+h] = t;
        }
        h /= 3;
    }
}

No comments:

Post a Comment