File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ */
426int 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
You can’t perform that action at this time.
0 commit comments