Shell Sort--Sorting techniques
-
Upload
shrekkie29 -
Category
Documents
-
view
234 -
download
0
Transcript of Shell Sort--Sorting techniques
-
7/30/2019 Shell Sort--Sorting techniques
1/14
Shell Sort
CSET 3150
-
7/30/2019 Shell Sort--Sorting techniques
2/14
Sorting and Searching
Shell Sortmore efficient than Bubble, Insertion and
Selection sortscompares distant items first and works waydown to nearby items
interval is called the gap
in a simple implementation, gap begins at one-half the length of the list and is successivelyhalved until each item is compared withneighbor
-
7/30/2019 Shell Sort--Sorting techniques
3/14
Shell Sort
A variation of the insertion sortBut faster than O(n2)
Done by sorting subarrays of equallyspaced indices
Instead of moving to an adjacent locationan element moves several locations away
Results in an almost sorted array
This array sorted efficiently with ordinaryinsertion sort
-
7/30/2019 Shell Sort--Sorting techniques
4/14
Shell SortGiven an array of n items
Start with the first item, and then take every n/2items to form a sub- arrayTake the second item and every n/2 item fromthere to form your second sub-array
Continue to form sub-arrays until every item isincluded in a sub-arrayWhen you have all of your sub-arrays sort them byan insertion sortThen 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)/2items and then process as before
Continue until you select 1 item
-
7/30/2019 Shell Sort--Sorting techniques
5/14
Shell Sort
An array and the subarrays formed by grouping
elements whose indices are 6 apart.
-
7/30/2019 Shell Sort--Sorting techniques
6/14
Shell Sort
The subarrays of the previous figure after they are
sorted, and the array that contains them.
-
7/30/2019 Shell Sort--Sorting techniques
7/14
Shell Sort
The subarrays of the array in previous figure formed by
grouping elements whose indices are 3 apart
-
7/30/2019 Shell Sort--Sorting techniques
8/14
Shell Sort
The subarrays of previous figure after they are sorted,
and the array that contains them.
-
7/30/2019 Shell Sort--Sorting techniques
9/14
Another Shell Sort ExampleConsider sorting the following list byShell sort:
45
23
4
9
6
7
91
8
12
24
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
4
6
7
8
9
12
23
24
45
91
GAP=3 GAP=2 GAP=1
-
7/30/2019 Shell Sort--Sorting techniques
10/14
Choosing the gap size
The idea of the decreasing gap size is that the listbecomes more and more sorted each time the gapsize is reduced, therefore you dont (for example)
want to have a gap size of 4 followed by a gap size of2 because youll be sorting half the numbers a secondtime.
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 oddnumbers 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 gap=3).
-
7/30/2019 Shell Sort--Sorting techniques
11/14
Efficiency of Shell Sort
Efficiency is O(n2) for worst case
Ifn
is a power of 2Average-case behavior is O(n1.5)
Any time the variable space is even,add 1
This also results in O(n1.5)
-
7/30/2019 Shell Sort--Sorting techniques
12/14
Comparing the Algorithms
Best Average WorstCase 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
-
7/30/2019 Shell Sort--Sorting techniques
13/14
Comparing O(N2), O(N1.25) & O(N)
O(N) O(N^1.25) O(N^2)
1 1 1
2 2.378414 4
3 3.948222 9
4 5.656854 16
5 7.476744 25
6 9.390507 36
7 11.38604 49
8 13.45434 64
9 15.58846 81
10 17.78279 100
11 20.03276 121
12 22.33452 144
13 24.68478 169
14 27.08071 196
15 29.51985 225
16 32 256
17 34.51923 289
18 37.07581 324
19 39.66815 361
20 42.29485 400
21 44.9546 441
22 47.64621 484
23 50.36859 529
24 53.12073 576
25 55.9017 625
0
100
200
300
400
500
600
700
1 3 5 7 911
13
15
17
19
21
23
25
Size of input (N)
Time
(T)
O(N)
O(N^1.25)
O(N^2)
-
7/30/2019 Shell Sort--Sorting techniques
14/14
The family of sorting methods
Main sorting themes
Comparison-basedsorting
Transpositionsorting
BubbleSort
Insert andkeep sorted
Insertionsort
Treesort
Divide andconquer
QuickSort MergeSort
ProxmapSort
RadixSort
ShellSort
Diminishingincrement
sorting
Address--basedsorting
Selectionsort
Heapsort
Priority queuesorting