SlideShare uma empresa Scribd logo
1 de 17
CSCD 300 Data Structures
Donald Shell’s Sorting Algorithm
Originally developed by Bill Clark, modified
by Tom Capaul and Tim Rolfe

1
Shell Sort - Introduction
More properly, Shell’s Sort
Created in 1959 by Donald Shell
Link to a local copy of the article:
Donald Shell, “A High-Speed Sorting
Procedure”, Communications of the ACM
Vol 2, No. 7 (July 1959), 30-32
Originally Shell built his idea on top of
Bubble Sort (link to article flowchart),
but it has since been transported over to
Insertion Sort.
2
Shell Sort -General Description
Essentially a segmented insertion sort
Divides an array into several smaller noncontiguous segments
The distance between successive elements
in one segment is called a gap.
Each segment is sorted within itself using
insertion sort.
Then resegment into larger segments
(smaller gaps) and repeat sort.
Continue until only one segment (gap = 1) final sort finishes array sorting.
3
Shell Sort -Background
General Theory:
Makes use of the intrinsic strengths of Insertion
sort. Insertion sort is fastest when:
The array is nearly sorted.
The array contains only a small number of
data items.
Shell sort works well because:
It always deals with a small number of elements.
Elements are moved a long way through array
with each swap and this leaves it more nearly
sorted.
4
Shell Sort - example
Initial Segmenting Gap = 4
80

93

60

12

42

30

68

85

10

10

30

60

12

42

93

68

85

80

5
Shell Sort - example (2)
Resegmenting Gap = 2
10

30

60

12

42

93

68

85

80

10

12

42

30

60

85

68

93

80

6
Shell Sort - example (3)
Resegmenting Gap = 1
10

12

42

30

60

85

68

93

80

10

12

30

42

60

68

80

85

93

7
Gap Sequences for Shell Sort
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 gap values.
Any sequence will work as long as it is increasing
and h1 = 1.

For any gap value hk we have A[i] <= A[i + hk]
An array A for which this is true is hk sorted.
An array which is hk sorted and is then hk-1
sorted remains hk sorted.
8
Shell Sort - Ideal Gap Sequence
Although any increasing sequence will
work ( if h1 = 1):
Best results are obtained when all values in
the gap sequence are relatively prime
(sequence does not share any divisors).
Obtaining a relatively prime sequence is often
not practical in a program so practical
solutions try to approximate relatively prime
sequences.

9
Shell Sort - Practical Gap Sequences
Three possibilities presented:
1) Shell's suggestion - first gap is N/2 - successive
gaps are previous value divided by 2.
Odd gaps only - like Shell method except if division
produces an even number add 1.
better performance than 1) since all odd values
eliminates the factor 2.
2.2 method - like Odd gaps method (add 1 to even
division result) but use a divisor of 2.2 and
truncate.
best performance of all - most nearly a relatively
prime sequence.
10
Shell Sort - Added Gap Sequence
Donald Knuth, in his discussion of Shell’s
Sort, recommended another sequence of
gaps.
h0 = 1
hj+1 = hj * 3 + 1
Find the hj > n, then start with hj/3

11
Link to the Java program that generated the above data.
12
Shell Sort - Time Complexity
Time complexity: O(nr) with 1 < r < 2
This is better than O(n2) but generally
worse than O(n log2n).

13
Shellsort - Code
public static void
shellSort( Comparable[ ] theArray, int n ) {
// shellSort: sort first n items in array theArray
for( int gap = n / 2; gap > 0; gap = gap / 2 )
for( int i = gap; i < n; i++ ) {
Comparable tmp = theArray[ i ];
int j = i;
for( ; j >= gap && tmp.compareTo(theArray[ j - gap ]) < 0 ; j -= gap )
theArray[ j ] = theArray[ j - gap ];
theArray[ j ] = tmp;
}
}

14
ShellSort -Trace (gap = 4)
[0] [1] [2]
theArray 80
n: 9
gap: 4

93

60

[3] [4] [5] [6]

[7]

[8]

12

85

10

42

30

68

i:
j:

for( int gap = n / 2; gap > 0; gap = gap / 2 )
for( int i = gap; i < n; i++ ) {
Comparable tmp = theArray[ i ];
int j = i;
for( ; j >= gap && tmp.compareTo(theArray[ j - gap ]) < 0 ; j -= gap )
theArray[ j ] = theArray[ j - gap ];
theArray[ j ] = tmp;
}

15
ShellSort -Trace (gap = 2)
[0] [1] [2]
theArray

[3] [4] [5] [6]

[7]

[8]

10

12

85

80

n: 9
gap: 2

30

60

42

93

68

i:
j:

for( int gap = n / 2; gap > 0; gap = gap / 2 )
for( int i = gap; i < n; i++ ) {
Comparable tmp = theArray[ i ];
int j = i;
for( ; j >= gap && tmp.compareTo(theArray[ j - gap ]) < 0 ; j -= gap )
theArray[ j ] = theArray[ j - gap ];
theArray[ j ] = tmp;
}

16
ShellSort -Trace (gap = 1)
[0] [1] [2]
theArray

[3] [4] [5] [6]

[7]

[8]

10

30

93

80

n: 9
gap: 1

12

42

60

85

68

i:
j:

for( int gap = n / 2; gap > 0; gap = gap / 2 )
for( int i = gap; i < n; i++ ) {
Comparable tmp = theArray[ i ];
int j = i;
for( ; j >= gap && tmp.compareTo(theArray[ j - gap ]) < 0 ; j -= gap )
theArray[ j ] = theArray[ j - gap ];
theArray[ j ] = tmp;
}

17

Mais conteúdo relacionado

Mais procurados

Shell sort in Data Structure Using C
Shell sort in Data Structure Using CShell sort in Data Structure Using C
Shell sort in Data Structure Using C
Ashish Gaurkhede
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
MYER301
 

Mais procurados (20)

Polynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptxPolynomial reppresentation using Linkedlist-Application of LL.pptx
Polynomial reppresentation using Linkedlist-Application of LL.pptx
 
Shell sorting
Shell sortingShell sorting
Shell sorting
 
Circular linked list
Circular linked listCircular linked list
Circular linked list
 
Insertion Sorting
Insertion SortingInsertion Sorting
Insertion Sorting
 
Hash table
Hash tableHash table
Hash table
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
3.3 shell sort
3.3 shell sort3.3 shell sort
3.3 shell sort
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Shell sort in Data Structure Using C
Shell sort in Data Structure Using CShell sort in Data Structure Using C
Shell sort in Data Structure Using C
 
Sorting algorithms
Sorting algorithmsSorting algorithms
Sorting algorithms
 
Hash table
Hash tableHash table
Hash table
 
Different types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with AnimationDifferent types of Shoring Algorithms with Animation
Different types of Shoring Algorithms with Animation
 
Insertion sort
Insertion sort Insertion sort
Insertion sort
 
Selection sort
Selection sortSelection sort
Selection sort
 
Insertion sort
Insertion sortInsertion sort
Insertion sort
 
Dsa – data structure and algorithms searching
Dsa – data structure and algorithms   searchingDsa – data structure and algorithms   searching
Dsa – data structure and algorithms searching
 
Queues
QueuesQueues
Queues
 
Selection sort 1
Selection sort 1Selection sort 1
Selection sort 1
 
Hashing
HashingHashing
Hashing
 

Destaque

Destaque (20)

Shell sort slide
Shell sort slideShell sort slide
Shell sort slide
 
Shell sort
Shell sortShell sort
Shell sort
 
Shell sort
Shell sortShell sort
Shell sort
 
Merge sort
Merge sortMerge sort
Merge sort
 
Quick Sort
Quick SortQuick Sort
Quick Sort
 
Bucket sort
Bucket sortBucket sort
Bucket sort
 
Chapter 8 sorting algo
Chapter 8 sorting algoChapter 8 sorting algo
Chapter 8 sorting algo
 
Shellsort
ShellsortShellsort
Shellsort
 
Merge sort code in C explained
Merge sort code in C explained Merge sort code in C explained
Merge sort code in C explained
 
Radix Sort
Radix SortRadix Sort
Radix Sort
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
Tutorial aed iii 002 - algoritmo de ordenação shellsort
Tutorial aed iii   002 - algoritmo de ordenação shellsortTutorial aed iii   002 - algoritmo de ordenação shellsort
Tutorial aed iii 002 - algoritmo de ordenação shellsort
 
ordenacao shellsort quicksort em C
ordenacao shellsort quicksort em Cordenacao shellsort quicksort em C
ordenacao shellsort quicksort em C
 
Algoritmo Shell Sort
Algoritmo Shell SortAlgoritmo Shell Sort
Algoritmo Shell Sort
 
Bubble Sort
Bubble SortBubble Sort
Bubble Sort
 
Insertion Sort
Insertion SortInsertion Sort
Insertion Sort
 
Makalah shell sort
Makalah shell sortMakalah shell sort
Makalah shell sort
 
Shell sort
Shell sortShell sort
Shell sort
 
BUCKET SORT
BUCKET SORTBUCKET SORT
BUCKET SORT
 
Selection Sort
Selection SortSelection Sort
Selection Sort
 

Semelhante a Shell sort[1]

Chapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptChapter 12 - Heaps.ppt
Chapter 12 - Heaps.ppt
MouDhara1
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
Rafay Farooq
 

Semelhante a Shell sort[1] (20)

shell and merge sort
shell and merge sortshell and merge sort
shell and merge sort
 
Advanced s and s algorithm.ppt
Advanced s and s algorithm.pptAdvanced s and s algorithm.ppt
Advanced s and s algorithm.ppt
 
algorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptxalgorithm assignmenteeeeeee.pptx
algorithm assignmenteeeeeee.pptx
 
SHELL SORT-2.pptx
SHELL SORT-2.pptxSHELL SORT-2.pptx
SHELL SORT-2.pptx
 
Lockless
LocklessLockless
Lockless
 
Chapter 4 ds
Chapter 4 dsChapter 4 ds
Chapter 4 ds
 
lecture-k-sorting.ppt
lecture-k-sorting.pptlecture-k-sorting.ppt
lecture-k-sorting.ppt
 
3.ppt
3.ppt3.ppt
3.ppt
 
Lecture k-sorting
Lecture k-sortingLecture k-sorting
Lecture k-sorting
 
ALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTESALGORITHMS - SHORT NOTES
ALGORITHMS - SHORT NOTES
 
Chapter 12 - Heaps.ppt
Chapter 12 - Heaps.pptChapter 12 - Heaps.ppt
Chapter 12 - Heaps.ppt
 
Chapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for printChapter 8 advanced sorting and hashing for print
Chapter 8 advanced sorting and hashing for print
 
algorithm unit 1
algorithm unit 1algorithm unit 1
algorithm unit 1
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer science
 
Storm Real Time Computation
Storm Real Time ComputationStorm Real Time Computation
Storm Real Time Computation
 
Spark 4th Meetup Londond - Building a Product with Spark
Spark 4th Meetup Londond - Building a Product with SparkSpark 4th Meetup Londond - Building a Product with Spark
Spark 4th Meetup Londond - Building a Product with Spark
 
Sorting Techniques
Sorting TechniquesSorting Techniques
Sorting Techniques
 
Lecture 11.2 : sorting
Lecture 11.2 :  sortingLecture 11.2 :  sorting
Lecture 11.2 : sorting
 
simple-sorting algorithms
simple-sorting algorithmssimple-sorting algorithms
simple-sorting algorithms
 
Design and Analysis of algorithms
Design and Analysis of algorithmsDesign and Analysis of algorithms
Design and Analysis of algorithms
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

Shell sort[1]

  • 1. CSCD 300 Data Structures Donald Shell’s Sorting Algorithm Originally developed by Bill Clark, modified by Tom Capaul and Tim Rolfe 1
  • 2. Shell Sort - Introduction More properly, Shell’s Sort Created in 1959 by Donald Shell Link to a local copy of the article: Donald Shell, “A High-Speed Sorting Procedure”, Communications of the ACM Vol 2, No. 7 (July 1959), 30-32 Originally Shell built his idea on top of Bubble Sort (link to article flowchart), but it has since been transported over to Insertion Sort. 2
  • 3. Shell Sort -General Description Essentially a segmented insertion sort Divides an array into several smaller noncontiguous segments The distance between successive elements in one segment is called a gap. Each segment is sorted within itself using insertion sort. Then resegment into larger segments (smaller gaps) and repeat sort. Continue until only one segment (gap = 1) final sort finishes array sorting. 3
  • 4. Shell Sort -Background General Theory: Makes use of the intrinsic strengths of Insertion sort. Insertion sort is fastest when: The array is nearly sorted. The array contains only a small number of data items. Shell sort works well because: It always deals with a small number of elements. Elements are moved a long way through array with each swap and this leaves it more nearly sorted. 4
  • 5. Shell Sort - example Initial Segmenting Gap = 4 80 93 60 12 42 30 68 85 10 10 30 60 12 42 93 68 85 80 5
  • 6. Shell Sort - example (2) Resegmenting Gap = 2 10 30 60 12 42 93 68 85 80 10 12 42 30 60 85 68 93 80 6
  • 7. Shell Sort - example (3) Resegmenting Gap = 1 10 12 42 30 60 85 68 93 80 10 12 30 42 60 68 80 85 93 7
  • 8. Gap Sequences for Shell Sort 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 gap values. Any sequence will work as long as it is increasing and h1 = 1. For any gap value hk we have A[i] <= A[i + hk] An array A for which this is true is hk sorted. An array which is hk sorted and is then hk-1 sorted remains hk sorted. 8
  • 9. Shell Sort - Ideal Gap Sequence Although any increasing sequence will work ( if h1 = 1): Best results are obtained when all values in the gap sequence are relatively prime (sequence does not share any divisors). Obtaining a relatively prime sequence is often not practical in a program so practical solutions try to approximate relatively prime sequences. 9
  • 10. Shell Sort - Practical Gap Sequences Three possibilities presented: 1) Shell's suggestion - first gap is N/2 - successive gaps are previous value divided by 2. Odd gaps only - like Shell method except if division produces an even number add 1. better performance than 1) since all odd values eliminates the factor 2. 2.2 method - like Odd gaps method (add 1 to even division result) but use a divisor of 2.2 and truncate. best performance of all - most nearly a relatively prime sequence. 10
  • 11. Shell Sort - Added Gap Sequence Donald Knuth, in his discussion of Shell’s Sort, recommended another sequence of gaps. h0 = 1 hj+1 = hj * 3 + 1 Find the hj > n, then start with hj/3 11
  • 12. Link to the Java program that generated the above data. 12
  • 13. Shell Sort - Time Complexity Time complexity: O(nr) with 1 < r < 2 This is better than O(n2) but generally worse than O(n log2n). 13
  • 14. Shellsort - Code public static void shellSort( Comparable[ ] theArray, int n ) { // shellSort: sort first n items in array theArray for( int gap = n / 2; gap > 0; gap = gap / 2 ) for( int i = gap; i < n; i++ ) { Comparable tmp = theArray[ i ]; int j = i; for( ; j >= gap && tmp.compareTo(theArray[ j - gap ]) < 0 ; j -= gap ) theArray[ j ] = theArray[ j - gap ]; theArray[ j ] = tmp; } } 14
  • 15. ShellSort -Trace (gap = 4) [0] [1] [2] theArray 80 n: 9 gap: 4 93 60 [3] [4] [5] [6] [7] [8] 12 85 10 42 30 68 i: j: for( int gap = n / 2; gap > 0; gap = gap / 2 ) for( int i = gap; i < n; i++ ) { Comparable tmp = theArray[ i ]; int j = i; for( ; j >= gap && tmp.compareTo(theArray[ j - gap ]) < 0 ; j -= gap ) theArray[ j ] = theArray[ j - gap ]; theArray[ j ] = tmp; } 15
  • 16. ShellSort -Trace (gap = 2) [0] [1] [2] theArray [3] [4] [5] [6] [7] [8] 10 12 85 80 n: 9 gap: 2 30 60 42 93 68 i: j: for( int gap = n / 2; gap > 0; gap = gap / 2 ) for( int i = gap; i < n; i++ ) { Comparable tmp = theArray[ i ]; int j = i; for( ; j >= gap && tmp.compareTo(theArray[ j - gap ]) < 0 ; j -= gap ) theArray[ j ] = theArray[ j - gap ]; theArray[ j ] = tmp; } 16
  • 17. ShellSort -Trace (gap = 1) [0] [1] [2] theArray [3] [4] [5] [6] [7] [8] 10 30 93 80 n: 9 gap: 1 12 42 60 85 68 i: j: for( int gap = n / 2; gap > 0; gap = gap / 2 ) for( int i = gap; i < n; i++ ) { Comparable tmp = theArray[ i ]; int j = i; for( ; j >= gap && tmp.compareTo(theArray[ j - gap ]) < 0 ; j -= gap ) theArray[ j ] = theArray[ j - gap ]; theArray[ j ] = tmp; } 17