
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Implement Shell Sort
The shell sorting technique is based on the insertion sort. In the insertion sort sometimes we need to shift large block to insert item in the correct location. Using shell sort, we can avoid large number of shifting. The sorting is done with specific interval. After each pass the interval is reduced to make smaller interval.
The complexity of Shell Sort Technique
Time Complexity: O(n log n) for best case, and for other cases, it depends on the gap sequence.
Space Complexity: O(1)
Input − The unsorted list: 23 56 97 21 35 689 854 12 47 66 Output − Array after Sorting: 12 21 23 35 47 56 66 97 689 854
Algorithm
shellSort(array, size)
Input: An array of data, and the total number in the array
Output: The sorted Array
Begin for gap := size / 2, when gap > 0 and gap is updated with gap / 2 do for j:= gap to size– 1 do for k := j-gap to 0, decrease by gap value do if array[k+gap] >= array[k] break else swap array[k + gap] with array[k] done done done End
Example Code
#include<iostream> using namespace std; void swapping(int &a, int &b) { //swap the content of a and b int temp; temp = a; a = b; b = temp; } void display(int *array, int size) { for(int i = 0; i<size; i++) cout << array[i] << " "; cout << endl; } void shellSort(int *arr, int n) { int gap, j, k; for(gap = n/2; gap > 0; gap = gap / 2) { //initially gap = n/2, decreasing by gap /2 for(j = gap; j<n; j++) { for(k = j-gap; k>=0; k -= gap) { if(arr[k+gap] >= arr[k]) break; else swapping(arr[k+gap], arr[k]); } } } } int main() { int n; cout << "Enter the number of elements: "; cin >> n; int arr[n]; //create an array with given number of elements cout << "Enter elements:" << endl; for(int i = 0; i<n; i++) { cin >> arr[i]; } cout << "Array before Sorting: "; display(arr, n); shellSort(arr, n); cout << "Array after Sorting: "; display(arr, n); }
Output
Enter the number of elements: 10 Enter elements: 23 56 97 21 35 689 854 12 47 66 Array before Sorting: 23 56 97 21 35 689 854 12 47 66 Array after Sorting: 12 21 23 35 47 56 66 97 689 854
- Related Articles
- Python Program to Implement Shell Sort
- Shell Sort program in C#
- Java program to implement bubble sort
- Java program to implement selection sort
- Java program to implement insertion sort
- C++ Program to Implement Radix Sort
- C++ Program to Implement Bucket Sort
- C++ Program to Implement Bubble Sort
- C++ Program to Implement Heap Sort
- C++ Program to Implement Merge Sort
- C++ Program to Implement Selection Sort
- C++ Program to Implement Insertion Sort
- C++ Program to Implement Counting Sort
- Shell Sort
- Program to implement Bucket Sort in JavaScript

Advertisements