char BinarySearchChar(char[] list, char c, int low, int high) {
if (low > high)
return ' ';
if (high - low + 1 == 2) {
if (list[low] <= c && list[high] > c)
return list[high];
return list[0];
}
if (high - low + 1 == 3) {
if (list[low] == c)
return list[low + 1];
if (list[low + 1] == c)
return list[low + 2];
if (list[low] < c && list[low + 1] > c)
return list[low + 1];
if (list[low + 1] < c && list[low + 2] > c)
return list[low + 2];
// default case
return list[0];
}
int mid = low + high / 2;
if (list[mid] == c)
return list[mid + 1];
else if (list[mid - 1] < c && list[mid] > c)
return list[mid];
else if (list[mid] < c && list[mid + 1] > c)
return list[mid + 1];
else if (list[mid] > c)
return BinarySearchChar(list, c, 0, mid - 1);
else if (list[mid] < c)
return BinarySearchChar(list, c, mid + 1, high);
return list[0];
}
No comments:
Post a Comment