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

Searching Algorithm

Uploaded by

pranshusahu862
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Searching Algorithm

Uploaded by

pranshusahu862
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Searching Algorithms in Computer Science

Searching algorithms are designed to retrieve specific elements or data from a collection. These
algorithms are fundamental in computer science, enabling tasks such as database queries, file
retrieval, and efficient data processing.

1. Introduction to Searching
Searching involves looking for an element within a dataset. The dataset can be unsorted, sorted, or
structured differently, such as in trees, graphs, or hash tables.
• Key Applications:
• Database queries.
• Information retrieval systems.
• Artificial intelligence and game development.

2. Types of Searching Algorithms


Searching algorithms are broadly divided into:
1. Linear Search Algorithms: Sequentially examine each element.
2. Binary Search Algorithms: Use divide-and-conquer for sorted data.
3. Hash-Based Search: Use hash tables for constant time lookups.
4. Tree and Graph Search: Traverse hierarchical structures for searching.

3. Linear Search
Description
Linear Search scans each element in a dataset until the desired value is found or the end is reached.
It is simple but inefficient for large datasets.
• Time Complexity:
• Best Case: O(1) (Element found at the beginning).
• Worst Case: O(n) (Element not found or at the end).

Algorithm (Pseudocode):
text
Copy code
function linearSearch(array, target):
for i = 0 to length(array) - 1:
if array[i] == target:
return i
return -1

Python Implementation:
python
Copy code
def linear_search(arr, target):
for i in range(len(arr)):
if arr[i] == target:
return i
return -1

4. Binary Search
Description
Binary Search operates on sorted data by repeatedly dividing the search interval in half. It compares
the middle element with the target to decide the next search interval.
• Time Complexity:
• Best Case: O(1) (Middle element is the target).
• Average/Worst Case: O(logn).

Algorithm (Pseudocode):
text
Copy code
function binarySearch(array, target):
low = 0, high = length(array) - 1
while low <= high:
mid = (low + high) // 2
if array[mid] == target:
return mid
elif array[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1

Python Implementation:
python
Copy code
def binary_search(arr, target):
low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1

Advantages:
• Extremely efficient for large sorted datasets.
5. Hash-Based Searching
Description
Hashing uses a hash function to map data into a hash table, allowing constant-time search
operations in the average case.
• Time Complexity:
• Average Case: O(1).
• Worst Case: O(n) (Hash collisions).

Applications:
• Dictionary lookups.
• Cache mechanisms.

Python Example:
python
Copy code
hash_table = {1: "Alice", 2: "Bob", 3: "Charlie"}
target_key = 2
print(hash_table[target_key]) # Output: Bob

6. Tree-Based Searching
Binary Search Trees (BST)
BSTs are tree structures where the left child node is less than the parent, and the right child node is
greater.
• Search Complexity:
• Average Case: O(logn).
• Worst Case: O(n) (Degenerate tree).

Algorithm:
python
Copy code
class Node:
def __init__(self, key):
self.key = key
self.left = None
self.right = None

def search_bst(root, target):


if not root or root.key == target:
return root
if target < root.key:
return search_bst(root.left, target)
return search_bst(root.right, target)
7. Graph Searching Algorithms
Breadth-First Search (BFS)
BFS explores all neighbors of a node before moving to the next level.
• Applications: Shortest path in unweighted graphs.
• Complexity: O(V+E), where V is vertices and E is edges.

Depth-First Search (DFS)


DFS explores as far as possible along a branch before backtracking.
• Applications: Detecting cycles in graphs.
• Complexity: O(V+E).

8. Comparison of Searching Algorithms


Algorithm Best Case Average Case Worst Case Data Structure
Linear Search O(1) O(n) O(n) Array
Binary Search O(1) O(logn) O(logn) Sorted Array
Hash-Based O(1) O(1) O(n) Hash Table
Tree-Based BST O(1) O(logn) O(n) Tree

9. Real-World Applications of Searching


• Databases: Query optimization using indexes.
• Networking: Routing algorithms in communication networks.
• E-commerce: Product search engines.
• AI and Robotics: State-space searching in problem-solving.

10. Advanced Searching Techniques


Ternary Search
Used in unimodal functions, where the search space is divided into three parts.

Exponential Search
Efficient for searching in infinite or unbounded lists.

You might also like