0% found this document useful (0 votes)
14 views

Shell_Sort

sorting algorithm - shell sort
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Shell_Sort

sorting algorithm - shell sort
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Understanding

Shell Sort
Algorithm Overview and Applications

Course : Data Structure and Algorithm


Date : 1- 10 - 2024
Introduction

What is Shell Sort?


• A generalization of insertion sort
• Designed to improve upon the inefficiency of insertion sort
• Created by Donald Shell in 1959
• The performance of the shell sort depends on the type of
sequence called Shell’s gap sequence
How Shell Sort Works
Concept:
• Insertion sort compares adjacent elements, Shell sort compares
elements far apart based on ‘Gap’(far away element)
• Initially Gap = n/2; Gap = Gap/2;
• Shell’s gap sequence: n/2, n/4, …, 1
• Check wheather left element < right element else swap it
• Move to next element repeat this untill end of array reach
• Gradually reduces the gap between compared elements
Shell Sort Routine
void shellSort(int arr[], int n) {
int gap, i, j, temp;
for (gap = n / 2; gap > 0; gap /= 2) {
for (i = gap; i < n; i++) {
temp = arr[i];
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
}
Time and Space Complexity

Time Complexity
• Worst Case: O(n²)
• Best Case: O(n log² n)
• Average Case: Depends on gap sequence, typically better than O(n²)

Space Complexity
• In-Place Sorting: O(1) auxiliary space
• No additional memory required.
Advantages & Disadvantages

Advantages:
• - Faster than Insertion Sort for larger arrays
• - Easy to implement
Disadvantages:
• - Not stable
• - Performance depends
on gap sequence
Use Cases

• Good for small to medium-sized datasets


• Useful in systems where memory is limited due to O(1) space
complexity.
• uClibc Library: This C standard library for embedded
systems uses shell sort for its efficiency and low overhead.
• If you have a collection of records or objects that need to be
sorted based on one or more attributes, Shell sort can be used.
Conclusion

Summary:
• Shell Sort is an optimized version of insertion sort.
• Efficient for medium-sized arrays when an
appropriate gap sequence is used.
• It sort the array based on the Gap

You might also like