Ayesha Khalid BSCE-24008
DISCRETE STRUCTURES
🔍 Searching Algorithm – Detailed Report (Discrete Structures)
1. Introduction
Searching algorithm ek technique hai jo kisi data set mein specific value ko locate karne ke liye use hoti
hai. Discrete structures mein searching algorithms ka study logical thinking, algorithm analysis, and
mathematical modeling ka hissa hota hai.
2. Definition
A searching algorithm is a method used to find an element (called a "key") within a data structure such
as an array, list, or graph.
TYPES :
1. Linear Search:
🔧 Pseudocode:
function linearSearch(array, target):
for i from 0 to length(array) - 1:
if array[i] == target:
return i
return -1 // Not found
⏱ Time Complexity
Best Case: O(1) (target is first element)
Average Case: O(n)
Worst Case: O(n)
🧠 Computational Complexity
Space Complexity: O(1)
Works for unsorted data.
✅ Advantages
Simple to implement
Works on both sorted and unsorted lists
No preprocessing required
❌ Disadvantages
Very slow for large datasets
Inefficient compared to binary search in sorted arrays
💡 Suggested Improvements
If the data is searched frequently, sort the data and use Binary Search.
Use sentinel linear search to reduce comparison operations.
2. Binary Search:
🔧 Pseudocode
function binarySearch(array, target):
left = 0
right = length(array) - 1
while left <= right:
mid = (left + right) / 2
if array[mid] == target:
return mid
else if array[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1
⏱ Time Complexity
Best Case: O(1) (middle element is target)
Average Case: O(log n)
Worst Case: O(log n)
🧠 Computational Complexity
Space Complexity: O(1) for iterative, O(log n) for recursive
Requires sorted data
✅ Advantages
Very efficient on large sorted datasets
Fewer comparisons compared to linear search
❌ Disadvantages
Only works on sorted arrays
More complex than linear search
💡 Suggested Improvements
Use exponential search or interpolation search for better performance in uniformly
distributed data.
Use ternary search for some unimodal function optimizations.
6. Applications in Discrete Structures
Set Membership Problems
Function Computation
Graph Traversal (e.g., BFS, DFS as advanced search)
Logical Queries (Propositional logic evaluation using search techniques)
🧠 CONCLUSION & GENERAL SUGGESTIONS
Searching algorithms are an essential topic in discrete structures because they combine logic,
algorithm design, and mathematical reasoning. They form the basis of problem-solving in computer
science and have applications in databases, AI, and decision-making systems.
· Unsorted data, small size: Use Linear Search
· Sorted data: Prefer Binary Search
🚀 General Improvements
Use caching/memoization when the same values are searched repeatedly.
· Apply parallel searching in multi-core environments.
· Build search indices for faster access (used in databases and search engines).
· Use data profiling to dynamically select the best algorithm.