Source of shellSort.cpp


  1: //  Created by Frank M. Carrano and Timothy M. Henry.
  2: //  Copyright (c) 2017 Pearson Education, Hoboken, New Jersey.

  4: // Programming Problem 8.

  6: template <class ItemType>
  7: void shellSort(ItemType theArray[], int n)
  8: {
  9:    for (int h = n / 2; h > 0; h = h / 2)
 10:    {
 11:       for (int unsorted = h; unsorted < n; unsorted++)
 12:       {
 13:          ItemType nextItem = theArray[unsorted];
 14:          int loc = unsorted;
 15:          while ( (loc >= h) && (theArray[loc – h] > nextItem) )
 16:          {
 17:             theArray[loc] = theArray[loc - h];
 18:             loc = loc − h;
 19:          }  // end while
 20:          theArray[loc] = nextItem;
 21:       }  // end for
 22:    }  // end for
 23: }  // end shellSort