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

Exponential Search More...

#include <assert.h>
#include <inttypes.h>
Include dependency graph for exponential_search.c:

Macros

#define ELEMENT   -10
 for assert for int64_t, uint16_t
 

Functions

int64_t binary_search (const int64_t *arr, const uint16_t l_index, const uint16_t r_index, const int64_t n)
 used to perform the binary search over the given array
 
int64_t exponential_search (const int64_t *arr, const uint16_t length, const int64_t n)
 used to perform the exponential search over the given array
 
static void test ()
 used to run the self-test implementations
 
int main ()
 Main function.
 

Detailed Description

Function Documentation

◆ binary_search()

int64_t binary_search ( const int64_t *  arr,
const uint16_t  l_index,
const uint16_t  r_index,
const int64_t  n 
)

used to perform the binary search over the given array

Function: binary_search.

algorithm that search the index of the given item

recursive function that search the given element in

the array using the <a href="https://github.com/TheAlgorithms/Algorithms-Explanation/blob/master/en/Search%20Algorithms/Binary%20Search.md" target="_blank" >Binary Search</a>

Parameters
arrarray where search the element
l_indexstart index of the array (arr) to apply the algorithm
r_indexend index of the array (arr) to apply the algorithm
nelement to find in the array (arr)
Returns
the index of the element (n) in the array (arr)
-1 if the n element wasn't found
56{
57 // calculate the middle index of the array
58 uint16_t middle_index = l_index + ( r_index - l_index ) / 2;
59 // base cases
60 if ( l_index > r_index ) { return -1; }
61 if ( arr[middle_index] == n ) { return middle_index; }
62 // recursion
63 if ( arr[middle_index] > n ) { return binary_search(arr, l_index, middle_index-1, n); } // left
64 return binary_search(arr, middle_index+1, r_index, n); // right
65}
int64_t binary_search(const int64_t *arr, const uint16_t l_index, const uint16_t r_index, const int64_t n)
used to perform the binary search over the given array
Definition: exponential_search.c:55
Here is the call graph for this function:

◆ exponential_search()

int64_t exponential_search ( const int64_t *  arr,
const uint16_t  length,
const int64_t  n 
)

used to perform the exponential search over the given array

Function: exponential_search.

algorithm that search the index of the given item

recursive function that take an array and quickly find the range

where to apply the binary search algorithm to find the given element

Parameters
arrarray where search the element
lengththe total length of the given array (arr)
nelement to find in the array (arr)
Returns
the index of the element (n) in the array (arr)
-1 if the element wasn't found
29{
30 if ( length == 0 ) { return -1; }
31 // find the upperbound
32 uint32_t upper_bound = 1;
33 while ( upper_bound <= length && arr[upper_bound] < n ) { upper_bound = upper_bound * 2; }
34 // calculate the range ( between lower_boud and upper_bound )
35 uint16_t lower_bound = upper_bound/2;
36 if ( upper_bound > length ) { upper_bound = length; }
37 // apply the binary search in the range
38 return binary_search(arr, lower_bound, upper_bound, n);
39}
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
72{
73 test(); // run self-test implementations
74 return 0;
75}
static void test()
used to run the self-test implementations
Definition: exponential_search.c:81
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

used to run the self-test implementations

Self-test implementations.

Returns
void
82{
83 // empty array
84 int64_t arr_empty[] = {};
85 assert(exponential_search(arr_empty, 0, 10) == -1);
86 // elent not found
87 int64_t arr_found[] = {1, 2, 3};
88 assert(exponential_search(arr_found, 3, 10) == -1);
89 // element found in an array of length 1
90 int64_t arr_one[] = {1};
91 assert(exponential_search(arr_found, 1, 1) == 0);
92 // find the first element in an array of length 2
93 int64_t arr_first_2[] = {1, 2};
94 assert(exponential_search(arr_first_2, 2, 1) == 0);
95 // find the last element in an array of length 2
96 int64_t arr_last_2[] = {1, 2};
97 assert(exponential_search(arr_last_2, 2, 2) == 1);
98 // find the first element in an array of length n
99 int64_t arr_first_n[] = {-1, 2, 4, 6, 8};
100 assert(exponential_search(arr_first_n, 5, -1) == 0);
101 // find the last element in an array of length n
102 int64_t arr_last_n[] = {-1, 2, 4, 6, 8};
103 assert(exponential_search(arr_last_n, 5, 8) == 4);
104 // find an element in an array of length n
105 int64_t arr_middle[] = {-1, 2, 4, 6, 8};
106 assert(exponential_search(arr_middle, 5, 6) == 3);
107}
int64_t exponential_search(const int64_t *arr, const uint16_t length, const int64_t n)
used to perform the exponential search over the given array
Definition: exponential_search.c:28
Here is the call graph for this function: