Shell Sort Algorithm
Shell Sort Algorithm
Shell sort is a highly efficient sorting algorithm and is based on insertion sort
algorithm. This algorithm avoids large shifts as in case of insertion sort, if the
smaller value is to the far right and has to be moved to the far left.
This algorithm uses insertion sort on a widely spread elements, first to sort them and
then sorts the less widely spaced elements. This spacing is termed as interval. This
interval is calculated based on Knuth's formula as −
h = h * 3 + 1
where −
h is interval with initial value 1
This algorithm is quite efficient for medium-sized data sets as its average and worst
case complexity are of O(n), where n is the number of items.
Pseudocode
procedure shellSort()
A : array of items
/* calculate interval*/
while interval < A.length /3 do:
interval = interval * 3 + 1
end while
https://www.tutorialspoint.com/data_structures_algorithms/shell_sort_algorithm.htm 1/7
2/27/24, 12:53 PM Shell Sort Algorithm
/* calculate interval*/
interval = (interval -1) /3;
end while
end procedure
Example
Let us consider the following example to have an idea of how shell sort works. We
take the same array we have used in our previous examples. For our example and
ease of understanding, we take the interval of 4. Make a virtual sub-list of all values
located at the interval of 4 positions. Here these values are {35, 14}, {33, 19}, {42,
27} and {10, 14}
https://www.tutorialspoint.com/data_structures_algorithms/shell_sort_algorithm.htm 2/7
2/27/24, 12:53 PM Shell Sort Algorithm
We compare values in each sub-list and swap them (if necessary) in the original
array. After this step, the new array should look like this −
Then, we take interval of 2 and this gap generates two sub-lists - {14, 27, 35, 42},
{19, 10, 33, 44}
https://www.tutorialspoint.com/data_structures_algorithms/shell_sort_algorithm.htm 3/7
2/27/24, 12:53 PM Shell Sort Algorithm
We compare and swap the values, if required, in the original array. After this step,
the array should look like this −
Finally, we sort the rest of the array using interval of value 1. Shell sort uses
insertion sort to sort the array.
https://www.tutorialspoint.com/data_structures_algorithms/shell_sort_algorithm.htm 4/7
2/27/24, 12:53 PM Shell Sort Algorithm
We see that it required only four swaps to sort the rest of the array.
Implementation
Shell sort is a highly efficient sorting algorithm and is based on insertion sort
algorithm. This algorithm avoids large shifts as in case of insertion sort, if the
smaller value is to the far right and has to be moved to the far left.
https://www.tutorialspoint.com/data_structures_algorithms/shell_sort_algorithm.htm 5/7
2/27/24, 12:53 PM Shell Sort Algorithm
#include<iostream>
using namespace std;
void shellSort(int *arr, int n){
int gap, j, k;
for(gap = n/2; gap > 0; gap = gap / 2) { //initially gap = n/2, decr
for(j = gap; j<n; j++) {
for(k = j-gap; k>=0; k -= gap) {
if(arr[k+gap] >= arr[k])
break;
else {
int temp;
temp = arr[k+gap];
arr[k+gap] = arr[k];
arr[k] = temp;
}
}
}
}
}
int main(){
int n;
n = 5;
int arr[5] = {33, 45, 62, 12, 98}; // initialize the array
cout << "Array before Sorting: ";
for(int i = 0; i<n; i++)
cout << arr[i] << " ";
cout << endl;
shellSort(arr, n);
cout << "Array after Sorting: ";
for(int i = 0; i<n; i++)
cout << arr[i] << " ";
cout << endl;
}
Output
https://www.tutorialspoint.com/data_structures_algorithms/shell_sort_algorithm.htm 6/7
2/27/24, 12:53 PM Shell Sort Algorithm
https://www.tutorialspoint.com/data_structures_algorithms/shell_sort_algorithm.htm 7/7