//binary search algorithm and its testing program#include<stdio.h>#include<stdlib.h>intBinarySearch(int*a,int low,int high,int tar);intmain(){int n;//the size of the arrayint tar;//the number to be foundint i;int index;//the index of the target numberprintf("Please input the size of the array : ");scanf("%d",&n);int* arr =(int*)malloc(sizeof(int)*n);printf("\n");printf("Please input the increasing array(with a space between two numbers) : ");for(i =0; i < n; i++){scanf("%d", arr + i);}printf("\n");printf("Please input the number to be found : ");scanf("%d",&tar);
index =BinarySearch(arr,0, n -1, tar);printf("\n");if(index ==-1|| index > n)printf("The number isn't found!\n");elseprintf("The index of the target number is : %d", index);return0;}intBinarySearch(int*a,int low,int high,int tar){int mid = low +(high - low)/2;//find the mediumif(a[mid]== tar)//found the number{return(mid +1);}elseif(low > high)//didn't found{return-1;}elseif(low <= high)//still in search{if(a[mid]< tar)//the target number is in the right part {BinarySearch(a, mid +1, high, tar);}elseif(a[mid]> tar)//the target number is in the left part {BinarySearch(a, low, mid, tar);}}}