Home     Blog

Shell Sort

Invented by Donald Shell in 1959, Shell sort is the most efficient of the O(n2) class of sorting algorithms. It is also the most complex of the O(n2) algorithms. This algorithm is similar to bubble sort in the sense that it also moves elements by exchanges.

Shell sort begins by comparing elements that are at distance d. With this, the elements that are quite away from their place will move more rapidly than the simple bubble sort.

In each pass, the value of d is reduced to half.

In each pass, each element is compared with the element that is located d indexes away from it, and an exchange is made if required. The next iteration starts with a new value of d.

The algorithm terminates when d=1.

Example:

12

9

-10

22

2

35

40

Let us consider the starting value of d to be half the number of elements. d = n/2 = 7/2 = 3

shell sort clip image002 Shell Sort shell sort clip image004 Shell Sort shell sort clip image006 Shell Sort

Analysis of Shell Sort

  1. It is difficult to predict the complexity of Shell sort, as it is very difficult to show the effect of one pass on another pass.
  2. One thing is very clear that if the new distance d is computed using the above formula, then the number of passes will approximately be log2d since d=1 will complete the sort.
  3. Empirical studies have shown that the worst case complexity of Shell sort is O(n2).

C/C++ Implementation

.cf { font-family: Lucida Console; font-size: 9pt; color: black; background: white; } .cl { margin: 0px; } .cb1 { color: green; } .cb2 { color: blue; } .cb3 { color: maroon; }

void shellsort(int a[], int n)
{
 int d, temp, i;
 d=n/2;
 while (d>=1)
 {
 for (i=0;i<n-d;i++)
 {
 if (a[i]>a[i+d])
 {
 temp=a[i];
 a[i]=a[i+d];
 a[i+d]=temp;
 }
 }
 if(d==1)
 return;
 d=d/2.0+0.5;
 }
}

Complete Program

/**
 * Shell Sort
 * http://www.tech-faq.com
 */
 
#include <stdio.h>
 
void shellsort(int [], int );
 
int main()
{
 int a[50], n, i;
 printf("How many elements do you want to create the array with?"
 "(max 50): ");
 scanf("%d", &n);
 fflush(stdout);
 puts("Enter the array elements");
 for (i=0; i<n; i++)
 scanf("%d", &a[i]);
 shellsort(a, n);
 puts("--------Sorted Array--------");
 for (i=0; i<n; i++)
 printf("%dn", a[i]);
 printf("n");
 
 return 0;
}
 
void shellsort(int a[], int n)
{
 int d, temp, i;
 d=n/2;
 while (d>=1)
 {
 for (i=0;i<n-d;i++)
 {
 if (a[i]>a[i+d])
 {
 temp=a[i];
 a[i]=a[i+d];
 a[i+d]=temp;
 }
 }
 if(d==1)
 return;
 d=d/2.0+0.5;
 }
}

Output

How many elements do you want to create the array with?(max 50): 6 Enter the array elements 3 67 85 89 56 44 ——–Sorted Array——– 3 44 56 67 85 89

VN:F [1.9.17_1161]
Rating: 10.0/10 (1 vote cast)
Shell Sort, 10.0 out of 10 based on 1 rating
Follow Daniel Memetic on

Comments (2)

 

  1. Ananthakrishnan says:

    shell sorting doesnt work for 3, 1, 4, 2 d=2 3* 3 3 1 1 * 1 4* 4 4 2 2* 2 d=1 1* 1 1 1 3* 3* 3 3 4 4* 4* 2 2 2 2* 4 anytng wrong in it?

    VA:F [1.9.17_1161]
    Rating: 0.0/5 (0 votes cast)
    • Will.Spencer says:

      Ananthakrishnan:

      Could you rephrase your question more clearly?

       

      VN:F [1.9.17_1161]
      Rating: 0.0/5 (0 votes cast)

Leave a Reply

Related Posts

  • Insertion Sort

    This algorithm is very popular with bridge players when they sort their cards. In this procedure, we pick up a particular value and then insert it at the appropriate place in the sorted sub list. This algorithm requires n-1 passes. Pass1: a[1] is inserted either before or after a[0] so that a[0] and a[1] are [...]...


  • Bubble Sort

    It requires n-1 passes to sort an array. In each pass every element a[i] is compared with a[i+1], for i=0 to (n-k-1), where k is the pass number and if they are out of order i.e. if a[i]>a[i+1], they are swapped. This will cause the largest element to move up or bubble up. Thus after [...]...


  • Selection Sort

    Selection sort requires (n-1) passes to sort an array. In the first pass, we find the smallest element from a[0], a[1], a[2], … a[n-1] and swap it with the first element, i.e. a[0]. In the second pass, we find the smallest element from a[1], a[2], a[3]….a[n-1] and swap it with a[1] and so on. Pass1. [...]...


  • Quick Sort

    Quick sort is a divide-and-conquer sorting algorithm. To understand quick-sort, let us look at a high-level description of the algorithm. Divide: If the sequence S has 2 or more elements, select an element x from S to be your pivot. Any arbitrary element, like the last, will do. Remove all the elements of S and [...]...


  • Merge Sort

    Merging means combining elements of two arrays to form a new array. The simplest way of merging two arrays is to first copy all the elements of one array into a new array and then append all the elements of the second array to the new array. If you want the resultant array to be [...]...