0% found this document useful (0 votes)
5 views

Searching Algorithm

The document provides an overview of Linear and Binary Search algorithms, detailing their procedures, complexity analyses, advantages, and disadvantages. Linear Search is simple and works on both sorted and unsorted data but is inefficient for large datasets, while Binary Search is faster and efficient for large sorted datasets but requires the data to be sorted. The document includes code examples and complexity analysis for both algorithms, highlighting their best, worst, and average case scenarios.
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)
5 views

Searching Algorithm

The document provides an overview of Linear and Binary Search algorithms, detailing their procedures, complexity analyses, advantages, and disadvantages. Linear Search is simple and works on both sorted and unsorted data but is inefficient for large datasets, while Binary Search is faster and efficient for large sorted datasets but requires the data to be sorted. The document includes code examples and complexity analysis for both algorithms, highlighting their best, worst, and average case scenarios.
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/ 4

Linear Search Algorithm with Complexity Analysis

Linear Search is a simple and straightforward searching algorithm that scans each element of a list sequentially until
the target element is found or the end of the list is reached. It is also known as a sequential search.
Algorithm (Linear Search): The linear search algorithm follows these steps:

1. Start at the first element of the array.


2. Compare each element with the target element.
3. If a match is found, return the index of the element.
4. If the end of the array is reached without finding the target, return -1.

#include <stdio.h>

// Function to perform Linear Search


int linearSearch(int arr[], int n, int target) {
for (int i = 0; i < n; i++) {
if (arr[i] == target) {
return i; // Element found, return index
}
}
return -1; // Element not found
}

int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 30;

int result = linearSearch(arr, n, target);

if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");

return 0;
}
Complexity Analysis of Linear Search : Let n be the number of elements in the array.

Best Case Complexity (O(1))

• The element is found at the first position.


• Only one comparison is needed.

Worst Case Complexity (O(n))

• The element is at the last position or not present in the list.


• The algorithm must check all n elements.

Average Case Complexity (O(n))

• On average, the search finds the target somewhere in the middle of the list.
• It performs approximately n/2n comparisons.
• In Big-O notation, it is still O(n).

Case Complexity Explanation

Best Case O(1) If the element is found at the first position.

Worst Case O(n) If the element is at the last position or not present.

Average Case O(n) On average, we search half the elements, leading to O(n)O(n)O(n).

Space Complexity Analysis

• O(1) (Constant Space)


o The algorithm does not use extra memory apart from a few variables.
o It works in-place.

Advantages of Linear Search


✅ Works on both sorted and unsorted data.
✅ Simple and easy to implement.
✅ No extra memory required (O(1) space complexity).
✅ Can work on any data structure (arrays, linked lists, etc.).

Disadvantages of Linear Search


❌ Inefficient for large datasets (compared to Binary Search).
❌ Slow, as it checks each element one by one.

When to Use Linear Search?

• When the list is small.


• When the list is unsorted.
• When the dataset is dynamic (changing frequently).
• When searching in a linked list (where binary search is inefficient).
• When there is no time to sort the data before searching.

Binary Search Algorithm with Complexity Analysis


Binary Search is an efficient searching algorithm that works on sorted arrays. It follows the divide-and-conquer
approach to reduce the search space by half at each step, making it significantly faster than Linear Search.

Binary Search Algorithm

1. Find the middle element of the array.


2. If the middle element is the target, return its index.
3. If the middle element is greater than the target, search in the left half.
4. If the middle element is smaller than the target, search in the right half.
5. Repeat the process until the target is found or the search space is exhausted.
Iterative Binary Search
#include <stdio.h>
// Function to perform Binary Search (Iterative)
int binarySearch(int arr[], int n, int target) {
int left = 0, right = n - 1;

while (left <= right) {


int mid = left + (right - left) / 2; // Prevents
overflow

if (arr[mid] == target)
return mid; // Element found

if (arr[mid] < target)


left = mid + 1; // Search in the right half
else
right = mid - 1; // Search in the left half
}

return -1; // Element not found


}

int main() {
int arr[] = {10, 20, 30, 40, 50};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 30;

int result = binarySearch(arr, n, target);

if (result != -1)
printf("Element found at index %d\n", result);
else
printf("Element not found\n");

return 0;
}
Complexity Analysis of Binary Search
Case Complexity Explanation

Best Case O(1) If the element is at the middle.

Worst Case O(logn) The search space is reduced by half at each step.

Average Case O(logn) The expected number of comparisons follows the logarithmic pattern.

Space Complexity Analysis


Approach Complexity Explanation

Iterative Binary Search O(1) Uses only a few variables.


Approach Complexity Explanation

Recursive Binary Search O(logn) Due to recursive function calls (stack memory).

Advantages of Binary Search

✅ Much faster than Linear Search (O(logn) vs. O(n)).


✅ Efficient for large datasets.
✅ Requires only a few comparisons to find an element.

Disadvantages of Binary Search

❌ Only works on sorted arrays.


❌ Requires additional time to sort the data before searching.
❌ Not efficient for small datasets (Linear Search may be better).

When to Use Binary Search?

• When the dataset is sorted.


• When the dataset is large (Binary Search is much faster than Linear Search).
• When we need efficient searching in applications like databases, search engines, and algorithms like KMP.

You might also like