-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search_using_pointers.c
50 lines (49 loc) · 1.6 KB
/
binary_search_using_pointers.c
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <stdio.h>
#include <stdlib.h>
int BinarySearch(int list[], int *endPtr, int target, int **locPtr);
int main()
{
int a[]= {10,20,30,40,50};
int n=5;
int *loc; //Pointer to hold the address of element, if found
int target;
int *endPtr = a + n-1;
printf("Array Elements :\n");
for(int *p=a; p<=endPtr; p++)
printf("%p = %d\n",p,*p);
printf("Enter value to be searched :");
scanf("%d",&target);
//function call
/*Address of the pointer variable, loc is sent to function
changes to this is reflected back in the main function
*/
int flag = BinarySearch(a, endPtr, target, &loc);
if (flag)
printf("Element found at %X",loc);
else
printf("Element Not Found");
}
int BinarySearch(int list[], int *endPtr, int target, int **locPtr)
{
/* locPtr is a pointer to pointer and receives the address of pointer variable loc from main and
returns address of element in loc, if found
locPtr --->loc---> searching element in array
*/
int *firstPtr = list;
int *lastPtr = endPtr;
int *midPtr = NULL;
int no_of_elements, midIndex;
while(firstPtr<=lastPtr)
{
//find the number of elements between last and first pointers
no_of_elements = (lastPtr - firstPtr+1);
midIndex = no_of_elements/2; // find the index of mid element
midPtr = firstPtr + midIndex; //Point to the mid element
if (target > *midPtr) firstPtr = midPtr + 1;
else if (target< *midPtr) lastPtr = midPtr - 1;
else
break;
}
*locPtr = midPtr; //
return (*midPtr == target); //return 1 or 0
}