0% found this document useful (0 votes)
4 views

Binary Search

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Binary Search

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Linear Search

vs
Binary Search

Dr. Kumkum Saxena


Linear Search
 Your code should look something like this:

int search(int array[], int len, int value) {

int i;
for (i=0; i<len; i++) {
if (array[i] == value)
return 1;
}
return 0;
}

Dr. Kumkum Saxena Linear Search vs Binary Search page 2


Linear Search
 Analyze code:
 Clearly, if the array is unsorted, this algorithm is
optimal
 They ONLY way to be sure that a value isn’t in the array is
to look at every single spot of the array
 Just like you can’t be sure that you DON’T have some
piece of paper or form unless you look through ALL of your
pieces of paper

 But we ask a question:


 Could we find an item in an array faster if it were
already sorted?

Dr. Kumkum Saxena Linear Search vs Binary Search page 3


Binary Search
 Array Search
 We are given the following sorted array:
index 0 1 2 3 4 5 6 7 8
value 2 6 19 27 33 37 38 41 118

 We are searching for the value, 19


 So where is halfway between?
 One guess would be to look at 2 and 118 and take
their average (60).
 But 60 isn’t even in the list
 And if we look at the number closest to 60
 It is almost at the end of the array

Dr. Kumkum Saxena Linear Search vs Binary Search page 7


Binary Search
 Array Search
 We quickly realize that if we want to adapt the
number guessing game strategy to searching an
array, we MUST search in the middle INDEX of
the array.
 In this case:
 The lowest index is 0
 The highest index is 8
 So the middle index is 4

Dr. Kumkum Saxena Linear Search vs Binary Search page 8


Binary Search
 Array Search
 Correct Strategy
 We would ask, “is the number I am searching for, 19,
greater or less than the number stored in index 4?
 Index 4 stores 33
 The answer would be “less than”
 So we would modify our search range to in between
index 0 and index 3
 Note that index 4 is no longer in the search space
 We then continue this process
 The second index we’d look at is index 1, since (0+3)/2=1
 Then we’d finally get to index 2, since (2+3)/2 = 2
 And at index 2, we would find the value, 19, in the array
Dr. Kumkum Saxena Linear Search vs Binary Search page 9
Binary Search
 Binary Search code:
int binsearch(int a[], int len, int value) {

int low = 0, high = len-1;


while (low <= high) {
int mid = (low+high)/2;
if (value < a[mid])
high = mid-1;
else if (value > a[mid])
low = mid+1;
else
return 1;
}

return 0;
}
Dr. Kumkum Saxena Linear Search vs Binary Search page 10
Binary Search
 Binary Search code:

 At the end of each array iteration, all we do is


update either low or high
 This modifies our search region
 Essentially halving it

Dr. Kumkum Saxena Linear Search vs Binary Search page 11


Binary Search
 Efficiency of Binary Search
 Analysis:
 Let’s analyze how many comparisons (guesses) are
necessary when running this algorithm on an array of
n items
First, let’s try n = 100
 After 1 guess, we have 50 items left,
 After 2 guesses, we have 25 items left,
 After 3 guesses, we have 12 items left,
 After 4 guesses, we have 6 items left,
 After 5 guesses, we have 3 items left,
 After 6 guesses, we have 1 item left
 After 7 guesses, we have 0 items left.
Dr. Kumkum Saxena Linear Search vs Binary Search page 12
Binary Search
 Efficiency of Binary Search
 Analysis:
 Notes:
 The reason for the last iteration is because the number of
items left represent the number of other possible values to
search
 We need to reduce this to 0.
 Also, when n is odd, such as when n=25
 We search the middle element, # 13
 There are 12 elements smaller than 13
 And 12 elements bigger than 13
 This is why the number of items is slightly less than ½ in
those cases

Dr. Kumkum Saxena Linear Search vs Binary Search page 13


Binary Search
 Efficiency of Binary Search
 Analysis:
 General case:

 After 1 guess, we have n/2 items left


 After 2 guesses, we have n/4 items left
 After 3 guesses, we have n/8 items left
 After 4 guesses, we have n/16 items left
 …
 After k guesses, we have n/2k items left

Dr. Kumkum Saxena Linear Search vs Binary Search page 14


Binary Search
 Efficiency of Binary Search
 Analysis:
 General case:
 So, after k guesses, we have n/2k items left
 The question is:
 How many k guesses do we need to make in order to find
our answer?
 Or until we have one and only one guess left to make?
 So we want to get only 1 item left
 If we can find the value that makes the above fraction
equal to 1, then we know that in one more guess, we’ll
narrow down the item

Dr. Kumkum Saxena Linear Search vs Binary Search page 15


Binary Search
 Efficiency of Binary Search
 Analysis:
 General case:
 So, after k guesses, we have n/2k items left
 Again, we want only 1 item left
 So set this equal to 1 and solve for k
n
k
1 n2 k
k  log 2 n
2
 This means that a binary search roughly takes log2n
comparisons when searching in a sorted array of n
items
Dr. Kumkum Saxena Linear Search vs Binary Search page 16
Binary Search
 Efficiency of Binary Search
 Analysis:
 Runs in logarithmic (log n) time
 This is MUCH faster than searching linearly
 Consider the following chart:
n log n
8 3
1024 10
65536 16
1048576 20
33554432 25
1073741824 30

 Basically, any log n algorithm is SUPER FAST.


Dr. Kumkum Saxena Linear Search vs Binary Search page 17

You might also like