Shell Sort subdivides the array of structures to be sorted into smaller pieces by selecting every nth record and then sorting that group of sturctures. By repeatedly reducing the value of n until it diminishes to 1 the array becomes sorted. The efficiency of Shell Sort comes from the fact that it is able to move structures which are greatly offset from their final location rapidly with few key comparisons.
/***************************************/ /* ShellSort() */ /* */ /* Sort records on integer key using */ /* a shell sort. */ /***************************************/ void ShellSort(StructType DataArray[], int count) { int i, delta; delta = count; do { delta = 1 + delta / 3; for(i=0; i<delta; i++) DeltaInsertionSort(DataArray, i, delta, count); } while(delta > 1); } /***************************************/ /* DeltaInsertionSort() */ /* */ /* Sort subarrays of records on integer*/ /* key using selection sort. */ /***************************************/ void DeltaInsertionSort(StructType DataArray[], int I, int Delta, int count) { int j, k; int key; int NotDone; StructType temp; j = I + Delta; while(j < count) { key = DataArray[j].key; /* Get next key to sort */ temp = DataArray[j]; /* Remove and hold */ /* Do insertion sort on this key in the block of delta records */ /* Move each struct where DataArray[j].key > key to the right */ /* by delta spaces and insert the key there. */ k = j; NotDone = TRUE; do { if(DataArray[k - Delta].key <= key) NotDone = FALSE; /* Terminate the loop */ else { DataArray[k] = DataArray[k - Delta]; k -= Delta; if(k==I) NotDone = FALSE; } } while(NotDone); /* Insert the moved key */ DataArray[k] = temp; /* Get next key to insert--one full delta increment to the right */ j += Delta; } }