3. It is a highly efficient sorting algorithm and is based
on insertion sort algorithm.
Shell Sort
This algorithm avoids large shifts as in case of
insertion sort, if the smaller value is to the far right
and has to be moved to the far left.
4. Uses insertion sort on a widely spread elements,
sort them and then sorts the less widely spaced
elements. This spacing is termed as interval.
Shell Sort
This interval is calculated based on Knuth's formula
(h = h * 3 +1) where − h is interval with initial
value 1
5. This algorithm is quite efficient for medium-sized
data sets as its average and worst case complexity
are of Ο(n), where n is the number of items.
Shell Sort
6. THEOREM:
Any sorting algorithm that only swaps adjacent
elements has average time no faster than
(O(n^{2})).
The Shell sort (named for its inventor, Donald Shell)
is an early and interesting attempt to do this.
Shell Sort
7. Insertion sort is fastest when;
a) The array is nearly sorted.
b) The array contains only small number of
data items
Shell Sort
Makes use of intrinsic strength of Insertion sort.
THEOREM:
8. Shell Sort
Shell sort works well because :
a) It always deals with a small number of
elements.
b) Elements are moved a long way through
array with each swap this leaves it more
nearly sorted.
THEOREM:
9. Shell Sort Best Case:
Shell Sort
• The best case in the shell sort is when the
array is already sorted in the right order.
• The number of comparisons is less.
10. Shell Sort Worst Case:
Shell Sort
• The running time of Shell sort depends on the
choice of increment sequence.
• Pairs of increments are not necessarily relatively
prime and smaller increments can have little
effect.
13. Shell Sort
Interval Sequence:
The sequence h1,h2,h3….ht is a sequence of
increasing integer values which will be used as a
sequence (from right to left) of interval values.
14. • Any sequence will work as long as it is increasing
and h1=1.
Shell Sort
• For any interval value, we all have A[i] <=A[i + h].
• An array A for which this is true is hk sorted.
• An array which is hk and is then hk-1 sorted remains
as hk sorted.
19. Selection Sort
It sorts an array by repeatedly finding the minimum
element (considering ascending order) from
unsorted part and putting it at the beginning.
The algorithm maintains two sub-arrays in a given
array.
1) The sub-array which is already sorted.
2) Remaining sub-array which is unsorted.
20. Selection Sort
It is conceptually the simplest sorting algorithm.
It is unstable sort and at the same time it can be
also stable.
It is unstable sort and at the same time it can be
also stable.
21. Selection Sort
Idea of Selection Sort: Largest-to-Smallest
• The first element of the sorted array will be the
one with the largest value.
• Second will be the largest element of the rest of
the array.
• Third will be the largest element of the new rest of
the array (initial array without the two already
sorted elements).
22. Selection Sort
HOW DOES SELECTION SORT WORKS?
1ST:
Find the index of the largest element and exchange
the position with the element at the last index.
23. 2nd:
Decrement the last index
When the array ends like this, the sorting stops.
Selection Sort
24. Selection Sort
Three procedures inside the selection sort:
1. Comparison, the times of comparison in a n value
array is (n(n-1))/2.
2. Value Assignment, the times of value assignment
is between 0 to 3(n-1).
3. Exchanging, the times need is between 0 to (n-1).