Skip to content

Commit 89f1123

Browse files
Merge pull request Pradeepsingh61#498 from rishanmenezes/feature/linear-search-implementation
Add Comprehensive Linear Search Algorithm Implementation in C
2 parents feeee31 + da13d53 commit 89f1123

1 file changed

Lines changed: 256 additions & 0 deletions

File tree

Lines changed: 256 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,256 @@
1+
/*
2+
* Algorithm Name:
3+
* Linear Search (Sequential Search)
4+
*
5+
* Programming Language:
6+
* C
7+
*
8+
* Category:
9+
* Searching
10+
*
11+
* Difficulty Level:
12+
* Easy
13+
*
14+
* Algorithm Description:
15+
*
16+
* 1. Problem it solves:
17+
* -> Linear Search finds the position of a target element in an array by checking
18+
* each element sequentially from the beginning until the target is found or
19+
* the end of the array is reached.
20+
*
21+
* 2. Approach / Idea:
22+
* -> Start from the first element of the array
23+
* -> Compare each element with the target value
24+
* -> If match found, return the index
25+
* -> If end of array reached without finding target, return -1
26+
* -> Works on both sorted and unsorted arrays
27+
*
28+
* 3. Complexity:
29+
* Time: O(n) - worst case when element is at end or not present
30+
* Space: O(1) - constant extra space
31+
*
32+
* 4. Variants Implemented:
33+
* -> Basic Linear Search
34+
* -> Linear Search with All Occurrences
35+
* -> Recursive Linear Search
36+
* -> Linear Search from End (Reverse)
37+
*
38+
* Author:
39+
* Rishan Menezes
40+
*/
41+
42+
#include <stdio.h>
43+
#include <stdlib.h>
44+
45+
// Function prototypes
46+
int linearSearch(int arr[], int n, int target);
47+
int* linearSearchAllOccurrences(int arr[], int n, int target, int* count);
48+
int linearSearchRecursive(int arr[], int n, int target, int index);
49+
int linearSearchReverse(int arr[], int n, int target);
50+
void printArray(int arr[], int n);
51+
52+
int main() {
53+
int choice, n, target, result;
54+
55+
printf("=== Linear Search Algorithm Demo ===\n\n");
56+
57+
// Get array size
58+
printf("Enter the number of elements: ");
59+
scanf("%d", &n);
60+
61+
if (n <= 0) {
62+
printf("Invalid array size!\n");
63+
return 1;
64+
}
65+
66+
int arr[n];
67+
68+
// Input array elements
69+
printf("Enter %d elements: ", n);
70+
for (int i = 0; i < n; i++) {
71+
scanf("%d", &arr[i]);
72+
}
73+
74+
printf("\nArray: ");
75+
printArray(arr, n);
76+
77+
printf("\nEnter the element to search: ");
78+
scanf("%d", &target);
79+
80+
// Menu for different search variants
81+
printf("\nChoose search variant:\n");
82+
printf("1. Basic Linear Search\n");
83+
printf("2. Find All Occurrences\n");
84+
printf("3. Recursive Linear Search\n");
85+
printf("4. Reverse Linear Search\n");
86+
printf("Enter your choice (1-4): ");
87+
scanf("%d", &choice);
88+
89+
printf("\n=== Results ===\n");
90+
91+
switch (choice) {
92+
case 1: {
93+
result = linearSearch(arr, n, target);
94+
if (result != -1) {
95+
printf("Element %d found at index %d (position %d)\n", target, result, result + 1);
96+
} else {
97+
printf("Element %d not found in the array\n", target);
98+
}
99+
break;
100+
}
101+
102+
case 2: {
103+
int count = 0;
104+
int* indices = linearSearchAllOccurrences(arr, n, target, &count);
105+
106+
if (count > 0) {
107+
printf("Element %d found %d time(s) at indices: ", target, count);
108+
for (int i = 0; i < count; i++) {
109+
printf("%d", indices[i]);
110+
if (i < count - 1) printf(", ");
111+
}
112+
printf("\n");
113+
free(indices);
114+
} else {
115+
printf("Element %d not found in the array\n", target);
116+
}
117+
break;
118+
}
119+
120+
case 3: {
121+
result = linearSearchRecursive(arr, n, target, 0);
122+
if (result != -1) {
123+
printf("Element %d found at index %d (position %d) using recursion\n", target, result, result + 1);
124+
} else {
125+
printf("Element %d not found in the array using recursion\n", target);
126+
}
127+
break;
128+
}
129+
130+
case 4: {
131+
result = linearSearchReverse(arr, n, target);
132+
if (result != -1) {
133+
printf("Element %d found at index %d (position %d) searching from end\n", target, result, result + 1);
134+
} else {
135+
printf("Element %d not found in the array searching from end\n", target);
136+
}
137+
break;
138+
}
139+
140+
default:
141+
printf("Invalid choice!\n");
142+
return 1;
143+
}
144+
145+
return 0;
146+
}
147+
148+
/**
149+
* Basic Linear Search
150+
* Searches for target element from beginning to end
151+
*
152+
* @param arr: Array to search in
153+
* @param n: Size of the array
154+
* @param target: Element to search for
155+
* @return: Index of element if found, -1 otherwise
156+
*/
157+
int linearSearch(int arr[], int n, int target) {
158+
for (int i = 0; i < n; i++) {
159+
if (arr[i] == target) {
160+
return i; // Return index where element is found
161+
}
162+
}
163+
return -1; // Element not found
164+
}
165+
166+
/**
167+
* Linear Search - Find All Occurrences
168+
* Finds all positions where target element appears
169+
*
170+
* @param arr: Array to search in
171+
* @param n: Size of the array
172+
* @param target: Element to search for
173+
* @param count: Pointer to store number of occurrences found
174+
* @return: Dynamically allocated array of indices where element is found
175+
*/
176+
int* linearSearchAllOccurrences(int arr[], int n, int target, int* count) {
177+
int* indices = (int*)malloc(n * sizeof(int)); // Worst case: all elements match
178+
*count = 0;
179+
180+
for (int i = 0; i < n; i++) {
181+
if (arr[i] == target) {
182+
indices[*count] = i;
183+
(*count)++;
184+
}
185+
}
186+
187+
if (*count == 0) {
188+
free(indices);
189+
return NULL;
190+
}
191+
192+
// Resize array to actual count to save memory
193+
indices = (int*)realloc(indices, (*count) * sizeof(int));
194+
return indices;
195+
}
196+
197+
/**
198+
* Recursive Linear Search
199+
* Implements linear search using recursion
200+
*
201+
* @param arr: Array to search in
202+
* @param n: Size of the array
203+
* @param target: Element to search for
204+
* @param index: Current index being checked (starts from 0)
205+
* @return: Index of element if found, -1 otherwise
206+
*/
207+
int linearSearchRecursive(int arr[], int n, int target, int index) {
208+
// Base case: reached end of array
209+
if (index >= n) {
210+
return -1;
211+
}
212+
213+
// Base case: element found
214+
if (arr[index] == target) {
215+
return index;
216+
}
217+
218+
// Recursive case: search in remaining array
219+
return linearSearchRecursive(arr, n, target, index + 1);
220+
}
221+
222+
/**
223+
* Reverse Linear Search
224+
* Searches for target element from end to beginning
225+
* Useful when target is more likely to be at the end
226+
*
227+
* @param arr: Array to search in
228+
* @param n: Size of the array
229+
* @param target: Element to search for
230+
* @return: Index of element if found, -1 otherwise
231+
*/
232+
int linearSearchReverse(int arr[], int n, int target) {
233+
for (int i = n - 1; i >= 0; i--) {
234+
if (arr[i] == target) {
235+
return i; // Return index where element is found
236+
}
237+
}
238+
return -1; // Element not found
239+
}
240+
241+
/**
242+
* Utility function to print array elements
243+
*
244+
* @param arr: Array to print
245+
* @param n: Size of the array
246+
*/
247+
void printArray(int arr[], int n) {
248+
printf("[");
249+
for (int i = 0; i < n; i++) {
250+
printf("%d", arr[i]);
251+
if (i < n - 1) {
252+
printf(", ");
253+
}
254+
}
255+
printf("]\n");
256+
}

0 commit comments

Comments
 (0)