Herong's Tutorial Notes on Sorting
Dr. Herong Yang, Version 5.03

Shell Sort

This chapter helps you to understand:

  • Basic Idea
  • Java Implementation
  • Performance
  • Improvements

Basic Idea

The basic idea of Shell Sort, invented by D.L. Shell is to:

1. Set a step size h that is smaller than the number of elements to be sorted, and greater that 1.

2. Group the entire collection of data elements in h groups by putting elements that are h steps away from each other into the same group.

3. Sort each group by exchanging locations of elements in the same group.

4. Repeat step 2 and 3 with a smaller step size h until h becomes 1.

This idea of sorting is based on the following facts:

  • Sorting h groups of n/h elements in each group takes much less time than sorting n elements.
  • If a collection is sorted with a step size h, it is called partially sorted, because any elements will be at most n-h steps away from it's final sorted position.
  • If a collection is sorted with a step size h, the insertion sort will be much more efficient than the original collection.
  • Insertion sort method should be used for each sorting step.
  • The performance of this sorting method is strongly dependending on the selection of grouping step sizes used in the sorting steps. One of the popular methods of selecting the grouping step sizes is to use this sequence: h(n+1) = 3*h(n) + 1.

Java Implementation

Here is my Java implementation of Shell Sort algorithm:

/**
 * HyArrays.java
 * This class contains sorting methods similar to java.util.Arrays.
 * All sorting methods should have a signiture of 
 * %Sort(Object[] a, int fromIndex, int toIndex)
 * where "fromIndex" is inclusive, and "toIndex" is exclusive.
 * Copyright (c) 1999 by Dr. Herong Yang
 */
   public static void shellSort(Object[] a, int fromIndex, 
      int toIndex) {
      int h = 1; 
      while (h<toIndex-fromIndex) h = 3*h + 1;
      while (h>1) {
         h = h/3;
         for (int i=fromIndex+h; i<toIndex; i++) {
            Comparable d = (Comparable)a[fromIndex+i];
            int j = i; 
            while (j>=fromIndex+h && d.compareTo(a[fromIndex+j-h])<0) {
               a[fromIndex+j] = a[fromIndex+j-h];
               j = j - h;
            }
            a[fromIndex+j] = d;
         }
      }
   }

Note that:

  • If you replace h by 1, the code should identical to the insertion sort.

Performance

Now let's see how it performs. I tried this implementation with my SortTest.java under JDK 1.3.1. Here are the results:

Array size: 10000
Average sorting time: 25 milliseconds
Number of tests: 1000
Performance: 2.5 O(N) nonaseconds
Performance: 0.18814374728998823 O(N*Log2(N)) nonaseconds
Performance: 2.5E-4 O(N*N) nonaseconds

Array size: 20000
Average sorting time: 81 milliseconds
Number of tests: 1000
Performance: 4.05 O(N) nonaseconds
Performance: 0.28346035337307884 O(N*Log2(N)) nonaseconds
Performance: 2.025E-4 O(N*N) nonaseconds

Array size: 30000
Average sorting time: 148 milliseconds
Number of tests: 1000
Performance: 4.933333333333334 O(N) nonaseconds
Performance: 0.3317045099854675 O(N*Log2(N)) nonaseconds
Performance: 1.6444444444444444E-4 O(N*N) nonaseconds

As you can, the performance of this Shell Sort is much better than Insertion sort. But it is still not good as Quicksort.

Improvements

You can play with the step sizes.

Dr. Herong Yang, updated in 2007
Herong's Tutorial Notes on Sorting - Shell Sort