SlideShare a Scribd company logo
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

More Related Content

What's hot (20)

PPTX
Quick sort
Jehat Hassan
 
PDF
Binary search algorithm
maamir farooq
 
PPTX
Linear and Binary search
Nisha Soms
 
PPTX
Doubly Linked List
Ninad Mankar
 
PDF
Searching and Sorting Algorithms
Ashutosh Satapathy
 
PPTX
Quick sort
Afaq Mansoor Khan
 
PPT
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
PPTX
Bubble sort
Rashmi R Upadhya
 
PPT
B trees in Data Structure
Anuj Modi
 
PPT
Insertion sort
Delowar Hossain
 
PDF
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
PDF
Sorting Algorithms
Mohammed Hussein
 
PPTX
trees in data structure
shameen khan
 
PPT
Hash tables
Rajendran
 
PPTX
Quicksort Presentation
irdginfo
 
PPT
Queue Data Structure
Zidny Nafan
 
PPTX
Bubble Sort Algorithm Presentation
AhmedAlbutty
 
PPTX
Insertion sort
almaqboli
 
PPTX
Binary search
AparnaKumari31
 
PPT
stack presentation
Shivalik college of engineering
 
Quick sort
Jehat Hassan
 
Binary search algorithm
maamir farooq
 
Linear and Binary search
Nisha Soms
 
Doubly Linked List
Ninad Mankar
 
Searching and Sorting Algorithms
Ashutosh Satapathy
 
Quick sort
Afaq Mansoor Khan
 
Algorithm And analysis Lecture 03& 04-time complexity.
Tariq Khan
 
Bubble sort
Rashmi R Upadhya
 
B trees in Data Structure
Anuj Modi
 
Insertion sort
Delowar Hossain
 
Quick Sort , Merge Sort , Heap Sort
Mohammed Hussein
 
Sorting Algorithms
Mohammed Hussein
 
trees in data structure
shameen khan
 
Hash tables
Rajendran
 
Quicksort Presentation
irdginfo
 
Queue Data Structure
Zidny Nafan
 
Bubble Sort Algorithm Presentation
AhmedAlbutty
 
Insertion sort
almaqboli
 
Binary search
AparnaKumari31
 

Viewers also liked (20)

PPTX
Shell sort slide
Bella Angriani
 
PPTX
Shell sort
Felipe Weizenmann
 
PPT
Shell sort
Nauman Ali
 
PPT
3.3 shell sort
Krish_ver2
 
PPT
Merge sort
Vidushi Pathak
 
PPT
Quick Sort
priyankanaidu6
 
PPT
Bucket sort
Hossain Md Shakhawat
 
PDF
Chapter 8 sorting algo
Charlie James Gomez
 
PPSX
Shellsort
IUPSM
 
PPTX
Merge sort code in C explained
Mohit Tare
 
PPT
Algorithm: Quick-Sort
Tareq Hasan
 
PDF
Tutorial aed iii 002 - algoritmo de ordenação shellsort
Flávio Freitas
 
PDF
ordenacao shellsort quicksort em C
Alessandro Trevisan
 
PDF
Algoritmo Shell Sort
Gabriel Albuquerque
 
PDF
Bubble Sort
Daniel Arndt Alves
 
PDF
Insertion Sort
Daniel Arndt Alves
 
PDF
Makalah shell sort
Bella Angriani
 
PPTX
Shell sort
Waqar Ashraf
 
PPTX
BUCKET SORT
eidyyolisbet
 
Shell sort slide
Bella Angriani
 
Shell sort
Felipe Weizenmann
 
Shell sort
Nauman Ali
 
3.3 shell sort
Krish_ver2
 
Merge sort
Vidushi Pathak
 
Quick Sort
priyankanaidu6
 
Chapter 8 sorting algo
Charlie James Gomez
 
Shellsort
IUPSM
 
Merge sort code in C explained
Mohit Tare
 
Algorithm: Quick-Sort
Tareq Hasan
 
Tutorial aed iii 002 - algoritmo de ordenação shellsort
Flávio Freitas
 
ordenacao shellsort quicksort em C
Alessandro Trevisan
 
Algoritmo Shell Sort
Gabriel Albuquerque
 
Bubble Sort
Daniel Arndt Alves
 
Insertion Sort
Daniel Arndt Alves
 
Makalah shell sort
Bella Angriani
 
Shell sort
Waqar Ashraf
 
BUCKET SORT
eidyyolisbet
 
Ad

Similar to Shell sort[1] (20)

PPTX
shell and merge sort
i i
 
PPT
Chapter 6 - Advanced Sorting Algorithms (1).ppt
afendimohammed288
 
PPT
Advanced s and s algorithm.ppt
LegesseSamuel
 
PPTX
algorithm assignmenteeeeeee.pptx
kassahungebrie
 
PPTX
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
PPTX
SHELL SORT-2.pptx
CS50Bootcamp
 
PDF
Lockless
Sandeep Joshi
 
PPT
Chapter 4 ds
Hanif Durad
 
PPT
lecture-k-sorting.ppt
SushantRaj25
 
PPT
Lecture k-sorting
Vijayaraj Raj
 
PPTX
Lecture-12-CS345A-2023 of Design and Analysis
ssuser9183b6
 
DOC
ALGORITHMS - SHORT NOTES
suthi
 
PPT
Chapter 12 - Heaps.ppt
MouDhara1
 
PPT
lecture to proide studeny with F-k-sorting.ppt
indranilbanerji25109
 
PDF
Chapter 8 advanced sorting and hashing for print
Abdii Rashid
 
RTF
algorithm unit 1
Monika Choudhery
 
PPTX
Algorithm in computer science
Riazul Islam
 
PPTX
Data Structure Algorithm -Algorithm Complexity
zeeshanhaidermazhar7
 
PDF
Storm Real Time Computation
Sonal Raj
 
shell and merge sort
i i
 
Chapter 6 - Advanced Sorting Algorithms (1).ppt
afendimohammed288
 
Advanced s and s algorithm.ppt
LegesseSamuel
 
algorithm assignmenteeeeeee.pptx
kassahungebrie
 
Module-1.pptxbdjdhcdbejdjhdbchchchchchjcjcjc
shashashashashank
 
SHELL SORT-2.pptx
CS50Bootcamp
 
Lockless
Sandeep Joshi
 
Chapter 4 ds
Hanif Durad
 
lecture-k-sorting.ppt
SushantRaj25
 
Lecture k-sorting
Vijayaraj Raj
 
Lecture-12-CS345A-2023 of Design and Analysis
ssuser9183b6
 
ALGORITHMS - SHORT NOTES
suthi
 
Chapter 12 - Heaps.ppt
MouDhara1
 
lecture to proide studeny with F-k-sorting.ppt
indranilbanerji25109
 
Chapter 8 advanced sorting and hashing for print
Abdii Rashid
 
algorithm unit 1
Monika Choudhery
 
Algorithm in computer science
Riazul Islam
 
Data Structure Algorithm -Algorithm Complexity
zeeshanhaidermazhar7
 
Storm Real Time Computation
Sonal Raj
 
Ad

Recently uploaded (20)

PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
PDF
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
PDF
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
PPTX
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
PPTX
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PPTX
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
PDF
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
Peak of Data & AI Encore - Real-Time Insights & Scalable Editing with ArcGIS
Safe Software
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
Agile Chennai 18-19 July 2025 | Workshop - Enhancing Agile Collaboration with...
AgileNetwork
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Generative AI vs Predictive AI-The Ultimate Comparison Guide
Lily Clark
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
IT Runs Better with ThousandEyes AI-driven Assurance
ThousandEyes
 
State-Dependent Conformal Perception Bounds for Neuro-Symbolic Verification
Ivan Ruchkin
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Agentic AI in Healthcare Driving the Next Wave of Digital Transformation
danielle hunter
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
Make GenAI investments go further with the Dell AI Factory
Principled Technologies
 

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