C语言二分法查找数组

代码不是我滴也算是我滴是久久/九九的,这是久久大一2016年写的.....

#include
int fun(int a[],int num,int n)
{
int left=0;
int right=n-1;
int mid=(left+right)/2;


while (1)
{
if (num==a[left])
return left;


if (num==a[mid])
return mid;


if (num==a[right])
return right;


if (mid==right||mid==left)
return -1;


if (num>a[mid])
{
left=mid;
mid=(right+left)/2;
}
else if (num {
right=mid;
mid=(right+left)/2;
}
}
}
int main()
{
int num=0;
int a[]={1,5,5,6,7,8,9,10};
int n=sizeof(a)/sizeof(int);
int key=0;


printf("请输入您需要查找的数:");
scanf("%d",&num);


key=fun(a,num,n);


printf("%d\n",key);


return 0;
}
----------------------------------------------------------2019  年  的我早就会写二分查找了,,,,

#include
int mid_search(int *a,int v,int len)
{
	int right=len-1;
	int left=0;
	int mid=(right+left)/2;
	while(left=0)
	{
		if(v>a[mid])
		{
			left=mid+1;
			mid=(left+right)/2;
		}
		else if(v==a[mid])
		{
			printf("find! value at:%d",mid);
			return 1;
		}
		else
		{
			right=mid-1;
			mid=(left+right)/2;
		}
	}
	return 0;
}
int main()
{
	int a[]={1,2,3,4,5,6,7,8};
	int ret=mid_search(a,1,8);
	printf("\n%d\n",ret);
}

 

你可能感兴趣的:(不开心,c语言)