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

Searching Algorithms

Uploaded by

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

Searching Algorithms

Uploaded by

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

SEARCHING ALGORITHM

Introduction

searching algorithm is a method used to find a specific item or


element within a collection of data. This collection of data can take
various forms, such as arrays, lists, trees, or other structured
representations
These algorithms are widely used in computer science and are
crucial for tasks like searching for a particular record in a database,
finding an element in a sorted list, or locating a file on a computer.
Introduction cont…

Some Key aspects


• Target Element: a specific target element or item that you want to find within the data
collection. This target could be a value, a record, a key, or any other data entity of interest.
• Search Space: refers to the entire collection of data within which you are looking for the
target element. Depending on the data structure used, the search space may vary in size and
organization.
• Complexity: Searching can have different levels of complexity depending on the data
structure and the algorithm used. The complexity is often measured in terms of time and
space requirements.
searching algorithms:

1.Linear Search
2.Binary Search
 Linear Search

Linear Search, also known as Sequential Search, is one of the


simplest and most straightforward searching algorithms. It works by
sequentially examining each element in a collection of data(array or
list) until a match is found or the entire collection has been
traversed.
Steps in Linear Search
i. Start from index 0 of the array compare the key value with the value present in index
0.
ii. If the value matches with the key, return the position at which the value was found.
iii. If the value does not match with the key, compare the next element in the array.
iv. Repeat Step 3 until there is a match found. Return the position at which the match
was found.
v. If it is an unsuccessful search, print that the element is not present in the array and
exit the program.
Complexity Analysis of Linear Search:
• 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.

 When to use Linear Search:


When there is small collection of data.
When data is unsorted.
 Binary Search

• Binary Search is defined as 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). It making it highly
efficient for large sorted lists.
Steps in Binary Search

i. Start with the entire sorted array or list.


ii. Select the middle item in the array and compare it with the key value to be
searched. If it is matched, return the position of the median.
iii. If it does not match the key value, check if the key value is either greater
than or less than the median value.
iv. If the key is greater, perform the search in the right sub-array; but if the key
is lower than the median value, perform the search in the left sub-array.
v. Repeat Steps 1, 2 and 3 iteratively, until the size of sub-array becomes 1.
vi. If the key value does not exist in the array, then the algorithm returns an
unsuccessful search.
Complexity Analysis of Binary Search:
• Time Complexity:
 Best Case: O(1) – When the key is found at the middle element.
 Worst Case: O (log N) – When the key is not present, and the
search space is continuously halved.
 Average Case: O(log N)
• Auxiliary Space: O(1)

 When to use Binary Search:


 When the data collection is sorted (essential condition).
It’s highly efficient for searching in large datasets compared to Linear
Search.

You might also like