×

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

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.

Given a Binary Tree Print the Shortest Path

Implementation // Writing a program in C++ to find the shortest between the nodes i and j.  #include <bits/stdc++.h> using namespace std; // the given function will print the path between nodes i and...

7 minutes read.

Binary Search Tree

Binary Search Tree: A binary search tree is a type of tree in which every node is organized in the sorted order. It is also called an ordered binary tree. Properties...

4 minutes read.

Difference between complete and full binary tree

As we all know that the  binary tree is a tree it contains one or two children at each other node. It contains two children's nodes in the Binary tree. The...

6 minutes read.

2-3 Trees and Basic Operations on them

2-3 Trees, like any other AVL trees or B-trees, are just a type of Height Balanced Tree. 2-3 Trees are the B-trees of order 3. Like every other B-tree, the...

4 minutes read.

Cycle sort

Cycle sort is an examination arranging calculation which powers exhibit to be figured into the quantity of cycles where every one of them can be pivoted to create an arranged...

5 minutes read.

Balanced Binary Tree

A balanced binary tree is just a random nod-based tree with a rule of keeping its height minimum in size to maintain various operations such as insertions, deletions and several...

3 minutes read.

Linear vs Binary Search: Data Structure

Difference Between Linear and Binary Search What is Linear Search? A linear search also referred as a sequential search. It is a way to find an element within a list and it...

3 minutes read.

Given a Binary Tree, Check if it's balanced

Implementation /*Creating a C++ program that will help us identify whether the given tree is height-balanced or not.  */ #include <bits/stdc++.h> using namespace std; /* A particular binary tree node consists of data with some...

4 minutes read.

Insertion Sort vs Selection Sort

In this article, we will discuss insertion sort, Selection sort and the basic differences between these two sorting techniques in detail: What is Insertion Sort? Insertion Sort – The insertion sort is...

5 minutes read.

Counts the number of times a given element occurs in a Linked List

Counts the number of times a given element occurs in a Linked List This article will explain how we can count the occurrences of a particular element in a list. Here,...

3 minutes read.

Doubly Linked List

Doubly Linked List Doubly linked list is another kind of Linked list. Doubly linked list contains two pointers for navigation. In this, we can traverse the list in both directions, either...

4 minutes read.

Hashing

Hashing: Hashing is a process in which a large amount of data is mapped to a small table with the help of hashing function. It is a searching technique. Hash table We...

4 minutes read.

Binary Tree in Data Structures

What is a Binary Tree in Data Structures? The term binary itself means bi, which implies two of anything. So very clearly, we know we present the trees in the form...

6 minutes read.

Does Overloading Work with Inheritance

This is a question that occasionally comes to many programmers. Who are curious to know more now has a complete explanation and a solution through this tutorial! Inheritance: The functions of...

3 minutes read.

Inorder Successor in Binary Trees

The next node in the Inorder traversal of a binary tree is known as Inorder successor of that particular node. In a Binary Search Tree, the definition of Inorder successor can...

9 minutes read.

Radix Sort

Radix Sort: The radix sort is a non-comparative integer sorting algorithm that sorts the elements by grouping the individual digits of the same location. It shares the same significant position...

4 minutes read.

Bubble Sort vs Heap Sort

In this article, we are going to compare the two most common sorting techniques, Bubble Sort and Heap sort. Before discussing their differences, let us first discuss the idea of...

7 minutes read.

Reverse the Singly Linked List in C

Reverse the Singly Linked List in C This article has given a singly linked list and will reverse the linked list by changing the links between nodes. Example:                         Input:  2 -> 4...

3 minutes read.

Stack vs Array

Difference between Array and Stack In this article, we are going to discuss the major differences between the stack and array data structures: Array – In the data structure, the array is...

3 minutes read.