0% found this document useful (0 votes)
9 views9 pages

Sorting Algorithm Part - 6

Uploaded by

Rangita Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Sorting Algorithm Part - 6

Uploaded by

Rangita Shah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Page 1 of 9

Home Whiteboard Graphing Calculator Online Compilers Articles Tool

Home Data_structures_algorithms 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.

Shell Sort Algorithm


Following is the algorithm for shell sort.

1. Initialize the value of h.


2. Divide the list into smaller sub-list of equal interval h.
3. Sort these sub-lists using insertion sort.
4. Repeat until complete list is sorted.

Pseudocode
Following is the pseudocode for shell sort.

[Link] 1/9
Page 2 of 9

procedure shellSort()
A : array of items

/* calculate interval*/
while interval < [Link] /3 do:
interval = interval * 3 + 1
end while

while interval > 0 do:


for outer = interval; outer < [Link]; outer ++ do:

/* select value to be inserted */


valueToInsert = A[outer]
inner = outer;

/*shift element towards right*/


while inner > interval -1 && A[inner - interval]
>= valueToInsert do:
A[inner] = A[inner - interval]
inner = inner interval
end while

/* insert the number at hole position */


A[inner] = valueToInsert
end for

/* 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}

[Link] 2/9
Page 3 of 9

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}

[Link] 3/9
Page 4 of 9

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.

Following is the step-by-step depiction −

[Link] 4/9
Page 5 of 9

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

[Link] 5/9
Page 6 of 9

far right and has to be moved to the far left.

C C++ Java Python

#include <stdio.h>
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 {
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
printf("Array before Sorting: ");
for(int i = 0; i<n; i++)
printf("%d ",arr[i]);
printf("\n");
shellSort(arr, n);
printf("Array after Sorting: ");
for(int i = 0; i<n; i++)
printf("%d ", arr[i]);
printf("\n");
}

Output

[Link] 6/9
Page 7 of 9

Array before Sorting: 33 45 62 12 98


Array after Sorting: 12 33 45 62 98

TOP TUTORIALS

Python Tutorial
Java Tutorial
C++ Tutorial

C Programming Tutorial
C# Tutorial
PHP Tutorial

R Tutorial
HTML Tutorial
CSS Tutorial

JavaScript Tutorial
SQL Tutorial

TRENDING TECHNOLOGIES

Cloud Computing Tutorial


Amazon Web Services Tutorial

Microsoft Azure Tutorial


Git Tutorial
Ethical Hacking Tutorial
Chapters Categories
Docker Tutorial
Kubernetes Tutorial
DSA Tutorial

Spring Boot Tutorial


SDLC Tutorial
Unix Tutorial

CERTIFICATIONS

Business Analytics Certification

Java & Spring Boot Advanced Certification


Data Science Advanced Certification

[Link] 7/9
Page 8 of 9

Cloud Computing And DevOps


Advanced Certification In Business Analytics

Artificial Intelligence And Machine Learning


DevOps Certification
Game Development Certification

Front-End Developer Certification


AWS Certification Training
Python Programming Certification

COMPILERS & EDITORS

Online Java Compiler

Online Python Compiler


Online Go Compiler
Online C Compiler

Online C++ Compiler


Online C# Compiler
Online PHP Compiler

Online MATLAB Compiler


Online Bash Terminal
Online SQL Compiler

Online Html Editor

ABOUT US | OUR TEAM | CAREERS | JOBS | CONTACT US | TERMS OF USE |

PRIVACY POLICY | REFUND POLICY | COOKIES POLICY | FAQ'S

Tutorials Point is a leading Ed Tech company striving to provide the best learning material on
technical and non-technical subjects.

[Link] 8/9
Page 9 of 9

© Copyright 2026. All Rights Reserved.

[Link] 9/9

You might also like