3. introduction
Problem:
Given a list of n numbers and q queries, find out whet
her each query is among the list of n numbers.
Analysis:
The solution is to use a linear search, where, for each
query k , we check each of then n numbers to see if k
is one of them. this o(n) algorithm will often be too slow.
we could use complicated data structures such as a set
or a hash table, but a much easier solution is to
implement a binary search
1
4. A binary search
is a divide-and-conquer .we algorithm divide the list in
half and search from only half of the list for future
iterations.
Objective: -
Find the value of the key to the matrix x .
The idea: -
This algorithm is based on binary search in the matrix
x. where the search begins from the element, which is
located in the center of the matrix, and every time I
compare it with the key so that, if the two values are
equal,
this
5. indicates that the key is to find values in the matrix, either if
the key value is less of the value of the item east settec
the search in the left part of the matrix and in the case of
adverse will be searched in the right part of the matrix,
and so on ... So we get a matrix consisting of one box a
value equal to the key or different about him.
3
6. The steps of an algorithm:
1. sort array descending or ascending
2. Set low=0 and high=length-1
3. If low not smaller or not equal high go to 8:
4. Set middle =(low + high)/2
5. If target equal array [middle] go to 9
6. If target large then array[mid] set low =mid+1 t and go to 3.
7. If target smaller than array[mid] set high=mid-1 and go to 3
8. Print target not found and go 10
9. return middle
10. End function
4
12. 3) Since 33 is smaller than 53, the search
process will be carried out on the first half:
10
13. 4) we work on the first half division and
become the med value is equal to: -
11
14. 5) Since 33 is greater than 25, the first half
will be excluded and the search continues
in the second half
1
2
15. 6) again, we re-partitioning process where
values become med equal to 5
7) Akhiranjd the value of 33 found in the site
if and which equals 4.
1
3
16. Best case and worst case :
·The best case for a binary search is finding the
target item on the first look
.The worst case for a binary search is searching for
an item which is not in the data or in last look .
1
4