Searching in PPS C Code
Searching in PPS C Code
• 2. Binary Search:
• - Works on sorted data by dividing the
search space into halves.
• - Time Complexity: O(log n)
Linear vs Binary Search
• Linear Search:
• • Works on unsorted data
• • Time Complexity: O(n)
• • Easy to implement
• Binary Search:
• • Requires sorted data
• • Time Complexity: O(log n)
• • More efficient for large datasets
Applications of Searching
• • Database Management Systems
• • Search Engines
• • E-commerce platforms (finding products)
• • Navigation Systems
• • Cybersecurity (pattern matching)
Linear Search Algorithm
• Algorithm:
• 1. Start at the first element.
• 2. Compare the target with the current
element.
• 3. If a match is found, return the index.
• 4. If no match is found, move to the next
element.
• 5. Repeat until the end of the list.
Binary Search Algorithm
• Algorithm:
• 1. Start with the middle element of the sorted
list.
• 2. Compare the target with the middle
element.
• 3. If it matches, return the index.
• 4. If the target is smaller, search the left half.
• 5. If the target is larger, search the right half.
• 6. Repeat until the target is found or the
Linear Search Code (C)
• #include <stdio.h>
• Disadvantages:
• • Inefficient for large datasets.
• • Time complexity: O(n), which can be slow for
larger inputs.
Advantages and Disadvantages:
Binary Search
• Advantages:
• • Highly efficient for large, sorted datasets.
• • Time complexity: O(log n).
• Disadvantages:
• • Only works on sorted data.
• • Requires additional sorting if the data isn't
sorted.
Real-life Examples of Searching
• 1. Searching for a contact in a phonebook
(Binary Search).
• 2. Looking up a word in a dictionary (Binary
Search).
• 3. Finding a product on an e-commerce site
(Search algorithms).
• 4. Searching for files or folders on a computer
(File system search).
• 5. Pattern matching in text or DNA sequences.
Time and Space Complexities
• Linear Search:
• • Time Complexity: O(n)
• • Space Complexity: O(1)
• Binary Search:
• • Time Complexity: O(log n)
• • Space Complexity: O(1) (iterative) or O(log n)
(recursive).
Use Cases for Searching Methods
• • Linear Search:
• - Useful for small datasets.
• - Works well when data is not sorted.
• • Binary Search:
• - Preferred for large datasets that are sorted.
• - Often used in databases and search
engines.
Searching in Multidimensional
Data
• • Searching in 2D Arrays:
• - Linear search can traverse row by row.
• - Binary search can be applied if rows and
columns are sorted.
• • Applications:
• - Image processing.
• - Geographic Information Systems (GIS).
Advanced Searching Algorithms
• • Hashing:
• - Uses a hash table for constant-time search.
• • Interpolation Search:
• - Works on sorted and uniformly distributed
data.
• - Time Complexity: O(log(log n)).
• • Exponential Search: