Title: Shell Sort
1Shell Sort
2Sorting and Searching
- Shell Sort
- more efficient than Bubble, Insertion and
Selection sorts - compares distant items first and works way down
to nearby items - interval is called the gap
- in a simple implementation, gap begins at
one-half the length of the list and is
successively halved until each item is compared
with neighbor
3Shell Sort
- A variation of the insertion sort
- But faster than O(n2)
- Done by sorting subarrays of equally spaced
indices - Instead of moving to an adjacent location an
element moves several locations away - Results in an almost sorted array
- This array sorted efficiently with ordinary
insertion sort
4Shell Sort
- Given an array of n items
- Start with the first item, and then take every
n/2 items to form a sub- array - Take the second item and every n/2 item from
there to form your second sub-array - Continue to form sub-arrays until every item is
included in a sub-array - When you have all of your sub-arrays sort them by
an insertion sort - Then take the first elements of the sorted
subarrays as the first elements in a new big
array the second elements as the next group
etc. - Form new subarrays by selecting every (n/2)/2
items and then process as before - Continue until you select 1 item
5Shell Sort
An array and the subarrays formed by grouping
elements whose indices are 6 apart.
6Shell Sort
The subarrays of the previous figure after they
are sorted, and the array that contains them.
7Shell Sort
The subarrays of the array in previous figure
formed by grouping elements whose indices are 3
apart
8Shell Sort
The subarrays of previous figure after they are
sorted, and the array that contains them.
9Another Shell Sort Example
- Consider sorting the following list by Shell sort
GAP3
GAP2
GAP1
9 24 45 91
6 8 23
4 7 12
9 6 4 24 8 7 45 23 12 91
4 8 9 12 45
6 7 23 24 91
4 6 8 7 9 23 12 24 45 91
45 23 4 9 6 7 91 8 12 24
4 6 7 8 9 12 23 24 45 91
10Choosing the gap size
- The idea of the decreasing gap size is that the
list becomes more and more sorted each time the
gap size is reduced, therefore you dont (for
example) want to have a gap size of 4 followed by
a gap size of 2 because youll be sorting half
the numbers a second time. - There is no formal proof of a good initial gap
size, but about a 10th the size of N is a
reasonable start. - Try to use prime numbers as your gap size, or odd
numbers if you can not readily get a list of
primes (though note gaps of 9, 7, 5, 3, 1 will
be doing less work when gap3).
11Efficiency of Shell Sort
- Efficiency is O(n2) for worst case
- If n is a power of 2
- Average-case behavior is O(n1.5)
- Any time the variable space is even, add 1
- This also results in O(n1.5)
12Comparing the Algorithms
Best Average Worst Case
Case Case Selection sort O(n2) O(n2)
O(n2) Insertion sort O(n) O(n2)
O(n2) Shell sort O(n) O(n1.5) O(n1.5)
The time efficiencies of three sorting algorithms
13Comparing O(N2), O(N1.25) O(N)
14The family of sorting methods
Main sorting themes
Address- -based sorting
Comparison-based sorting
Proxmap Sort
RadixSort
Transposition sorting
BubbleSort
Divide and conquer
Diminishing increment sorting
Priority queue sorting
Insert and keep sorted
MergeSort
QuickSort
Selection sort
Heap sort
Insertion sort
Tree sort
ShellSort