1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
#include <stdio.h>
const int SIZE=9;
void TSearch(int arr[SIZE], int elem, int low, int high);
int main()
{
int arr[SIZE]={2,3,4,5,6,7,8,9,10};
int elem=7;
int low=0;
int high=SIZE-1;
TSearch(arr, elem, low, high);
getchar();
return 0;
}
void TSearch(int arr[SIZE], int elem, int low, int high)
{
if(elem<arr[low]||elem>arr[high])
{
printf("Element not present\n\n");
return;
}
if(low==high)
{
printf("Element at position %d\n\n",low);
return;
}
else if(elem<=arr[low+(high-low)/2]) TSearch(arr,elem,low,low+(high-low)/2);
else TSearch(arr,elem,low+((high-low)/2)+1,high);
}
|