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 <stdio.h>
Include dependency graph for exponential_search.c:

Macros

#define ELEMENT   -10
 for assert
 

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

Macro Definition Documentation

◆ ELEMENT

#define ELEMENT   -10

for assert

for int64_t, uint16_t for printf

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
57{
58 // calculate the middle index of the array
59 uint16_t middle_index = l_index + ( r_index - l_index ) / 2;
60 // base cases
61 if ( l_index > r_index ) { return -1; }
62 if ( arr[middle_index] == n ) { return middle_index; }
63 // recursion
64 if ( arr[middle_index] > n ) { return binary_search(arr, l_index, middle_index-1, n); } // left
65 return binary_search(arr, middle_index+1, r_index, n); // right
66}
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:56
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
30{
31 if ( length == 0 ) { return -1; }
32 // find the upperbound
33 uint32_t upper_bound = 1;
34 while ( upper_bound <= length && arr[upper_bound] < n ) { upper_bound = upper_bound * 2; }
35 // calculate the range ( between lower_boud and upper_bound )
36 uint16_t lower_bound = upper_bound/2;
37 if ( upper_bound > length ) { upper_bound = length; }
38 // apply the binary search in the range
39 return binary_search(arr, lower_bound, upper_bound, n);
40}
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
73{
74 test(); // run self-test implementations
75 return 0;
76}
static void test()
used to run the self-test implementations
Definition exponential_search.c:82
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
83{
84 // empty array
85 int64_t arr_empty[] = { 0 };
86 assert(exponential_search(arr_empty, 0, 10) == -1);
87 // elent not found
88 int64_t arr_found[] = {1, 2, 3};
89 assert(exponential_search(arr_found, 3, 10) == -1);
90 // element found in an array of length 1
91 int64_t arr_one[] = {1};
92 assert(exponential_search(arr_found, 1, 1) == 0);
93 // find the first element in an array of length 2
94 int64_t arr_first_2[] = {1, 2};
95 assert(exponential_search(arr_first_2, 2, 1) == 0);
96 // find the last element in an array of length 2
97 int64_t arr_last_2[] = {1, 2};
98 assert(exponential_search(arr_last_2, 2, 2) == 1);
99 // find the first element in an array of length n
100 int64_t arr_first_n[] = {-1, 2, 4, 6, 8};
101 assert(exponential_search(arr_first_n, 5, -1) == 0);
102 // find the last element in an array of length n
103 int64_t arr_last_n[] = {-1, 2, 4, 6, 8};
104 assert(exponential_search(arr_last_n, 5, 8) == 4);
105 // find an element in an array of length n
106 int64_t arr_middle[] = {-1, 2, 4, 6, 8};
107 assert(exponential_search(arr_middle, 5, 6) == 3);
108
109 printf("All tests have successfully passed!\n");
110}
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:29
Here is the call graph for this function: