shell sort
DESCRIPTION
SORTING TECHNIQUETRANSCRIPT
![Page 1: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/1.jpg)
Shell Sort
Chapter-4
![Page 2: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/2.jpg)
Bucket sort
![Page 3: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/3.jpg)
Bucket sort example
![Page 4: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/4.jpg)
Bucket sort Example 2
![Page 5: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/5.jpg)
Shell Sort
• Shell sort works by comparing elements that are distant rather than adjacent elements in an array.
• Shell sort improves on the efficiency of insertion sort by quickly shifting values to their destination.
• Shell sort is also known as diminishing increment sort.
• The distance between comparisons decreases as the sorting algorithm runs until the last phase in which adjacent elements are compared
![Page 6: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/6.jpg)
Shell Sort
• 18 32 12 5 38 33 16 2floor(8/2) floor(4) = 418 32 12 5 38 33 16 2
Resulting numbers after increment 4 pass:
18 32 12 2 38 33 16 5
![Page 7: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/7.jpg)
Shell Sort
floor(4/2) floor(2) = 2
increment 2:
• 18 32
• 12 2
• 38 33
• 16 5
• Again sort the column….
![Page 8: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/8.jpg)
Shell Sort
• The result is,
• 12 2
• 16 5
• 18 32
• 38 33
![Page 9: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/9.jpg)
Shell Sort
floor(2/2) floor(1) = 1
• 12 2 16 5 18 32 38 33
• The last increment or phase of Shell sort is basically an Insertion Sort algorithm.
2 5 12 16 18 32 33 38
![Page 10: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/10.jpg)
Shell Sort
• Arrange the list into a table and sort the columns (using an insertion sort).
• Repeat this process, each time with smaller number of longer columns.
• At the end, the table has only one column.
• For example, consider a list of numbers like • [ 13 14 94 33 82 25 59 94 65 23 45 27 73 25 39 10 ]. • If we started with a step-size of 5, we could visualize
this as breaking the list of numbers into a table with 5 columns.
![Page 11: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/11.jpg)
Example
• This would look like this:
• 13 14 94 33 82
• 25 59 94 65 23
• 45 27 73 25 39
• 10
![Page 12: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/12.jpg)
Example Contd…
• We then sort each column, which gives us
• 10 14 73 25 23
• 13 27 94 33 39
• 25 59 94 65 82
• 45
![Page 13: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/13.jpg)
Example Contd…
• When read back as a single list of numbers, we get [ 10 14 73 25 23 13 27 94 33 39 25 59 94 65 82 45 ].
• Here, the 10 which was all the way at the end, has moved all the way to the beginning.
• This list is then again sorted using a 3-gap sort as shown below.
![Page 14: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/14.jpg)
Example Contd…
• 10 14 73
• 25 23 13
• 27 94 33
• 39 25 59
• 94 65 82
• 45
• Again sort each column
![Page 15: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/15.jpg)
Example Contd…
• Which gives us • 10 14 13 • 25 23 33 • 27 25 59 • 39 65 73• 45 94 82• 94 • Now all that remains to be done is a 1-gap sort
(simple insertion sort).
![Page 16: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/16.jpg)
INTERPOLATION SEARCH:
• It is a modified algorithm of Binary search reducing the complexity.
• Best/Average case complexity is O(log log n) ) which will occur when elements of the array are fairly uniformly distributed over their interval eg. . [3,5,8,10,13,15]
• Worst case complexity is O(n) which will occur when the elements are not fairly uniformly distributed over their interval eg. [3,5,8,10,13,100]
![Page 17: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/17.jpg)
Example:
• Search for the key 20 in the following array using Interpolation Search
• int[] a = {0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30}
![Page 18: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/18.jpg)
Interpolation Search:
• middle = low + ( high - low )*( (key – a[low]) / ( a[high] – a[low] ))
• middle = 0 + ceil(15 – 0)* ( (20 – 0) / (30 – 0) ) = 0 +10 =10
• a[middle] = 20 which is equal to key.
• So key found at index 10.
![Page 19: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/19.jpg)
![Page 20: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/20.jpg)
![Page 21: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/21.jpg)
![Page 22: SHELL SORT](https://reader033.vdocuments.us/reader033/viewer/2022051012/5453a632b1af9f9b7c8b4661/html5/thumbnails/22.jpg)