Search Algo
Search Algo
techniques, essential
of search
Introduction to searching
Searching is the fundamental process of locating a specific element or item within a
collection of data. This collection of data can take various forms, such as arrays,
lists, or other structured representations.
Essential of searching
Efficiency: Efficient searching algorithms improve program
performance.
Data Retrieval: Quickly find and retrieve specific data from large
datasets.
Problem Solving: Used in a wide range of problem-solving tasks.
Linear Search
Binary Search
Linear Search
Linear Search or Sequential Search, is one of the simplest searching algorithms is which the algorithm
works by sequentially iterating through the whole array or list from one end until the target element is
found.
Pseudo Code:
LinearSearch(Array, key){
for each element in Array{
if element == key{
return the index of element
}
}
return “Not Found”
}
Binary Search
Binary Search is defined as a searching algorithm used in a sorted array by repeatedly
dividing the search interval in half.
binarySearch(Array, key){
Low = 0
High = Array.length – 1
while (Low <= High) {
mid = (Low +High) / 2
if (Array[mid] == key)
return mid
else if (Array[mid] < key)
Low = mid + 1
else
High = mid – 1
}
return “Not found”
}
Time complexity of linear search:
Best Case: O(1) – When the key is found at the middle element.
Worst Case: O(N) – When the key is not present, and the search
space is
continuously halved.