Searching Algorithm:
Linear Search & Binary Search
Linear Search
(sequential search)
The linear 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.
Sincethe arrayelementsarestored in linearorder
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)
Linear Search Example
element
-23 97 18 21 5 -86 64 0 -37
Searching for -86.
4
Linear Search Example
element
-23 97 18 21 5 -86 64 0 -37
Searching for -86.
5
Linear Search Example
element
-23 97 18 21 5 -86 64 0 -37
Searching for -86.
6
Linear Search Example
element
-23 97 18 21 5 -86 64 0 -37
Searching for -86.
7
Linear Search Example
element
-23 97 18 21 5 -86 64 0 -37
Searching for -86.
8
Linear Search Example
element
-23 97 18 21 5 -86 64 0 -37
Searching for -86: found!
9
Linear Search
(sequential search)
Analysis of Linear Search
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)
Analysis of Linear Search
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
Advantages
The linearsearchissimple - Itisveryeasyto
understand and implement
Itdoes not requirethe data in the arrayto be stored
in any particular order
Disadvantages
Ithasverypoor efficiency because it 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
Binary Search
The general termfor 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.
Binary Search Example
low
Searching for 18.
middle high
-86 -37 -23 0 5 18 21 64 97
16
Binary Search Example
-86 -37 -23 0 5 18 21 64 97
low
Searching for 18.
middle
high
17
Binary Search Example
low
middle
Searching for 18:found!
high
-86 -37 -23 0 5 18 21 64 97
17
Best Case Time Complexity 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).
Worst Case Time Complexity 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).
Need to SortArray
21
Binarysearchonly worksif the arrayisalready sorted.
Itturnsout thatsorting isahuge issuein computing –and
the subjectof our nextlecture.
Examples
Example(1): The running time 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)

Algorithm 8th lecture linear & binary search(2).pptx

  • 1.
  • 2.
    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)
  • 4.
    Linear Search Example element -2397 18 21 5 -86 64 0 -37 Searching for -86. 4
  • 5.
    Linear Search Example element -2397 18 21 5 -86 64 0 -37 Searching for -86. 5
  • 6.
    Linear Search Example element -2397 18 21 5 -86 64 0 -37 Searching for -86. 6
  • 7.
    Linear Search Example element -2397 18 21 5 -86 64 0 -37 Searching for -86. 7
  • 8.
    Linear Search Example element -2397 18 21 5 -86 64 0 -37 Searching for -86. 8
  • 9.
    Linear Search Example element -2397 18 21 5 -86 64 0 -37 Searching for -86: found! 9
  • 10.
  • 11.
    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.
  • 16.
    Binary Search Example low Searchingfor 18. middle high -86 -37 -23 0 5 18 21 64 97 16
  • 17.
    Binary Search Example -86-37 -23 0 5 18 21 64 97 low Searching for 18. middle high 17
  • 18.
    Binary Search Example low middle Searchingfor 18:found! high -86 -37 -23 0 5 18 21 64 97 17
  • 19.
    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)