2. What is searching?
●
In computer science, searching is the process of finding an item
with specified properties from a collection of items. The items
may be stored as records in a database, simple data elements in
arrays, text in files, nodes in trees, vertices and edges in graphs,
or maybe be elements in other search place.
●
The definition of a search is the process of looking for
something or someone.
●
Example : An example of a search is a quest to find a missing
person.
2
2
3. ●
Searchingis one of the core computer science algorithms. We know
that today’s computers store a lot of information. To retrieve this
information proficiently we need very efficient searching
algorithms.
Why do we need searching?
Types of Searching
Linear search
Binary search
2
3
4. Disadvantages of Linear Search
4
●
Linear search is slower then other
searching algorithm.
●
If the array is small then it is good to use
linear search.
●
If the array size is large then it is not good
to use linear search because linear search
takes a large amount of time to search as
the element is compared sequentially with
each and every element .
5. What is Binary Search ?
• Binary search is searching technique
which uses divide and conquer approach.
• In binary search we divide the array in two halves.
We find the element present at the middle index of
the array. Now we compare the element to be
searched with the element present at the middle
index.
5
●
We perform binary search only on a
sorted array. If the array is not sorted
the binary search fails to work.
6. Continued….
• If the element to be searched is less than the
element at the middle index ,we search in the left
half of the array otherwise we will search in the
right half.
6
●
We continue this process until
we find our element to be
searched.
7. ●
Because binary search is faster than linear
search.
●
It is efficient because it eliminates half items at
each step: ½ then ¼ then 1/8.
●
It is also useful when we have a large array to be
searched whereas in linear search , we have to
search the whole array.
7
Why we need Binary Search ?
12. Algorithm of Binary search
12
Step 1: Find middle element of the array.
Step 2: Compare the value of the middle element with the target value.
Step 3: If they match, it is returned.
Step 4: If the value is less or greater than the target, the search continues in the lower or
upper half of the array accordingly.
Step 5: The same procedure as in step 2-4 continues, but with a smaller part of the array. This
continues until the target element is found or until there are no elements left.
12
13. 13
Disadvantages of Binary Search
●
In binary search the first and the
foremost thing is that the array should
be sorted.
●
If the array is not sorted then we have
to sort it first and then we have to
perform binary search. Because of this
the complexity of binary search will
increase.
13
14. Comparison between Linear and Binary Search
●
Linear search uses sequential approach.
●
Linear search slow working.
● Linear search does the sequential access
●
In linear search, the worst case complexity
is O(n)
●
Input data need not to be sorted in Linear
Search.
●
Binary search implements divide and
conquer approach.
●
Binary search is better and quite faster
●
Binary search access data randomly.
●
Binary search worst case complexity is
O(log n) .
●
Input data needs to be sorted in Binary
Search
14
15. Complexities of Linear and Binary Search
●
Since in Linear search we
compare the target value
with each and every
element of the array ,
therefore the complexity of
linear search is O(n).
●
At each iteration binary search
divides the array in two halves
so it is taking logarithmic time
to search the array. Therefore
the complexity of binary
search is O(log2 (n)).
15