DSA Lab Manual(Shell Sort)
DSA Lab Manual(Shell Sort)
Objective:
To understand and implement the Shell Sort algorithm using C++ and analyze its performance.
Theory:
Shell Sort is an optimization over the Insertion Sort algorithm. It works by dividing the list into
smaller sublists based on a gap value and sorting each sublist using insertion sort. As the gap
decreases, the sublists grow until the entire list is sorted.
Key Concepts:
1. Gap Sequence: The gap determines how far apart elements are compared. Common
sequences include .
2. Efficiency: Shell Sort is more efficient than Insertion Sort for larger datasets as it reduces
the number of shifts and comparisons.
3. Stability: Shell Sort is not stable since it might change the relative order of equal
elements.
Algorithm:
Code Implementation:
#include <iostream>
using namespace std;
// Driver program
int main() {
int arr[] = {12, 34, 54, 2, 3};
int n = sizeof(arr) / sizeof(arr[0]);
shellSort(arr, n);
return 0;
}
Input:
Output:
Sorted array: ..??
Observations:
Conclusion:
Shell Sort is a simple and efficient sorting algorithm that performs better than basic Insertion
Sort for larger datasets. The gap sequence plays a crucial role in its performance.
Viva Questions:
1. What is the main difference between Insertion Sort and Shell Sort?
2. How does the gap sequence influence the performance of Shell Sort?
3. Can Shell Sort be used for linked lists? Why or why not?
4. Is Shell Sort a stable sorting algorithm? Explain your answer.
Practice Task: