C++ Program to Implement Shell Sort


This is a C++ program to sort the given data using Shell Sort.

Problem Description

1. Shell sort is an improvement over insertion sort.
2. It compares the element separated by a gap of several positions.
3. A data element is sorted with multiple passes and with each pass gap value reduces.
4. The worst case time complexity is O(n*log(n)).

Problem Solution

1. Assign gap value as half the length of the array.
2. Compare element present at a difference of gap value.
3. Sort them and reduce the gap value to half and repeat.
4. Display the result.
5. Exit.

Program/Source Code

C++ program to implement Shell Sort.
This program is successfully run on Dev-C++ using TDM-GCC 4.9.2 MinGW compiler on a Windows system.

using namespace std;
// A function implementing Shell sort.
void ShellSort(int a[], int n)
	int i, j, k, temp;
	// Gap 'i' between index of the element to be compared, initially n/2.
	for(i = n/2; i > 0; i = i/2)
		for(j = i; j < n; j++)
			for(k = j-i; k >= 0; k = k-i)
				// If value at higher index is greater, then break the loop.
				if(a[k+i] >= a[k])
				// Switch the values otherwise.
					temp = a[k];
					a[k] = a[k+i];
					a[k+i] = temp;
int main()
	int n, i;
	cout<<"\nEnter the number of data element to be sorted: ";
	int arr[n];
	for(i = 0; i < n; i++)
		cout<<"Enter element "<<i+1<<": ";
	ShellSort(arr, n);
	// Printing the sorted data.
	cout<<"\nSorted Data ";
	for (i = 0; i < n; i++)
	return 0;
Program Explanation

1. Take input of data.
2. Call ShellSort() function with ‘arr’ the array of data and ‘n’ the number of values, in the argument list.
3. Implement Sorting algorithm using nested for loop.
4. The first loop will run on ‘i’ Which decides the gap value to compare two elements.
5. The second loop will run on ‘j’ from i to n-1.
6. The third loop will run on ‘k’ and sort the element having i as a gap between their index.
7. Switch the values if arr[k] < arr[k-i]. 8. Return to main and display the result. 9. Exit.

Runtime Test Cases
Case 1:
Enter the number of data element to be sorted: 10
Enter element 1: 9
Enter element 2: 3
Enter element 3: 4
Enter element 4: 6
Enter element 5: 8
Enter element 6: 5
Enter element 7: 1
Enter element 8: 2
Enter element 9: 7
Enter element 10: 0
Sorted Data ->0->1->2->3->4->5->6->7->8->9

Sanfoundry Global Education & Learning Series – C++ Algorithms.
To practice all C++ Algorithms, here is complete set of 1000 C++ Algorithms.

Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn