Algorithm 8th lecture linear & binary search(2).pptx
The document discusses linear and binary search algorithms. Linear search sequentially checks each element of an unsorted array to find a target value, resulting in O(n) time complexity in the worst case. Binary search works on a sorted array by comparing the target to the middle element and recursively searching half the array, resulting in O(log n) time complexity in the worst case, which is more efficient than linear search.
Linear Search
(sequential search)
Thelinear searchisasequentialsearch,which usesaloop to
step through an array,starting withthe first element.
Itcompares each element withthe valuebeing searched
for,and stops when either the valueisfound or the end of
the arrayis encountered.
Ifthe valuebeing searched isnot in the array,the algorithm
will unsuccessfullysearchto the end of the array.
3.
Sincethe arrayelementsarestored inlinearorder
searching the element in the linear order makeit easy
and efficient.
The searchmaybe successfulor unsuccessfully.
Thatis,if the required element isfound themthe search
issuccessful other wiseit is unsuccessfully.
Linear Search
(sequential search)
Analysis of LinearSearch
How long will our searchtake?
Inthe best case,the target valueisin the firstelement of the array.
Sothe searchtakessome tiny,and constant,amount of time.
Inthe worstcase,the target valueisin the lastelement of the array.
Sothe searchtakesan amount of timeproportional to the length
of the array.
Best-case performance: O(1)
Worst-case performance: O(n)
12.
Analysis of LinearSearch
Inthe average case,the target valueissomewherein the array.
Infact,since the target valuecan be anywherein the array,any
element of the arrayisequally likely.
Soon average,the target valuewill be in the middle of the array.
Sothe searchtakesan amount of timeproportional to half the
length of the array
13.
Advantages
The linearsearchissimple -Itisveryeasyto
understand and implement
Itdoes not requirethe data in the arrayto be stored
in any particular order
14.
Disadvantages
Ithasverypoor efficiency becauseit takes
lots of comparisons to find aparticular record
in big files
The performance of the algorithm scaleslinearly
withthe size of the input
Linearsearchisslower then other searching
algorithms
15.
Binary Search
The generaltermfor asmartsearchthrough sorted data isabinary search.
1. The initial searchregion isthewhole array.
2. Look atthedata valuein themiddle of thesearchregion.
3. Ifyou’vefound your target, stop.
4. Ifyourtarget islessthan themiddle data value,thenewsearch region is
thelower half of the data.
5. Ifyourtarget isgreater than themiddle data value,the new
searchregion isthehigher half of the data.
6. Continue fromStep2.
Best Case TimeComplexity of
Binary Search
19
The best case of binary search is when the first comparison/guess
is correct(the key item is equal to the mid of array). It means,
regardless of the size of the list/array, we’ll always get the result
in constant time.
So the best case complexity is O(1).
20.
Worst Case TimeComplexity of
Binary Search
20
Think about how it operates: after you examine a value,you cut the search region in
half.
So,the first iteration of the loop, your search region isthe whole array.
The second iteration, it’shalf the array.The third iteration, it’sa quarter of the array.
...
The kthiteration, it’s(1/2k-1)of thearray.
The worst case complexity of binary search is O(log n).
21.
Need to SortArray
21
Binarysearchonlyworksif the arrayisalready sorted.
Itturnsout thatsorting isahuge issuein computing –and
the subjectof our nextlecture.
22.
Examples
Example(1): The runningtime of binary search algorithm is given recurrence
T(1) = c( constant)
T(n) = T(n/2) + c, n > 1
Here T(n/2) is time to search left-half or right-half of a sorted array, c is the combined cost of comparing one
key and finding the middle element in the array. Solution is as follows:
Initially, T(n) = T(n/2) + c = T(n/21) + c
Substituting for T(n/2), T(n) = T(n/4) + 2c
The Substitution Method
= T(n/22)
Again substituting for T(n/4), T(n) =
+ 2.c
T(n/8) + 3c
= T(n/23) + 3.c
T(n/2k) + k.c
Continuing, after kth step, T(n) =
The base case is reached when n / 2k =1,or n=2k . or k = lgn
Substituting for k , T(n) = T(1)+ lg n. c
= c + lg n. c
Ignoring constants, T(n) = θ( lg n)
(Closed form)
( Asymptotic notation)