1.
Definition
Sequential Search, also known as Linear Search, is a simple searching
algorithm where each element of a list or array is checked one by one
from the beginning until the desired element is found or the end of the list
is reached.
Time Complexity:
Best Case: O(1) (if the element is at the beginning)
Worst Case: O(n) (if the element is at the end or not present)
---
2. Real-Life Application
Example: Searching for a name in an attendance list
Imagine a teacher checking attendance by calling out each student's
name one by one and marking them present. This is exactly how
sequential search works—checking each element one after another until
the target is found.
---
3. Iterative Method Example in C
#include <stdio.h>
// Function to perform iterative sequential (linear) search
int sequentialSearchIterative(int arr[], int size, int key) {
// Loop through each element of the array
for (int i = 0; i < size; i++) {
// If current element matches the key, return its index
if (arr[i] == key)
return i;
}
// If key is not found, return -1
return -1;
}
int main() {
// Declare and initialize an array
int data[] = {12, 34, 56, 78, 90};
int size = sizeof(data) / sizeof(data[0]); // Calculate number of elements
in the array
int key = 56; // Element to search for
// Call the iterative search function
int result = sequentialSearchIterative(data, size, key);
// Check the result and print output
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
}
---
4. Recursive Method Example in C
#include <stdio.h>
// Function to perform recursive sequential (linear) search
int sequentialSearchRecursive(int arr[], int size, int key, int index) {
// Base Case 1: If index exceeds array size, element not found
if (index >= size)
return -1;
// Base Case 2: If current element matches the key
if (arr[index] == key)
return index;
// Recursive Case: Search in the remaining array
return sequentialSearchRecursive(arr, size, key, index + 1);
}
int main() {
// Declare and initialize an array
int data[] = {12, 34, 56, 78, 90};
int size = sizeof(data) / sizeof(data[0]); // Calculate number of elements
int key = 78; // Element to search
// Call the recursive search function starting from index 0
int result = sequentialSearchRecursive(data, size, key, 0);
// Check and print the result
if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");
return 0;
}