This commit is contained in:
parent
16ed3d024a
commit
a8e225cbbb
|
@ -0,0 +1,33 @@
|
|||
#include <stdio.h>
|
||||
|
||||
#define SIZE 55
|
||||
|
||||
// Search complexity is O(n)
|
||||
// bc growths in respect to input, if array is 999
|
||||
// in the worst case the search will take 999 iterations over the loop
|
||||
// if array is 4, the worst case will take 4 loops
|
||||
|
||||
int linear_search(int array[], int size, int value)
|
||||
{
|
||||
for (int i = 0; i < size; ++i)
|
||||
if (array[i] == value)
|
||||
return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int array[SIZE];
|
||||
for (int i = 0; i < SIZE; ++i)
|
||||
array[i] = 0;
|
||||
|
||||
array[3] = 45;
|
||||
int value = 45;
|
||||
|
||||
int is_found = linear_search(array, SIZE, value);
|
||||
printf("1 if found: %d\n", is_found);
|
||||
|
||||
is_found = linear_search(array, SIZE, 99);
|
||||
printf("1 if found: %d\n", is_found);
|
||||
}
|
Loading…
Reference in New Issue