#include <stdio.h>
/// 1 4 7
/// ret -1 前查找
/// ret 0 找到
/// ret -1 后查找
int compare(const void* src, const void* tar)
{
int data = *((int*)src);
}
typedef int (*compare)(const void*, const void*);
int find(const void* arry[], const int count, const int size,
compare com_func, const void *target)
{
void* src;
int head, tail, mid, ret;
head = 0;
tail = (count - 1);
while (head < tail) {
mid = ((head + tail) / 2);
src = arry + (mid * size);
ret = com_func(src, target);
if (ret == 0)
return mid;
else if (ret < 0)
tail = mid;
else
head = mid;
}
if (head == tail) {
src = arry + (head * size);
ret = com_func(src, target);
if (ret == 0)
return head;
}
return -1;
}
来源:CSDN
作者:guoz_li
链接:https://blog.csdn.net/li7032/article/details/104793031