next up previous contents index Search
Next: 0.3.7 Selection Sort Up: 0.3.6 Shellsort Previous: 0.3.6.1 Analysis

0.3.6.2 Source Code

Below is a C implementation of the Shellsort:


void insertionsort (int *A, int n, int incr) {
  int i, j;

  for (i = incr; i < n; i+= incr)
    for (j = i; (j >= incr) && (A[j] < A[j-incr]); j-=incr)
      swapi (A[j], A[j-incr]);
}


void shellsort (int *array, int n) {
  int i, j;

  for (i = (n/2); i > 1; i /= 2)
    for (j = 0; j < i; j++)
      insertionsort(\&array[j], n-j, i);

  insertionsort(array, n, 1);
}

Scott Gasch
1999-07-09