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

Lecture_5

This document discusses searching algorithms, focusing on Sequential (Linear) Search and Binary Search. Linear Search is simple but inefficient for large datasets, while Binary Search is efficient for sorted arrays, using a divide-and-conquer strategy. The document also highlights the time complexities of both algorithms and provides pseudocode examples for implementation.

Uploaded by

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

Lecture_5

This document discusses searching algorithms, focusing on Sequential (Linear) Search and Binary Search. Linear Search is simple but inefficient for large datasets, while Binary Search is efficient for sorted arrays, using a divide-and-conquer strategy. The document also highlights the time complexities of both algorithms and provides pseudocode examples for implementation.

Uploaded by

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

Data Structures & Algorithms

Dr. Ghulam Farooque


Assistant Professor
Department: CS & IT
The University of Lahore
Email: [email protected]
Searching Algorithms
Sequential (Linear) Search &
Binary Search
Searching Algorithms
• Searching algorithms allow us to find specific
elements within data structures.
• Two primary types of searching algorithms:
– Sequential (Linear) Search: Looks through each
element one by one.

– Binary Search: Efficiently finds elements in sorted


arrays by dividing the search space in half.
Sequential (Linear) Search
• A sequential Search of a list/array begins at the
beginning of the list/array and continues until the
item is found or the entire list/array has been
searched.
• A method that checks each element sequentially until
the target is found or the list ends.
• Usage: Works on both sorted and unsorted lists but
is inefficient for large data.
• Real-world analogy: Searching for a book in a stack
by checking each one until you find the correct one.
Linear Search Pseudocode

• function linearSearch(arr, target)


• for each element in arr:
• if element == target:
• return index of element
• return -1
Linear Search in C++
bool LinSearch(double x[ ], int n, double item){

for(int i=0;i<n;i++){
if(x[i]==item) return true;
else return false;
}
return false;
}
Linear Search - Time Complexity

• Worst Case (O(n)): When the target is the last


element or not in the array.

• Best Case (O(1)): When the target element is


the first element.
Linear Search
Binary Search
• A binary search looks for an item in a list using a
divide-and-conquer strategy.
• An efficient search algorithm for sorted arrays, which
works by repeatedly dividing the search interval in
half.
• Key Points:
– The array must be sorted.
– Reduces the problem size by half after each
comparison.
Binary Search
• The binary search algorithm assumes that the items in
the array being searched are sorted.
• The algorithm begins at the middle of the array in a
binary search.
• If the item for which we are searching is less than the
item in the middle, we know that the item won’t be in
the second half of the array.
• Once again, we examine the “middle” element.
• The process continues with each comparison cutting in
half the portion of the array where the item might be.
Binary Search Pseudocode
• function binarySearch(arr, target, left, right)
• while left <= right:
• middle = (left + right) / 2
• if arr[middle] == target:
• return middle
• else if arr[middle] < target:
• left = middle + 1
• else:
• right = middle - 1
• return -1
Binary Search
Binary Search: middle element

left + right
mid =
2
Binary Search
int binarySearch(int arr[], int left, int right, int target) {
while (left <= right) {
int mid = (left + right) / 2;

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

if (arr[mid] < target)


left = mid + 1; // Search right half
else
right = mid - 1; // Search left half
}
return -1; // Element not found }
Binary Search: Example
Binary Search
Binary Search
Binary Search
Binary Search
• Binary Search has a time complexity of O(log n)
because it eliminates half of the remaining
elements with each comparison.

• Logarithmic time complexity due to halving the


problem size with each comparison.
Linear Search vs Binary Search

| Feature | Linear Search | Binary Search |


|------------------------ |--------------------------------- |------------------------------ |
| Array Type | Works on any array | Requires sorted array |
| Time Complexity | O(n) | O(log n) |
| Efficiency | Slow for large datasets | Fast for large datasets |
| Ease of Use | Simple to implement | Requires array sorting |
Practical Example of Binary Search
Efficiency
• Consider an array of 1 million elements.
• Linear Search: Requires up to 1 million
comparisons.
• Binary Search: Requires only log2(1,000,000) ≈
20 comparisons.
When to Use Linear Search?

• Small datasets.
• When the data is unsorted.
• If simplicity is more important than
efficiency.
When to Use Binary Search?

• Large datasets.
• When the data is already sorted.
• If you need fast and efficient
searching performance.
Big-O Recap

• Linear Search:
– Best Case: O(1)
– Worst Case: O(n)
• Binary Search:
– Best Case: O(1)
– Worst Case: O(log n)
Summary of Key Concepts
• Linear Search is straightforward but
inefficient for large arrays.
• Binary Search is a faster algorithm but
works only on sorted arrays.
• Understanding the Big-O complexity
helps choose the right search method
for the task.

You might also like