 Welcome back in the previous segment, we defined a search problem and we also defined binary search. Now we are going to do sample execution through our code. So this is our code and I have just removed the comments so that I can fit my code into these slides, it is really the same code. So we are searching this array over here, so starting index is 0, length is 8 and we are searching for 11. So the first call is this call, so we search a 0 8 is our first call. So how does this call execute? So when this call starts executing s has the value 0 which comes from this argument itself and l has the value 8 again from the argument and x has the value 11 from the argument. So we are at this point then we do a check is l equal to 1, well l is 8 and certainly not equal to 1, so this statement does not execute, so we come to this statement. So at this point we are defining a new variable h whose value is going to be l by 2, so h becomes 4. Next we execute this statement, so we check whether what is the relationship between x and a of s plus h, well what is x, x is 11, what is s plus h, so s is 0, h is 4, so s plus h is 4, so what is a of s plus h, well this is a, so 0, 1, 2, 3, 4, so a of s plus h is 10. So the question that we are asking is this, is 11 smaller than 10, well that is not true, 11 is not smaller than 10, so this comparison comes out to be a false, so therefore we are going to make this call. So this is the call that we make next, s of a s plus h l minus h x, but what are the values, the values are s plus h is 4, l minus h, what is l, l is 8, h is 4, so it is also 4 and x is 11. So a new recursive call starts, so what happens in this recursive call, well we are again going to start from here, so s in this recursive call is 4, which is we got from our arguments, so s is 4, l is also 4 and x is 11, so in this new recursive call these are the values, so we are going to do the same thing again. So what happens over here, so we first check if l is 1, well l is not 1, l is 4 now, so in any case this statement is not going to get executed. So we now create h equals l by 2, so h, so l is 4, so h becomes 2. Next we are going to ask how does x compare to a of s plus h, so what is the value of x, again it is 11, what is s plus h this time, so s is 4, h is 2, so s plus h is 8, so a of s plus h is 0, 1, 2, 3, 4, 5, 6, so this is 30, so we are comparing 11 with a of 6 which is 30, so 11 is smaller than 30, so this is true and therefore we are going to, so this is true, so we are going to make this recursive call, so as h, x gets called, so what are these values, so these values are 4, s is 4 over here, h is 2 and x is 11, so the values that are going to get called, that the call that we are going to make is a 4 to 11. So let us examine this call a 4 to 11, so what happens in this, so again the variables in the activation frame will take values, s will take value 4, l will take value 2, these come directly from the arguments, x also will take value 11, then we are going to execute, we are going to execute this statement, but l is not 1, it is 2, so nothing will happen, then we move on to this, what will happen to h, well h will become 1, half of l and then we are going to execute this, so we are going to ask is x less than a of s plus h, okay, so what is x again, it has stayed 11 all the time and s plus h is now 5, so we are going to ask a of s plus h is 0, 1, 2, 3, 4, 5, so we are going to ask is x less than a 5 or 12 and this is also true, okay, so if it is true then we are going to make this call, so in this call s is 4, h is 1 and x is 11, so we are now going to start another recursive call, so again we start from this point and because of the argument list s is going to get 4, this value l is going to get 1, this value and x is going to be 11, so next what is going to happen, we are going to check if l is equal to 1, but yes this time l is indeed 1 and therefore we are going to return whether a of s of x is equal to x, okay, so what is a of s, so s is 4, so 0, 1, 2, 3, 4, okay, so a of s is x, yeah, so l is equal to 1, it is true and therefore we are going to return whether 11 is equal to a of 5, okay, so is 11 equal to a of 5, well so a of 5 is 12, so this is not equal and therefore we are going to be returning false and all the previous recursive calls will just return the same thing forwarded back, send it backwards and so this is the value that will go into main as we expected, all right, okay, so let us do a quick demo of this, so the function is bsearch, so let me show it to you, okay, so this is bsearch and bsearch is the same as what I have shown you, but the only thing is I am writing over here what the arguments are, okay, so just to see how the function executes, so let us compile it, so this is how bsearch ran, so first it was called with argument 0, 8, 11 and of course the argument a is there, but I am leaving that out, then 4, 4, 11, 4, 2, 1, 4, 1, 11 and if you see what we executed, this was exactly the sequence and even here 0 was returned as happened in our execution sequence, the execution that we did by hand as well, okay, so let us move on to the presentation, so a few remarks, binary search is based on a simple but powerful idea, but even though the idea is simple, the details are somewhat tricky and experience shows that even professional programmers may make mistakes when writing binary search, okay, so the mistakes are of this kind, so when we are comparing should we be asking less than or equal to or less than, so then there is this question that if the region that we are searching is odd length, then integer division behaves in a slightly different way than we might expect, so we have to be careful about that and then the sub ranges to be searched have to be specified carefully, so should it be s through h plus l minus 1 or s through h plus l whatever it is, so the way to get it right is to write down precisely what the function is expected to do, okay, so at least once you write down what you want to happen then you can check whether that is happening, so at least if you have to be sure as to what you want, so please write the specification very carefully and make sure that you are getting these minus ones, minus ones correct, okay, all right, so now let us analyze this and check, look if this is so carefully, how to be written so carefully, is it worth going to the trouble, so in other words is binary search better than linear search just going through the elements from left to right, so if you consider an array of size 1000 in the second call we search an area of size 500, in the third call we search an area of size 250 and so on, so every time the size of the array we are searching is roughly having, so that means in the 10th call, so if you do this 10 times you will get to an array of size 1, so at that point we know we do not have, we do not recurse further and we can just return and whatever we return at that point is returned all the way back to the main program, so what does this mean, so time taken in this case is about 10 calls worth of work and each call is fairly simple as opposed to that in linear search we would do 1000 comparisons, now let us see what happens if you were doing million elements searching through million elements, then here we would have million comparisons and how many calls would we have over here, interesting as it may, you may find the number of calls over here is only going to be 20, so binary search is much, much more efficient especially if the size of the array being searched is large, is large, so put it another way this is log n, so the time is proportional to log n and this is n, so rather than n, so definitely a big difference, so what have we discussed in this segment, so we said that binary search is a very fast way to search for a key in a sorted array and in fact this notion of dividing by 2 is something that we have encountered, so we did this in the bisection method, so even there we were probing in the middle of the range, so the range here is sort of the size of the array that we are searching, searching over, then if you are likely to search an array frequently it seems that you should first sort it and then search it because the time to sort the array will be compensated for by the time saved in the subsequent searches, so this raises the question, how do you sort an array in the first place, so this is what we are going to discuss in the next segment, so let us take a break.