1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
|
int IndexFileBinarySearch (indexRecord array[], int &low, int &high, char key[])
{
int mid;
char temp[21];
memset (temp, '\0', 21);
strcpy (temp, key);
if (low == 0 && high == 0)
return 0;
mid = low + ((high - low) / 2);
if (array[mid].key[0] == '\0') // null result
{
printf ("\nSearch failed.\n");
return -1;
}
printf ("\nYou want to search for key = %s. I am now at Key %d.\n", temp, mid);
if (strcmp (temp, array[mid].key) == 0)
{
printf ("Comparison found.\n");
printf ("\nKey is: %s (%d)", array[mid].key, mid);
char c = getchar(); // to "step through" manually
return mid;
}
else if (strcmp (temp, array[mid].key) > 0)
{
printf ("\nSearching upper");
printf ("\nCurrent median key is: %s (%d)", array[mid].key, mid);
char c = getchar(); // to "step through" manually
return IndexFileBinarySearch (array, mid + 1, high, temp);
}
else if (strcmp (temp, array[mid].key) < 0)
{
printf ("\nSearching lower");
printf ("\nCurrent median key is: %s (%d)", array[mid].key, mid);
char c = getchar(); // to "step through" manually
return IndexFileBinarySearch (array, low, mid, temp);
}
else printf ("\nWah, it doesn't seem to be there.");
}
|