Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
A presentation on the comparison on complexity between
1. PRESENTED BY :
JUBAYER HASAN & ELEAS KANCHON
DEPT. OF COMPUTER SCIENCE & ENGINEERING
RAJSHAHI UNIVERSITY OF ENGINEERING & TECHNOLOGY
A presentation on the comparison on
complexity between linear search and
binary search
2. What will be discussed?
Necessity of different types searching techniques.
Types of searching algorithm.
Analysis of linear search algorithm.
Analysis of binary search algorithm.
Comparison between two algorithms.
Comparison between linear search and binary search
2
20-Jan-17
3. Why different searching techniques???
To reduce time consumed by the algorithms.
To minimize space taken by the algorithm to process data.
To work with greater amount of data.
Comparison between linear search and binary search
3
20-Jan-17
4. Types of searching techniques
Linear search : Search linearly through the data set.
Binary search : Search using divide and conquer technique.
Comparison between linear search and binary search
4
20-Jan-17
5. Linear search
*Searches data linearly from a
data set. Starting from the
beginning it traverses the whole
data set until it finds the desired
data. If found the algorithm
returns the index.
*If data is absent then it returns
an invalid result as index.
*Easy to understand and
implement.
*Not good for large amount of
data.
Comparison between linear search and binary search
5
20-Jan-17
6. Complexity analysis of linear search
In this algorithm, we search our desired data from the beginning of the dataset.
So the worst case can happen when the desired data lies at the end of the
dataset.
Hence the complexity is O(n).
Comparison between linear search and binary search
6
20-Jan-17
7. Binary search
Search using divide and conquer technique.
Works only on sorted array or linked list.
Takes a mid value to compare if the mid value is less than or greater than the
item to be searched.
Continuously changes searching area based on the value of mid.
Works until the value is found or the limit of lower bound becomes greater than
the value of upper bound.
Comparison between linear search and binary search
7
20-Jan-17
8. The algorithm of binary search
BINARYSEARCH(LEFT,RIGHT,DATA, VALUE)[Here DATA is an array with N elements.]
1. IF LEFT>RIGHT THEN :
RETURN -1.
2. MID:=(LEFT+RIGHT)>>2.//FINDING MID USING SHIFT OPERATION.
3. IF DATA[MID]:=VALUE THEN:
RETURN MID.
4. IF DATA[MID]<VALUE THEN:
RETURN BINARYSEARCH(MID+1,RIGHT,DATA,VALUE).
5. IF DATA[MID]<VALUE THEN:
RETURN BINARYSEARCH(LEFT,MID-1,DATA,VALUE).
Comparison between linear search and binary search
8
20-Jan-17
9. The algorithm
*Lets take an array containing 10
data elements such as :
10,14,19,26,27,31,35,42,44.
Comparison between linear search and binary search
9
20-Jan-17
10. The algorithm
*Lets take an array containing 10
data elements such as :
10,14,19,26,27,31,35,42,44.
*The value to be searched is 35.
Comparison between linear search and binary search
10
20-Jan-17
11. The algorithm
*Lets take an array containing 10
data elements such as :
10,14,19,26,27,31,35,42,44.
*The value to be searched is 35.
*Starting with lower bound at
index 0 and upper at index 9,we
can get the mid value at the
index of (0+9)/2=4.
Comparison between linear search and binary search
11
20-Jan-17
12. The algorithm
(contd.)
*The desired value 35 is greater
than the mid value we got after
getting mid at 4th position.
Comparison between linear search and binary search
12
20-Jan-17
13. The algorithm
(contd.)
*The desired value 35 is greater
than the mid value we got after
getting mid at 4th position.
*So we shifted the left index to
5th position and calculated mid
position again.
*We got the mid position now
on (5+9)/2=7th position.
Comparison between linear search and binary search
13
20-Jan-17
14. The algorithm
(contd.)
*The desired value 35 is greater
than the mid value we got after
getting mid at 4th position.
*So we shifted the left index to
5th position and calculated mid
position again.
*We got the mid position now
on (5+9)/2=7th position.
*So we got our desired value
after running the algorithm just
for 2 times.
Comparison between linear search and binary search
14
20-Jan-17
15. Complexity analysis of binary search
We can observe that every time the algorithm runs the searching area is getting
divided into two parts. That means if we have 8 elements, after 1st operation we
will have 8/2=4 elements, after 2nd operation we will have 4/2=2 elements and
so on.
The process will continue until we get our desired element.
If we need ‘m’ number of comparisons to get this point we can say that:
n/(2m)=1. That means m=log2n.
Comparison between linear search and binary search
15
20-Jan-17
17. Why binary search is better?
Binary search reduces the searching complexity to O(log2n) for n elements. It is
a massive reduction in searching time if we compare the complexity with linear
search which has complexity of O(n).
In case of large amount of data binary search is very much effective than linear
search. It reduces the iteration number.
Though binary search only works on sorted dataset while in case of linear search
it is not mandatory, the less iteration number gives the binary search algorithm
more efficiency.
Comparison between linear search and binary search
17
20-Jan-17
18. A statistical overview (For 1000000 items)
Search Item Index No. of iteration for Linear
Search
No. of iteration for Binary
Search
1000 981 75
10000 9970 59
25000 24998 44
50000 49984 29
1000000 999964 15
Comparison between linear search and binary search
18
20-Jan-17