用函数写一个整型有序数组二分查找

时间:2022-12-05 15:55:57

#define _CRT_SECURE_NO_WARNINGS 1

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<math.h>

int binary_search(int a[],int k,int s)

{

 int left = 0;

 int right = s-1;

 while(left <= right)

 {

 int mid = (left + right)/2;

  if(a[mid]>k)

  {

    right = mid -1;


  }

  else if(a[mid]<k)

  {

    left = mid+1;

   

  }

  else

   return mid;

 }

 return -1;

}

int main()

{  

int arr[] = {1,2,3,4,5,6,7,8,9,10};

int key = 17;

int sz = sizeof(arr) /sizeof(arr[0]);

//找到了就返回找到的位置的下标,找不到返回-1

   int ret = binary_search(arr,key,sz);


  if(-1 == ret)

  {

    printf("找不到\n");

   

   }

  else

  {

     printf("找到了,下标是:%d\n",ret);

   

  }

 

 return 0;

}