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

Searching algorithm

Uploaded by

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

Searching algorithm

Uploaded by

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

Searching Algorithms

Searching algorithms are essential tools in computer science used to locate specific items
within a collection of data. These algorithms are designed to efficiently navigate through data
structures to find the desired information, making them fundamental in various applications
such as databases, web search engines, and more.

Linear Search Algorithm


In Linear Search, we iterate over all the elements of the array and check if it the current element
is equal to the target element. If we find any element to be equal to the target element, then
return the index of the current element. Otherwise, if no element is equal to the target
element, then return -1 as the element is not found. Linear search is also known as sequential
search.
Given an array, arr of n integers, and an integer element x, find whether element x is present in
the array. Return the index of the first occurrence of x in the array, or -1 if it doesn’t exist.
Input: arr[] = [1, 2, 3, 4], x = 3
Output: 2
Explanation: There is one test case with array as [1, 2, 3 4] and element to be searched as 3.
Since 3 is present at index 2, the output is 2.
Input: arr[] = [10, 8, 30, 4, 5], x = 5
Output: 4
Explanation: For array [1, 2, 3, 4, 5], the element to be searched is 5 and it is at index 4. So, the
output is 4.
Input: arr[] = [10, 8, 30], x = 6
Output: -1
Explanation: The element to be searched is 6 and its not present, so we return -1.
Time and Space Complexity of Linear Search Algorithm:
Time Complexity:
• Best Case: In the best case, the key might be present at the first index. So the best case
complexity is O(1)
• Worst Case: In the worst case, the key might be present at the last index i.e., opposite to
the end from which the search has started in the list. So the worst-case complexity is
O(N) where N is the size of the list.
• Average Case: O(N)
Auxiliary Space: O(1) as except for the variable to iterate through the list, no other variable is
used.
Applications of Linear Search Algorithm:
• Unsorted Lists: When we have an unsorted array or list, linear search is most commonly
used to find any element in the collection.
• Small Data Sets: Linear Search is preferred over binary search when we have small data
sets with
• Searching Linked Lists: In linked list implementations, linear search is commonly used to
find elements within the list. Each node is checked sequentially until the desired element
is found.
• Simple Implementation: Linear Search is much easier to understand and implement as
compared to Binary Search or Ternary Search.
Advantages of Linear Search Algorithm:
• Linear search can be used irrespective of whether the array is sorted or not. It can be
used on arrays of any data type.
• Does not require any additional memory.
• It is a well-suited algorithm for small datasets.
Disadvantages of Linear Search Algorithm:

• Linear search has a time complexity of O(N), which in turn makes it slow for large datasets.

• Not suitable for large arrays.

Binary Search Algorithm


Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing
the search interval in half. The idea of binary search is to use the information that the array is
sorted and reduce the time complexity to O(log N).
• Divide the search space into two halves by finding the middle index “mid”.
• Compare the middle element of the search space with the key.
• If the key is found at middle element, the process is terminated.
• If the key is not found at middle element, choose which half will be used as the next
search space.
o If the key is smaller than the middle element, then the left side is used for next
search.
o If the key is larger than the middle element, then the right side is used for next
search.
• This process is continued until the key is found or the total search space is exhausted.
Complexity Analysis of Binary Search Algorithm
• Time Complexity:
o Best Case: O(1)
o Average Case: O(log N)
o Worst Case: O(log N)
• Auxiliary Space: O(1), If the recursive call stack is considered then the auxiliary space
will be O(logN).
Applications of Binary Search Algorithm
• Binary search can be used as a building block for more complex algorithms used in
machine learning, such as algorithms for training neural networks or finding the optimal
hyperparameters for a model.
• It can be used for searching in computer graphics such as algorithms for ray tracing or
texture mapping.
• It can be used for searching a database.
Advantages of Binary Search
• Binary search is faster than linear search, especially for large arrays.
• More efficient than other searching algorithms with a similar time complexity, such as
interpolation search or exponential search.
• Binary search is well-suited for searching large datasets that are stored in external
memory, such as on a hard drive or in the cloud.
Disadvantages of Binary Search
• The array should be sorted.
• Binary search requires that the data structure being searched be stored in contiguous
memory locations.
• Binary search requires that the elements of the array be comparable, meaning that they
must be able to be ordered.

You might also like