3. SORT
TYPES OF SORT
HISTORY AND USAGE
ALGORITHM
PRINCIPLES OF WORKING
COMPLEXITY
ADVANTAGES AND DRAWBACKS
APPLICATIONS
CONCLUSION
OUTLINE
4. SORT
BEFORE WE START WITH SHELL SORT, LET US FIRST KNOW ABOUT SORTING
What is sorting?
Sorting is the process of arranging elements either in ascending (or) descending order. The term
sorting came into existence when humans realized the importance of searching quickly.
Why do we use sorting?
A sorting algorithm will put items in a list into an order, such as alphabetical or numerical order. For
example, a list of customer names could be sorted into alphabetical order by surname, or a list of
people could be put into numerical order by age.
6. THE SORT WE CHOSE TO EXPLAIN IS THE SHELL SORT:
BASICALLY IF WE ASK ANYONE OVER HERE ABOUT THE
TYPES OF SORTS, THEY SAY THE TYPES WE PREVIOUSLY
LEARNT. BUT THE SHELL SORT IS THE TYPE WHERE WE
NEVER HEARD OR LEARNT IN OUR EARLIER STAGES OF
EDUCATION.
So what exactly is shell sort?
Shell sort is the generalization of the insertion sort. It first sorts
elements that are far apart from each other and then reduces the
gap between the elements to be sorted. Gap reduction is based on
the sequence used. It is basically faster than the other sorts. We
use this sort to fasten our process.
7. HISTORY AND THE BASICS OF SHELL SORT
The shell sort (also known as shell's method) is named after its inventor, Donald Shell, who
published the algorithm in 1959. This was invented at university of Cincinnati.
Shell sort is a sequence of interleaved insertion sorts based on an increment sequence. The
increment size is reduced after each pass until the increment size is 1.
With an increment size of 1, the sort is a basic insertion sort, but by this time the data is
guaranteed to be almost sorted, which is insertion sort's "best case".
Any sequence will sort the data as long as it ends in 1, but some sequences work better than
others.
8. WHY DO WE USE SHELL
SORT
The shell sort is used to sort the array by sorting the pair of elements far apart from each other
depending on it's gap/interval and reduces the gap between the elements to be sorted. It can transfer the
disordered elements into the correct place faster than any other sort.
Shell sort has improved the average time complexity of insertion sort. As similar to insertion sort, it is a
comparison-based and in-place sorting algorithm. Shell sort is efficient for medium-sized data sets.
This algorithm first sorts the elements that are far away from each other, then it subsequently reduces
the gap between them. This gap is called as interval. This interval can be calculated by using
the Knuth's formula given below -
HH = H * 3 + 1 (where, 'h' is the interval having initial value 1)
9. THE ALGORITHM & HOW DOES IT WORK?
Let's see the working of the shell sort algorithm. To understand the working of the shell sort algorithm, let’s take an
unsorted array. It will be easier to understand the shell sort via an example.
• Let the elements of array be –
23 29 15 19 31 7 9 5 2
We will use the original sequence of shell sort, i.e. N/2, n/4,....,1 as the intervals.
Step-1: Insert all the elements in array a[20]
Step-2: In 1st pass, Gap= ⌊N/2⌋ = ⌊9/2⌋ = ⌊4.5⌋ = 4
So the elements of the gap/interval of 4 are compared and swapped if they are out of order in the first gap in both
forward & backward directions.
Here, 0th element and the 4th element are compared.
23 29 15 19 31 7 9 5 2
0 1 2 3 4 5 6 7 8
INDEX ->
ELEMENTS->
10. If the 0th element > 4th element then both are swapped.
If the 0th element == 4th element then no swapping is required.
The rearranged elements in an n/2 interval:
Array: [ 23 29 15 19 31 7 9 5 2 ]
Similarly, this procedure is repeated for all remaining elements & rearranges the elements in an n/2 interval, i.e
1st 5th
2nd 6th
3rd 7th
4th 0th
12. Step-3: In 2nd pass, gap = ⌊N/4⌋ = ⌊9/4⌋ = ⌊2.5⌋ = 2 [2 is the chosen interval]
Here, the elements are sorted & stored which are at the gap of 2.
Until J < = N – 1, run
2 7 9 5 23 29 15 19 31
2 5 9 7 23 29 15 19 31
2 5 9 7 15 29 23 19 31
2 5 9 7 15 19 23 29 31
Compare 0th element with 2nd element and go on….
0 1 2 3 4 5 6 7 8
2 7 9 5 23 29 15 19 31
In N/4 Interval
Result Of Pass-2
INDEX ->
ELEMENTS->
13. Step-4: In 3rd pass
Here, shell sort works same as insertion sort
Gap = ⌊N/8⌋ = ⌊9/8⌋ = ⌊1.11⌋ = 1
2 5 9 7 15 19 23 29 31
Step-5: Gap = ⌊n/16⌋ = 0
We can see the sorted array of elements.
2 5 9 7 15 19 23 29 31
15. CODE SNIPPET OF SHELL SORT:
int main()
{
int a[20],i,n;
printf("Enter number of
elements:");
scanf("%d", &n);
printf("Enter array elements:n");
for(i=0;i<n;++i)
scanf("%d",&a[i]);
sort(a, n);
printf("n Array after shell sort:n");
for(i=0;i<n;++i)
printf("%d ",a[i]);
return 0;
}
#include<stdio.h>
void sort(int a[],int n)
{
int gap, i, j, temp;
for(gap=n/2;gap>0;gap/=2)
{
for(i = gap; i < n; i+=1)
{
temp=a[i];
for(j=i; j>=gap&&a[j-gap]>temp;
j-=gap)a[j]=a[j-gap];a[j]=temp;
}
}
}
Output:
Enter number of elements:
5
Enter array elements: 5 6 7
2 9 12
Array after shell sort: 2 7 9
12 56
16.
17.
18. THE PRINCIPLES OF ITS WORKING:
In insertion sort, at a time, elements can be moved ahead by one position only. To
move an element to a far-away position, many movements are required that
increase the algorithm's execution time. But shell sort overcomes this drawback of
insertion sort. It allows the movement and swapping of far-away elements as well.
Note: if gap==1, the shell sort is same as insertion sort. If gap==0 , then we get the
sorted array.
The performance and efficiency of shell depends on the gap sequence. Better the
gap sequence the less time would be take to sort the array. Some of the optimal
sequences / methods that can be used in the shell sort algorithm to find the gap/
interval are following:
19. Out of that, we are going to take shell's
original sequence (N/2,N/4,N/8....1) as
intervals.
Gap = floor value (n/2) method (ex: floor of
0.0 to 0.9 is 0)
So in 1st pass gap1 = n/2
In 2nd pass gap2 = n/4
In 3rd pass gap3 = n/8 . .
……
Until we get gap == 1
20. SHELL SORT COMPLEXIETY:
Best Case Complexity - It occurs when there is no sorting required, i.e., the
array is already sorted. The best-case time complexity of Shell sort
is O(n*logn).
Average Case Complexity - It occurs when the array elements are in jumbled
order that is not properly ascending and not properly descending. The average
case time complexity of Shell sort is O(n*logn).
Worst Case Complexity - It occurs when the array elements are required to be
sorted in reverse order. That means suppose you have to sort the array in
ascending order, but elements are in descending order. The worst-case time
complexity of Shell sort is O(n2).
The space complexity of Shell sort is O(1).
21. INSERTION AND SHELL SORT ARE IDENTICAL?
In insertion sort , the element moves one position ahead to insert an element
at its correct position, whereas the shell sort exchanges the far elements.
The difference follows: while insertion sort works only with one sequence (i.e. from first element) and expands it;
shell sort has a diminishing increment where there is a gap between the compared elements (initially n/2). Hence
there are n/2 sequences to be sorted using insertion sort. In each step, the increment is shrunk and the number of
sequences is reduced. In the last step, there is no gap and the algorithm degenerates to simple insertion sort.
Because of the diminishing increment, the large and small elements are moved rapidly to correct part of the array
and than in the last step sorted using insertion sort really fast. This leads to reduced time complexity o(n^(4/3)).
22. So a sorted list is 1-sorted. If the odd elements are sorted, and the even elements
are sorted, then the list is 2-sorted. An insertion sort basically skips to the last step
of shell sort. Hence shell sort is considered as a generalization of insertion sort.
WHAT IS THE RELATIONSHIP B/W INSERTION SORT AND SHELL SORT?
The shell sort, sometimes called the “diminishing increment
sort, improves on the insertion sort by breaking the original list
into a number of smaller sub-lists, each of which is sorted using
an insertion sort. The unique way that these sub-lists are chosen is the key to the shell sort.
23. WHY IS SHELL SORT BETTER THAN INSERTION SORT?
Shell sort allows swapping of indexes that are far apart, n insertion sort,
we move elements only one position ahead. When an element has to be
moved far ahead, many movements are involved. The idea of shell sort is to allow exchange of far items. In
shell sort, we make the array h-sorted for a large value of h
HOW IS SHELL SORT A VARIATION OF INSERTION SORT?
Shell sort is mainly a variation of insertion sort. In insertion sort, we move elements only one position ahead.
When an element has to be moved far ahead, many movements are involved.
24. ADVANTAGES:
Shell sort algorithm is only efficient for finite number of elements in an array.
With improved average time complexity, it is a very efficient algorithm for medium size arrays.
Shell sort algorithm is 5.32 x faster than bubble sort algorithm. Less number of swaps and comparisons are required
compared to insertion sort.
DISADVANTAGES:
It is a complex algorithm. It is not stable sorting algorithm.
Not that efficient as merge sort and quicksort.
Limited to use for small size arrays.
Shell sort algorithm is significantly slower than the merge sort, quick sort and heap sort algorithms.
25. APPLICATIONS:
It performs more operations and has higher cache miss ratio.
Since it can be implemented using little code and does not use the call stack, some
implementations of the osort function in the ‘C’ at embedded system.
Similarly, an implementation of shell sort is present in the Linux kernel used in the uclibc
library.
Shell sort can also serve as a sub-algorithm of introspective sort. This principle is employed
in the bzip2 compressor.
26. CONCLUSION:
Shell sort is one the most efficient sorting algorithm for large datasets. This
technique is based on insertion sort but it reduces the no. of swaps considerably. It
does not require extra memory which makes it an overall efficient algorithm for
sorting. It is used in C to sort the array by sorting pair of elements far apart from
each other and successively reducing gap between the elements to be stored.
Hence, we can say that it is not a stable sorting algorithm.