-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicLinearSearch.c
36 lines (30 loc) · 937 Bytes
/
BasicLinearSearch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
/**
* Performs linear search on an array to find the target element.
*
* Time Complexity: O(n), where n is the number of elements in the array.
* Linear search checks each element in the array sequentially until the target element is found or the end of the array is reached.
*
* @param array : The array to search in.
* @param size : The size of the array.
* @param checkElement : The element to search for within the array.
*
* @return : The index of the found element if present, otherwise -1.
*/
int linearSearch(int array[], int size, int checkElement);
int main()
{
return 0;
}
int linearSearch(int array[], int size, int checkElement)
{
int l_index = 0;
for (l_index = 0; l_index < size; l_index++)
{
if (array[l_index] == checkElement)
{
return l_index; // return the index of the element in the array
}
}
return -1;
}