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

Searching in PPS C Code

The document provides an overview of searching techniques in programming, focusing on linear and binary search methods. It outlines their algorithms, advantages, disadvantages, and applications in various fields such as databases and e-commerce. Additionally, it discusses complexities and advanced searching algorithms like hashing and interpolation search.

Uploaded by

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

Searching in PPS C Code

The document provides an overview of searching techniques in programming, focusing on linear and binary search methods. It outlines their algorithms, advantages, disadvantages, and applications in various fields such as databases and e-commerce. Additionally, it discusses complexities and advanced searching algorithms like hashing and interpolation search.

Uploaded by

saivarun437
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Searching in PPS

Problem Solving and Programming


Skills
Introduction to Searching
• • Searching is the process of finding a specific
element in a data structure.
• • It is one of the fundamental operations in
problem-solving and programming.
• • Commonly used in databases, search
engines, and applications requiring quick
access to data.
Types of Searching
• 1. Linear Search:
• - Sequentially checks each element until the
target is found or the list ends.
• - Time Complexity: O(n)

• 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>

• int linear_search(int arr[], int size, int target) {


• for (int i = 0; i < size; i++) {
• if (arr[i] == target) {
• return i;
• }
• }
• return -1;
Binary Search Code (C)
• #include <stdio.h>

• int binary_search(int arr[], int size, int target) {


• int low = 0, high = size - 1;
• while (low <= high) {
• int mid = low + (high - low) / 2;
• if (arr[mid] == target) {
• return mid;
• } else if (arr[mid] < target) {
Advantages and Disadvantages:
Linear Search
• Advantages:
• • Simple to implement.
• • Works on both sorted and unsorted data.

• 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:

You might also like