Upload
harvey-strickland
View
218
Download
0
Embed Size (px)
Citation preview
SEARCHING
Searching refers to the operation of finding the location of an element in the array of list.
(OR)Searching is the process of determining
whether or not a given value exists in a data structure or a storage media.
Ex:- searching the telephone number
Types of Searching: Linear Search Binary Search Fibonacci Search Interpolative search
Searching:-
This is a technique for searching a particular element in
sequential manner until the desired element is found.
If an element is found then search is successful,
otherwise search is said to be unsuccessful.
This is applicable to a table of element or linked list
Linear search:-
Analysis:-
Best case:-
If the 1st element is the required element. The
time complexity is 1.
Worst case:-
If none of the element is same s the search
element. The time complexity is ‘n’.
Linear search:-
public static int linearSearch(Object[] array, Object key)
{
for(int k = 0; k < array.length; k++)
if(array[k].equals(key))
return k;
return -1;
}
Algorithm
In this search all the elements in the array are sorted in ascending order.
Find the position of the mid element using the boundaries of an array.
If the searching element is grater than the mid element then adjust the lower boundary of an array.
Ex:- low=mid+1 If the searching element is small than the mid element the
adjust the higher boundary of an array.Ex:- high=mid-1
If the searching the element is equal to the middle element then stop the process and print search successful.
Repeat the steps until(low<=high)
Binary search : -
Binary search uses a recursive method to search an array to find a specified value
Each execution of the recursive method reduces the search space by about a half
The binary search algorithm is extremely fast compared to an algorithm that tries all array elements in order
About half the array is eliminated from consideration right at the start
Then a quarter of the array, then an eighth of the array, and so forth
Efficiency of Binary Search