Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
binary_search.c File Reference

Program to perform binary search of a target value in a given sorted array. More...

#include <assert.h>
#include <stdio.h>
Include dependency graph for binary_search.c:

Functions

int binarysearch1 (const int *arr, int l, int r, int x)
 Recursive implementation. More...
 
int binarysearch2 (const int *arr, int l, int r, int x)
 Iterative implementation. More...
 
void test ()
 Test implementations. More...
 
int main (void)
 Main function. More...
 

Detailed Description

Program to perform binary search of a target value in a given sorted array.

Authors
James McDermott - recursive algorithm
Krishna Vedala - iterative algorithm

Function Documentation

◆ binarysearch1()

int binarysearch1 ( const int *  arr,
int  l,
int  r,
int  x 
)

Recursive implementation.

Parameters
[in]arrarray to search
lleft index of search range
rright index of search range
xtarget value to search for
Returns
location of x assuming array arr[l..r] is present
-1 otherwise
22{
23 if (r >= l)
24 {
25 int mid = l + (r - l) / 2;
26
27 // If element is present at middle
28 if (arr[mid] == x)
29 return mid;
30
31 // If element is smaller than middle
32 if (arr[mid] > x)
33 return binarysearch1(arr, l, mid - 1, x);
34
35 // Else element is in right subarray
36 return binarysearch1(arr, mid + 1, r, x);
37 }
38
39 // When element is not present in array
40 return -1;
41}
int binarysearch1(const int *arr, int l, int r, int x)
Recursive implementation.
Definition: binary_search.c:21
Here is the call graph for this function:

◆ binarysearch2()

int binarysearch2 ( const int *  arr,
int  l,
int  r,
int  x 
)

Iterative implementation.

Parameters
[in]arrarray to search
lleft index of search range
rright index of search range
xtarget value to search for
Returns
location of x assuming array arr[l..r] is present
-1 otherwise
52{
53 int mid = l + (r - l) / 2;
54
55 while (arr[mid] != x)
56 {
57 if (r <= l || r < 0)
58 return -1;
59
60 if (arr[mid] > x)
61 // If element is smaller than middle
62 r = mid - 1;
63 else
64 // Else element is in right subarray
65 l = mid + 1;
66
67 mid = l + (r - l) / 2;
68 }
69
70 // When element is not present in array
71 return mid;
72}

◆ main()

int main ( void  )

Main function.

106{
107 test();
108 return 0;
109}
void test()
Test implementations.
Definition: binary_search.c:75
Here is the call graph for this function:

◆ test()

void test ( void  )

Test implementations.

76{
77 // give function an array to work with
78 int arr[] = {2, 3, 4, 10, 40};
79 // get size of array
80 int n = sizeof(arr) / sizeof(arr[0]);
81
82 printf("Test 1.... ");
83 // set value to look for
84 int x = 10;
85 // set result to what is returned from binarysearch
86 int result = binarysearch1(arr, 0, n - 1, x);
87 assert(result == 3);
88 printf("passed recursive... ");
89 result = binarysearch2(arr, 0, n - 1, x);
90 assert(result == 3);
91 printf("passed iterative...\n");
92
93 printf("Test 2.... ");
94 x = 5;
95 // set result to what is returned from binarysearch
96 result = binarysearch1(arr, 0, n - 1, x);
97 assert(result == -1);
98 printf("passed recursive... ");
99 result = binarysearch2(arr, 0, n - 1, x);
100 assert(result == -1);
101 printf("passed iterative...\n");
102}
int binarysearch2(const int *arr, int l, int r, int x)
Iterative implementation.
Definition: binary_search.c:51
Here is the call graph for this function: