编辑代码

#include<stdio.h>
//调用函数
 //采用折半查找算法进行
bool Binary_Search(int a[],int length ,int elemt){
	int start = 0;// 初始化起始位置为0
	int end = length -1;// 初始化结束位置为数组长度减1
	int mid;// 定义中间位置变量
	while(start<=end){ // 当起始位置小于等于结束位置时,继续查找
		mid  = (start+end)/2;
		if(a[mid] == elemt) {// 如果中间位置的元素等于要查找的元素,返回true
			return true;	
		}
		else if(a[mid]>elemt) {
			start  = mid +1;
		}else{// 如果中间位置的元素小于要查找的元素,更新起始位置为中间位置加1
			end = mid -1;
		}
		
	}
	 return false; 

} 

//主函数 
int main()
{	
	int a [] = {85, 63, 52, 34, 25, 17, 6};
	int length = sizeof(a)/sizeof(a[0]);
	int search_elemt = 6; 
	bool flat =  Binary_Search(a,length,search_elemt);
	if(flat) printf("查找成功元素为\t%d\n",search_elemt);
	else printf("不存在元素 %d\n",search_elemt);
	
	return 0;
	
 }