Skip to content

Commit ceebb29

Browse files
authored
docs: add documentation for linear search
Added documentation and clarifying comments to the linear search example (no functional/algorithmic changes)
1 parent e5dad3f commit ceebb29

1 file changed

Lines changed: 25 additions & 2 deletions

File tree

searching/linear_search.c

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,34 @@
1+
/* Linear Search Algorithm
2+
*
3+
* This program demonstrates the linear search technique, where each element
4+
* of the array is checked sequentially until the target value is found.
5+
*
6+
* Time Complexity:
7+
* - Best case: O(1)
8+
* - Worst case: O(n)
9+
*
10+
* Space Complexity:
11+
* - O(1)
12+
*
13+
* Works on both sorted and unsorted arrays.
14+
*/
115
#include <stdio.h>
216
#include <stdlib.h>
3-
17+
/*
18+
* Performs linear search on an integer array.
19+
*
20+
* @param arr Pointer to the array
21+
* @param size Number of elements in the array
22+
* @param val Value to search for
23+
*
24+
* @return 1 if the value is found, otherwise 0
25+
*/
426
int linearsearch(int *arr, int size, int val)
527
{
628
int i;
729
for (i = 0; i < size; i++)
830
{
31+
// Compare each element with the target value
932
if (arr[i] == val)
1033
return 1;
1134
}
@@ -22,7 +45,7 @@ int main()
2245
printf("Enter the contents for an array of size %d:\n", n);
2346
for (i = 0; i < n; i++)
2447
scanf("%d", &a[i]); // accepts the values of array elements until the
25-
// loop terminates//
48+
// loop terminates
2649

2750
printf("Enter the value to be searched:\n");
2851
scanf("%d", &v); // Taking input the value to be searched

0 commit comments

Comments
 (0)