next up previous contents index Search
Next: 0.3.6 Shellsort Up: 0.3.5 Insertion Sort Previous: 0.3.5.2 Improvements

0.3.5.3 Source Code

Below is an Insertion Sort implemented in C:


void insert (int *array, int num, int x) {
  int i;

  for (i = n-1; i >= 0; i--) {
    if (x < array[i]) array[i+1] = a[i] else break;
  }
  array[i+1] = x;
}

void insertion_sort (int *array, int n) {
  int i;

  for (i = 0; i < n; i++) {
    insert(array, i, array[i]);
  }  
}

Scott Gasch
1999-07-09