If I pass a value greater than 100 as the second argument to BinaryInsertionSort, I get a segmentation fault.
int
BinarySearch (int a[], int low, int high, int key)
{
int mid;
if (low == high)
return low;
mid = low + ((high - low) / 2);
if (key > a[mid])
return BinarySearch (a, mid + 1, high, key);
else if (key < a[mid])
return BinarySearch (a, low, mid, key);
return mid;
}
void
BinaryInsertionSort (int a[], int n)
{
int ins, i, j;
int tmp;
for (i = 1; i < n; i++) {
ins = BinarySearch (a, 0, i, a[i]);
if (ins < i) {
tmp = a[i];
memmove (a + ins + 1, a + ins, sizeof (int) * (i - ins));
a[ins] = tmp;
}
}
}