Shell Sort
Shell Sort
Cxample The data in the table needs to be sorted into ascending order. For simplicity, we have chosen the
sequence (3, 2, 1) for our n. Elements in the table that have a white background are being
ignored in that step, Elements with a shaded background are the ones we are interested in. The
top line of each table shows the data before we performed that step, the bottom shows the data
afterwards.
ANALYSIS OF SIMPLE SORTING ALGORITHMS 89
In this example, we will assume there is a selection sort being used to do
the actual sorting.
8 4 5 7 8 3 2
5 4 1 8 7 6 3 2
5 4 1 7 6 3 2
5 3 1 8 4 6 7 2
5 1 8 6 9 7 2
3 1 8 4 2 9 7 6
As well as 1, 6, and 2.
Now that all of the elements have been sorted once with n=3, we repeat the process with the
next value of n i.e, 2. If you look carefully, there is a general congregation of large numbers at the
right hand side, and the smaller numbers are at the left. There are still quite a few misplaced
numbers (most notably 8, 2, 5 and 6), but it is better sorted than it was.
5 3 1 4 2 9 7 6
3 5 2 6 7
1 3 8 5 2 6 7 9
1 2 4 3 5 76 8
You can see nowthatthe datais almost completely sorted - after just .more
2 steps! Al
ANALYSIS
remains is to sort it again with n=1to fix up any elements that are still out of order.
we are just performing a normal sort, making sure every element in the dataset is in iits When n=lthat,
correct place.
You may wonder why don't we just skip to this step. Yes, doing that would work,
the selection and bubble sorts are fastest when the data is already sorted (or howevershell
close to). The
sort method orders the data in fewer steps than would be required for either of the aboove
methods,
3 5 7 6 8 9
23456789
1 2 3 4 5 678 9
Allsorted !
Algorithm
Input :an array aof length nwith array elements numbered 0to n-1
1. inc + round (n/2)
2. while inc > 0
3 for i =inc to n -1
temp - a i]
j+i
while j >inc and a [j-inc]>temp
a]- aj -inc]
j+j-inc
}
ai] temp
}
4. inc 4- round(inc/2.2)
Shell sort improves on the efficiency of insertion sort by quickly shifting values to ther
destination. Average sort time is O(n) while worst-case time is On15) In this method, instead
of sorting the entire array at once, the array is first divided into smaller segments e.g., ksegnent,
Then these segments are sorted separately using the insertion sort. The shell sort is also ca
diminishing increment sort because the value of kcontinuously decreases, where kis preterabiy*
prime number.
Shell sort is a sorting algorithm which requires O(n*) comparisons and exchangesin
the worst case.
ANALYSIS OF SIMPLE SORTING ALGORITHMS 91
4 Shell sort is a generalization of insertion sort, with
two important observations :
Insertion sort is efficient if the input is "almost sorted".
Insertion sort is inefficient, on average, because it moves values just one
at a time. position
4 Shellsort improves insertion sort by
positions.
comparing elements separated by a gap of several
a This lets an element take "bigger steps"
toward its expected position. Multiple passes
over the data are taken with smaller and smaller gap sizes.
The last step of Shell sort is a plain insertion sort, but by then,
the array of data is
guaranteed to be almost sorted.
Shell sort is built upon the concept of H-sorting. Alist is said to be
any position, every H-th item is in sorted position relative to the otherH-sorted when, starting at
items. That concept will
become clear as you work through an example. In the following example, we start by 3-sorting the
list from Figure. In other words, we will consider only every 3rd element and sort
relative to each other.
those items
Example
45 36 75 20 05 90 80 65 30 50 10 75 85
45 36 75 20 05 80 65 30 50 75 85
20 05 30 45 I0 75 50 36 75 80 65 90 85
92 ALGORITHMS DESIGN AND ANALYSIO
Output of Pass 3
05 I0 20 30 36 45 50 65 75 75 80 85 90
Best Case
The best case in the shellsort is when the array is already sorted in the right order. The
number of comparisons is less.
Worst case
< The running time of Shell sort depends on the choice of increment sequence.
4 The problem with Shell's increments is that pairs of increments are not necessarily
relatively prime and smaller increments can have little effect.
160
140
120
Seconds
100
80
60
40
20
0
10 100 1000 10000 25000 50000 75000 100000