diff --git a/The Last Algorithms Course You'll Need/3. Linear Search.c b/The Last Algorithms Course You'll Need/3. Linear Search.c new file mode 100644 index 0000000..d6b94e9 --- /dev/null +++ b/The Last Algorithms Course You'll Need/3. Linear Search.c @@ -0,0 +1,33 @@ +#include + +#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); +}