×

Shell Sort

Shell Sort: Shell sort is a sorting algorithm. It is an extended version of the insertion sort.

  • In this sorting, we compare the elements that are distant apart rather than the adjacent.
  • We start by comparing elements that are at a certain distance apart. So, if there are N elements then we start with a value gap < N.
  • In each iteration we keep decreasing the value of the gap until the difference becomes 1.
  • In the last iteration, the shell sort acts as an insertion-type.

Complexity table of shell sort

ComplexityBest caseAverage caseWorst case
TimeO(n)O((nlog(n))2)O((nlog(n))2)
Space  O(1)

Algorithm of shell sort

Shell_Sort (array, size)
for interval i ? size /2 down to 1.     
for each interval "i" in the array.        
sort all the elements at interval "i".  
end Shell_Sort.  

Shell sort example

Suppose you have the following array, which you have to sort.

141819372340293011

The size of array is 9, so n = 9.

Shell Sort in DS

    Now, find the value of the Gap.                             

Gap = floor(n / 2)                                     

= floor (9 / 2)                                     

= floor(4.5)                                     

= 4                

Pass = 1 Gap = 4  

Gap 4 means, if we consider the first element at index 0 then next element will be at index 0+4 = 4., and third element will be at index 4 + 4 = 8.

Shell Sort in DS

 Similarly, if we consider the element at index 1 then next element will be at index 1 + 4 = 5, and third element will be at index 5 + 4 = 9. But there is no 9th index in the above array so we will just have index 1 and 5.

Shell Sort in DS

Similarly, if we consider the element at index 2, 3, and 4 respectively then next element will be at index 2 + 4 = 6, 3 + 4 = 7, and 4 + 4 = 8 respectively.

Shell Sort in DS

  Compare the elements.  

A[0] > A[4] = 14 > 23 // false  

A[1] > A[5] = 18 > 40 // false  

A[2] > A[6] = 19 > 29 // false  

A[3] > A[7] = 37 > 30 // true i.e., swap the both element positions.

Shell Sort in DS

A[4] > A[8] = 23 > 11 // true i.e., swap the both element positions.  

A[1] > A[4] = 14 > 11 // true i.e., swap the both element positions.

Shell Sort in DS

Pass 1 is complete.  

Pass 2: Gap = floor(Gap / 2)            

Gap = floor(4 / 2)  // in pass 1,

Gap was 4.            

Gap = 2  

Gap 2 means, if we consider the first element at index 0 then next element will be at index 0+2 = 2, and similarly this process applied till the end of index.

Shell Sort in DS

Compare the elements.

A[0] > A[2] = 11 > 19 // false

A[]1 > A[3] = 18 > 30 // false

A[2] > A[4] = 19 > 14 // true, i.e. swap the both element positions.

A[1] > A[2] = 11 > 14 // false

Shell Sort in DS

A[3] > A[5] = 30 > 40 //false

A[4] > A[6] = 19 > 29 // false

A[5] > A[7] = 40 > 37 // true, i.e. swap the both element positions.

A[3] > A[5] = 30 > 37 // false

Shell Sort in DS

A[6] > A[8] = 29 > 23 // true, i.e. swap the both element positions.

A[4] > A[6] = 19 > 23 // false

Shell Sort in DS

Pass 2 is complete.  

Pass 3: Gap = floor(Gap/2)             

Gap = floor(2 / 2)  // in pass 2, Gap was 2.             

Gap = 1                 // i.e. last pass when gap is 1, shell sort is like the insertion sort. Compare the elements.

A[0] > A[1] = 11 > 18 // false

A[1] > A[2] = 18 > 14 // true i.e. swap the both element positions.

A[0] > A[1] = 11 > 14 // false

Shell Sort in DS

A[2] > A[3] = 18 > 30 // false A[3] > A[4] = 30 > 19 // true i.e. swap the both element positions. A[2] > A[3] = 18 > 19 // false

Shell Sort in DS

A[4] > A[5] = 30 > 37 // false A[5] > A[6] = 37 > 23 // true i.e. swap the both element positions. A[4] > A[5] = 30 > 23 // true i.e. swap the both element positions. A[3] > A[4] = 19 > 23 // false

Shell Sort in DS

A[6] > A[7] = 37 > 40 // false A[7] > A[8] = 40 > 29 // true i.e. swap the both element positions. A[6] > A[7] = 37 > 29 // true i.e. swap the both element positions. A[5] > A[6] = 30 > 29 // true i.e. swap the both element positions. A[4] > A[5] = 23 > 29 // false

Shell Sort in DS

Pass 3 is complete, and array is fully sorted.

Shell sort program in C language:

#include <stdio.h>
   // Shell sort void shellSort(int array[], int n) {   
  // Rearrange elements at each n/2, n/4, n/8, ... intervals 
  for (int gap = n / 2; gap > 0; gap /= 2) { 
    for (int i = gap; i < n; i += 1) {  
     int temp = array[i];  
     int j;     
  for (j = i; j >= gap && array[j - gap] > temp; j -= gap) { 
        array[j] = array[j - gap];   
    }     
  array[j] = temp;   
  } 
  }
 } 
  // Print an array 
void printArray(int array[], int size) {
   for (int i = 0; i < size; ++i) { 
    printf("%d  ", array[i]);   
} 
  printf("\n"); 
}   // Driver code 
int main() 
{  
 int data[] = {9, 8, 3, 7, 5, 6, 4, 1}; 
  int size = sizeof(data) / sizeof(data[0]);   
shellSort(data, size); 
  printf("Sorted array: \n"); 
  printArray(data, size); 
}

Related Topics

Check if a Singly Linked List is Palindrome

Check if a Singly Linked List is Palindrome In this section, we have given a singly linked list, and we need to check whether the given list is a palindrome. Example:           1...

3 minutes read.

Heap Sort vs Merge Sort

In this article, we are going to discuss the Heap Sort, Merge sort and the difference between them. What is Heap Sort? Heap – A heap is an abstract data type categorised...

7 minutes read.

Function to Create a Copy of Binary Search Tree

Implementation // creating a new hashmap in the language C++ that will help us clone a binary tree with arbitrary pointers.  #include<iostream> #include<unordered_map> using namespace std; /* A given binary tree has a record, a...

9 minutes read.

Detect and Remove Loop in a Linked List

Create a function called detectAndRemovetheLoop() that verifies whether a given Linked List has a loop, eliminates the loop if it does, and returns true if it does. It returns false...

6 minutes read.

Applications of trees in data structures

Data structures Storage used to organize and store data is known as the data structure. It is a method of managing computerized data to translate or retrieve it more efficiently. A...

7 minutes read.

Lowest Common Ancestor in a Binary Tree

The lowest node in the tree that contains both n1 and n2 as descendants is the lowest common ancestor (LCA), and n1 and n2 are the nodes for which we...

11 minutes read.

Tim Sort

Tim Sort is a mixture stable arranging calculation that exploits normal examples in information, and uses a mix of an improved Merge sort and Binary Insertion sort alongside an interior...

6 minutes read.

Bubble Sort vs Selection Sort

In this article, we will discuss the basic differences between these two sorting algorithms. Let us have a quick overview of what these sorting algorithms are? And what are the...

6 minutes read.

Binary Tree Implementation Using Arrays

Implementation Converting a binary tree into a list of arrays is one interesting problem. Let us see that in depth. In this section, we will see the implementation of the binary Trees...

4 minutes read.

Given Two Binary Trees, Check if it is Symmetric

Implementation // creating a C++ program that will help us check whether the two given trees are mirror images of each other.  #include<bits/stdc++.h> using namespace std; /* A given binary tree has a data...

5 minutes read.

Difference between Stack and Queue

In this article, we will learn about the major differences between Stack and Queue data structures. What is a stack? Stack – A stack is an abstract data structure defined as the...

3 minutes read.

Types of Data Structures

Almost every programme or software system that has been built makes use of data structures. Furthermore, data structures are basics of computer science and software engineering. When it comes to...

7 minutes read.

Pairwise swap elements of a given linked list

Pairwise swap elements of a given linked list In this problem, we have given a linked list, and we need to pairwise swap elements of the given linked list. Example:                                     Input:1 ->3...

4 minutes read.

Binary tree deletion

This article will discuss the deletion operation's implementation in the binary tree. The deletion operation helps us eliminate an element from the tree. Implementation #include <bits/stdc++.h> using namespace std; /* A binary tree node...

4 minutes read.

All About Minimum Cost Spanning Trees in Data Structure

Data management is called database management. This allows the computer to sort or organize the data for efficient retrieval. A data model is a system that stores, manages, and optimizes...

7 minutes read.

Traversal of binary tree

Traversal of binary tree: A node is visited only once in the traversal of the binary tree. There are three main types of traversal methods in the binary tree. In-order traversalPre-order...

3 minutes read.

Collision Resolution Techniques

Collision Resolution Techniques Collision in hashing In this, the hash function is used to compute the index of the array.The hash value is used to store the key in the hash table,...

2 minutes read.

Linear Queue VS Circular Queue

What is Queue? A queue is one of the important linear data structures extensively used in various computer applications. It is based on the FIFO (First In First Out) principle. It...

9 minutes read.

Assembly Line Scheduling

If we take an example of a car factory, there are two assembly lines. In an assembly line, we can assemble and repair the parts of a car. Now, suppose...

5 minutes read.

Convert a Binary Tree into a Binary Search Tree

Implementation #include <stdio.h>   #include <stdlib.h>       //creating a node of the binary tree.  struct __nod{       int record;       struct __nod *Lft;       struct __nod *Rt;   };       // presenting the root of the binary tree.   struct...

5 minutes read.