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

DSA Lab Manual(Shell Sort)

The document outlines a lab manual for implementing the Shell Sort algorithm in C++. It explains the theory behind Shell Sort, its efficiency compared to Insertion Sort, and provides a code implementation in C++. Additionally, it includes observations, a conclusion on the algorithm's performance, and viva questions for further understanding.

Uploaded by

Irfan Ul Haq
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

DSA Lab Manual(Shell Sort)

The document outlines a lab manual for implementing the Shell Sort algorithm in C++. It explains the theory behind Shell Sort, its efficiency compared to Insertion Sort, and provides a code implementation in C++. Additionally, it includes observations, a conclusion on the algorithm's performance, and viva questions for further understanding.

Uploaded by

Irfan Ul Haq
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab Manual

Subject: Data Structures and Algorithms (DSA)


Experiment Title: Implementation of Shell Sort Algorithm in C++

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:

1. Initialize the gap sequence.


2. For each gap, divide the list into sublists.
3. Sort each sublist using insertion sort.
4. Decrease the gap and repeat until the gap becomes 1.
5. Perform a final pass with a gap of 1 to ensure the list is sorted.

Code Implementation:

#include <iostream>
using namespace std;

// Function to sort an array using Shell Sort


void shellSort(int arr[], int n) {
// Start with a big gap, then reduce the gap
for (int gap = n / 2; gap > 0; gap /= 2) {
// Do a gapped insertion sort for this gap size.
// The first gap elements a[0..gap-1] are already in gapped
order.
// Keep adding one more element until the entire array is
// gap sorted
for (int i = gap; i < n; i += 1) {
// add a[i] to the elements that have been gap sorted
// save a[i] in temp and make a hole at position i
int temp = arr[i];

// shift earlier gap-sorted elements up until the correct


// location for a[i] is found
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)
arr[j] = arr[j - gap];

// put temp (the original a[i]) in its correct location


arr[j] = temp;
}
}
}

// Function to print an array


void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << endl;
}

// Driver program
int main() {
int arr[] = {12, 34, 54, 2, 3};
int n = sizeof(arr) / sizeof(arr[0]);

shellSort(arr, n);

cout << "Sorted array: \n";


printArray(arr, n);

return 0;
}

Input and Output:

Input:

Original array: 12, 34, 54, 2, 3

Output:
Sorted array: ..??

Observations:

1. The number of comparisons and shifts decreases as the gap reduces.


2. The algorithm performs efficiently for moderately sized datasets.
3. The choice of gap sequence affects the performance of Shell Sort.

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:

1. Implement Shell Sort with a custom gap sequence such as .


2. Analyze the time complexity of Shell Sort for different input sizes and gap sequences.

You might also like