Friday, 28 February 2014

Shell Sort

Shell Sort
Shell sort, also known as Shell sort or Shell's method, is an in-place comparison sort. It generalizes an exchanging sort, such as insertion or bubble sort, by starting the comparison and exchange of elements with elements that are far apart before finishing with neighbouring elements. Starting with far apart elements can move some out-of-place elements into position faster than a simple nearest neighbour exchange. Donald Shell published the first version of this sort in 1959.[1][2] The running time of Shell sort is heavily dependent on the gap sequence it uses. For many practical variants, determining their time complexity remains an open problem.
Class
Data structure
depends on gap sequence
depends on gap sequence
depends on gap sequence
О(n) total, O(1) auxiliary

Example
Algorithm
# Sort an array a[0...n-1].
gaps = [701, 301, 132, 57, 23, 10, 4, 1]

foreach (gap in gaps)
{
    # Do an insertion sort for each gap size.
    for (i = gap; i < n; i += 1)
    {
        temp = a[i]
        for (j = i; j >= gap and a[j - gap] > temp; j -= gap)
        {
            a[j] = a[j - gap]
        }
        a[j] = temp
    }


}

No comments:

Post a Comment