[go: up one dir, main page]

0% found this document useful (0 votes)
29 views5 pages

Sequential Search

Sequential Search, or Linear Search, is an algorithm that checks each element of a list until the desired element is found, with a best-case time complexity of O(1) and a worst-case of O(n). It can be implemented iteratively or recursively, as demonstrated in C code examples. A real-life application of this search method is a teacher calling out names on an attendance list to find a specific student.

Uploaded by

garvita4321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views5 pages

Sequential Search

Sequential Search, or Linear Search, is an algorithm that checks each element of a list until the desired element is found, with a best-case time complexity of O(1) and a worst-case of O(n). It can be implemented iteratively or recursively, as demonstrated in C code examples. A real-life application of this search method is a teacher calling out names on an attendance list to find a specific student.

Uploaded by

garvita4321
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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;
}

You might also like