面试题3

二次信任 提交于 2020-03-11 12:55:30
#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;
}

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!