 So, today we will have a quick review of the fundamental control flow that C plus plus computing offers us and in the process we will look at couple of more examples on computing. One involving the finding out of roots, another method we will look at. The purpose of that looking at that method is that called bisection method. We shall use this subsequently to introduce an extremely important notion of searching given data in very large arrays. So, we will do the review of the control flow as you know control flow ordinarily in any program is that our dumbo or C plus plus will execute our instructions sequentially one after another. But whenever we want instructions to be executed out of sequence based on certain conditions we have special instructions which are through the if statement fundamentally where you conditionally execute one block or the other which is simple and understood very easily by everyone. But there is another mechanism of setting up an iteration which permits us to repeatedly execute a block of instructions again and again and again and that is what makes C plus plus or any other programming language so powerful. It is absolutely important that each one of us understands exactly what happens when such iterations are executed. We have seen the while and for loops we shall reexamine them looking at a variant of while in the process. Finally we will look at the array manipulations we had looked at an array last time how exactly the large arrays could be read in and how exactly we could find maximum element of a given array etc but today we shall introduce a search problem. Again the emphasis is on using the control structures both for ordinary computation without involving arrays but involving only simple variables and also using array variables. First we look at the iteration using while loop. So suppose there are there is a long program that you have written the first command says there is a previous line before that there might be some other lines of program. The next one says while condition opening brass code block closing brass so this essentially is the while loop starting here ending here and this will be followed by next list. So ordinarily C plus plus will be executing all the instructions before this then this and ordinarily it will just execute all of these ones and go ahead but because of while statement an iteration is introduced. What does the while statement do really? This is the control flow in the while loop. I had drawn this kind of flow chart by hand but it is absolutely essential that each one of you understands perfectly how exactly instructions are executed so I am very quickly refreshing our memory. Suppose there is some program instructions here they will be executed indicated by the previous line which is executed here. Incidentally this kind of flow chart had been the standard way of depicting steps in our algorithm in a traditional sense. Nowadays people do not have to draw flow chart because they can understand the program constructs in the abstract sense much more easily but for beginners it is useful to look at such flow charts. We had explained this notion that a box will contain any one of the statements like assignment or input or output or whatever that is an action. A diamond will introduce a condition which will be checked and a diamond in a flow chart will have exactly two outlets one called the true outlet and another called the false outlet. So if the condition is true the program flow sequence goes this way if it is false the program flow sequence goes this way. So effectively when we say while condition code block what it means is that this condition is evaluated if the condition is true then the code block is executed and the C++ number jumps back to recheck that condition. So this is the loop this is the while loop and this while loop will be executed as long as the condition remains true. The moment condition becomes false you will get out of this loop and go to the next line which may be the next line of instruction. This is simple but it is important that you understand the exact significance of how it is done. Very important to note the condition is evaluated at the beginning before you enter the loop and the code block is then executed only if the condition is true. If it is true the code block is executed and then you go back to evaluate the condition so that is the iteration path. Is this very clear because this simple three line instruction actually may cause the computer to execute 1 million or 10 million times the code block depending upon the condition that you have written which is the crux of the power of computing that we get and the power of expression that we get to make C++ work harder. Here is another example from our computational routines. This is finding out a route by what we call the bisection method. You are by now familiar with the roots of an equation. So let us say this is a function fx which has something like this shape. So this is x axis, this is y axis. We do not know where the 0 of x equal to 0 is, it does not matter. What we need to find out is at which point this function crosses x axis because that is where f of x is 0 at some point in x so this is the desired root as is shown here. We had done earlier a different set of argument to locate this root by finding out the root through Newton Raphson method but there is another simpler iteration method that is possible. This method will work only if we know at least one value on either side of the root which is one is positive, one is negative. What is the meaning? Consider this value which I have arbitrarily called h i and consider this value which I have arbitrarily called low l o. Notice that at the point x equal to h i, f x i is positive. At the point x equal to l o, f x i is negative. Very clearly if at one point the value is positive, one point the value is negative then f x equal to 0 must lie somewhere in between. We do not know where it lies. In this example diagram we have shown that the root lies here but since we do not know one simple method is we get the midpoint of this high and low calculate actually high plus low by 2. This is the midpoint. Does this midpoint have any sanctity no, it is an arbitrary guess. We divide high plus low by 2, we get some midpoint. At midpoint we evaluate the function again. So what happens now? In this particular example we see that the function is still positive. What it means? If the function is positive here then the root must now lie between this point and the low point. Is that obvious? So what have we done effectively? We have reduced the search space. Originally this whole thing was the search space. From low to high we were searching for the root. We have reduced that space by half because we have gone to the midpoint. Suppose this function was negative at this point what would I have done? We would have said the root lies on the right hand side. So by finding out the midpoint and evaluating the function at the midpoint we suddenly know whether the root lies on the right of midpoint or left of midpoint. In this example case the root lies on midpoint or left of midpoint. So what we should do now? We should reset HI to this point. So if we reset HI to mid and re-execute the same block of code we will again find another midpoint somewhere here. Obviously we will go closer to it. It may so happen that in this process at some point the midpoint may come on this side. If it comes on this side obviously the function value will be negative there and we know that the root must lie on the right of that. At which point we will set low to the new midpoint rather than high. Is that clear? So if this is how I wish to actually execute this algorithm I need initially a mechanism of course to calculate f of x so there must be some formula giving f of x. I do not need derivative or anything I just need f of x but I also need a good guess for the high value and a good guess for the low value. We can given any complex equation we can arbitrarily evaluate it for x so guessing is not very difficult here. But the program must check whether your guess is right or wrong. How can a program check whether your initial guess is right or wrong? Well we know that at one point the value must be positive at another point the value must be negative. We do not know which one. High need not mean that the value of the function is high. High means it is on the higher side of x low means it is on the lower side of but one thing is very clear. One of the function values should be positive one should be negative clearly their product must be positive their product must be negative if their product is positive either both function values are positive or both function values are negative so there is no root unlikely. So we start with high and low values such that function value of low multiplied by function value at high is less than 0 which is negative. You can see this condition means something if this condition is true then your initial guess is correct. Starting with this initial guess now you compute midpoint and the function value at midpoint now you just need to compare if the modulus of that function value is greater than 0 if the function value becomes 0 you have found the root but the function value may be positive or negative slightly more than 0 slightly less than 0. So you have to consider the absolute value of that function at the midpoint the absolute value which is known by Mark so absolute value of f of mid should be greater than 0. Now the trouble is exact 0 is very difficult to achieve look at this particular point you notice at this point the function f of x is 0 exactly but notice that computationally since you are calculating midpoint iteratively you may approach that point from this side of that side and there might be a f of x which is minus 0.0001 or plus 0.001 or 002. So reaching 0 is a matter of chance we do not need the root with that much exact accuracy because we cannot find it with that much exact accuracy this is a computational method not algebraic solution so exact root finding is impossible. So we define some kind of a threshold value some small threshold value or some small tolerant saying that if at the final midpoint the value of the function absolute value of the function is less than 0.001 let us say then we assume that it is the root. So we stop our iterations at that point and that is why we check if the value of the function at midpoint is greater than that threshold value we look at the next interval the next interval as I said would be either low comma mid or mid comma high it will be either this interval or this interval is that clear the program then gets written like this we write this program to solve the cubics a x cube plus b x square plus c x plus d we take in as inputs a b c d we also take in as input low high and tall tall is short for tolerance. So tolerance is that small error which we are willing to live with now this particular program should return immediately if function of low and function of high multiplication is greater than 0 as we discussed initial this means our initial value guess itself is wrong I cannot find the roots but if the value guess are ok then as long as function value at the midpoint is greater than my tolerance I will keep repeating what I will keep repeating finding out the next interval which will be set to either low mid or mid high and keep repeating this whenever I find the midpoint which is very close to f of mid equal to 0 that is within my tolerance I will get out and so I will return the midpoint which is the root if and when f mid becomes less than tall is this clear the program then writes like this so notice it says include I O stream using like namespace s t d statements which we make without really understanding them but in command we all understand that is the main program we define float a b c d low high mid f l o f h i f mid what would these be representing f l o means function value at low point f h i means function value at high point and f mid is the function value at mid point this is a notation any name I could have used I define another variable called tall which is short for tolerance in actual program when you write you should by the way write tolerance midpoint you should write variable names which make immediate sense to the reader the only reason why I am not doing it here in the class is I have a limited space on the slide otherwise the e n c of tolerance may go outside the screen so I am using the short names but that is not indicated practice so I first ask for a b c d values and then I collect a b c d values then I ask for low high and tolerance values the three input values and I collect l o h i and t o l values from input this is the initial setting so I got a b c d as the coefficients of the quadratic and I got the values of low and mid high which are initial cases followed by the tolerance initially itself what do I have to do remember I have got low and high but I have to first check whether function at low and function at high are at the opposite side of the x axis otherwise I can't find a rule so I evaluate f l o as a into low into low into low plus b into low into low plus c into low plus d this is a crazy way of calculating cube root and square root but this is the easiest way for c plus plus then we calculate f h i again similarly this time we use high instead of low so we got f h i and f low initially now we check if f l o into f h i is greater than zero then what it means either both are positive or both are negative so I get out I say error in high low backslash n return one notice that ordinarily in the main program whenever we write return we have encountered return zero so far and we have said we routinely write it this time we say return one this is actually a neat technique used by programmers to return to the operating system dumbo because main program will return to the operating system telling the operating system that look some ghapla happened during execution of this program I have not executed the program properly is easiest way to indicate is to give a non-zero value actually later on when we write very large programming systems we can use such values it could be one it could be 110 it could be 270 any number of values that you can think of but the returning operating system or a program to be another program to be this function returns can make use of this value to take intelligent action if the value is one it means I did not give high and low correctly if the value is to it may mean something else etc etc we shall discuss these things later but I this is just to for you to understand that if high and low are not proper in the context of my bisection method then I am quitting the program the program execution will not go further please note that ordinary we have said that if I execute an if statement here there is an if statement then if this condition is true only these statements will be executed otherwise subsequent statements will be executed even if this condition is true these statements will be executed and then next statements will be executed that is our understanding of if but if within inside any code block if there is a return statement it supersedes anything else that program says the return statement says forget everything get out go back so this is like break just get out of this whole mess and go back to the operating system don't execute anything further return is therefore an extremely powerful statement that is the reason why you would usually have the return statement only at the end of the program never anywhere in between but this is a special case I can't proceed any further and therefore I need to return so that is how I do it well how else I could have done that instead of returning here I could have put the brass and put an else and return the entire program under that else this is a simpler way of doing it now this is where I will write the main program so the main part of our code will come here what is that main part you know that bisection method finding out the midpoint iterating etc etc obviously at the end of this I would have found out the root and what would be the root the midpoint the last midpoint used and that is what is being put out here if we come ever to this point then we return with zero telling the operating system dumbo that everything is all right so is the main line of code clear first I read the values of a b c d then I calculate function value at low function value at high then I check whether the function values have opposite side so that there is a possibility of my finding a root in between if it is not so I print an error message on my terminal and get out return one means get out shouting bad things one one not zero otherwise I come out here I will write the main part of my code here and then I will print the value of me what is the main part of the code that is shown in the next slide so look at this slide carefully because this shows the main program using which we iteratively go to the root the first one initial values for mid and f mid starting with low and high I calculate the midpoint very simple mid is equal to low plus high by 2 having got the midpoint I will now calculate the function value at the midpoint which I have represented by a variable name f mid so I said f mid is equal to same thing a into mid into mid plus b into mid into mid plus c into mid plus d this will give me the cubic function value at that point after that while f a b s in bracket f mid greater than tall what does this statement do this while statement I am setting up an iteration but what does that condition say remember the condition that we wanted was to check the absolute value of the function value at that midpoint the function value at midpoint we have calculated and we have called it f mid this f mid therefore is a variable name however this f a b s is not our variable name this f a b s is the name of a standard function a standard computational function which comes ready made with c plus plus can you guess what does f a b s do calculate the absolute value very simply if f mid was positive that will be the absolute value if it was negative the negative of that will be the absolute value we could have written a complicated statement also here but this is much simpler again tells you the importance of having such ready made functions available as assistance to us f a b s is one such function so when I say f a b s f mid greater than tolerance remember we don't say greater than 0 because we may never reach 0 that is why we took the tolerance value if that is so then what am I supposed to do I am now supposed to decide whether the new range where I search is between low and mid or is between mid and high so very clearly that will depend upon what whether again I will check a flow into f mid is greater than 0 if a flow into f mid is greater than 0 what does it mean that both low and mid are such that function value is either on the positive side or on the negative side on same side so that means the f mid is still taking me or keeping me on the same side as a flow if that is so then I must advance a flow to f mid low to mid so that is what is being done I reset low to mid and a flow to f mid because I have found that value already if that is not so if the signs are opposite that means now midpoint has crossed over from the low side so obviously the range I have to search for is between mid and high so this time I will set high to the midpoint and f hi to f mid I'm sorry the other way round please execute these statements in your lab or by hand yourself to convince yourself that the algorithm actually works correctly but this is a simple mechanism all that I am doing is I have a range they don't really matter but let's stick to the nomenclature that we have used so this is low and this is f of low this is high and this is f of high and let's say this is my midpoint so if this is made and this is f of mid so when I compare the value of the function at low and the value of the function at mid I know they are of opposite sides which means the root must lie between these two points so that means in this given situation I must now search for this range of values please note originally I was searching between low and high now to search between low and mid so what should happen high should become mid and the function value of high should become function value at mid and then the new search begins exactly using the same logic this is what has been done here if a fellow multiplied by f mid is not greater than zero that means the condition that we saw saw in the example then I have set high to mid and f hi to mid on the other hand if mid point is on this side I have to shift low which is what is done here so is this clear how it is done very simple then I calculate the new midpoint and I calculate the new f mid because now there is a new midpoint and with this my vile statement ends so I will go back again and again check the absolute value of f mid this simple code will cause the computer to reiterate around this body till it finds out the root to a desired accuracy you can change the value of tolerance and see what happens but that is the main part of the code so please note that the vile statement is an extremely effective control structure which permits us to set up an iteration during which any given code is executed again and again and again while a condition remains true and you must remember what happens first the condition is checked only if it is true you enter the code block and then keep on repeating the condition is correct there is another way of iteration however which is called a do vile loop very similar to our vile iteration this we have not seen so this is a new thing in this do vile loop we first write the word do then put an opening blast then write the block of code which is to be iterated and finally at the end of that block we say vile condition in plain English it means do this while this condition is true the difference is there is no condition checking at the beginning do this is the first statement so in this kind of control structure c++ will execute that block first time automatically without checking anything then it will check the condition if the condition is true it will go back and do this again and again and again when the condition becomes false it will fall down and go to the next step a slight variation therefore as compared to the other vile that we had seen so this is the flow chart for that unlike in the previous vile execution I come from the previous slide and I automatically execute the code block once having executed I check the condition is the condition true I go back again and execute the code block if it is still true I go back again and so on but the moment condition becomes false I come out and I go to the next line I think it is very simple and straightforward so it is clear try to rewrite the previous program using do why just so you understand how the condition checking has to be done differently how initialization has to be done differently etc etc the third way of setting up iterations is through counting this is just a confirmation of what we just said these are the features of do why loop I have written the thing again after previous line I said do code block while condition and the two important things code block is executed at least once no matter what the condition and the condition is evaluated only at the end if that condition is true you go back to again execute code block this is the variation so this is the fall loop can anybody tell me what it does well I have a comment there which says countdown so what does countdown do it starts with int man it introduces a integer variable called count for count equal to ten count greater than zero count equal to count minus one do the following what see out that is print out the value of count followed by a comma and keep on doing this at the end it says end of line value of count is count so I am printing count many many times what will this fall loop do it will start with count equal to ten execute this once then we will reduce count by one because instruction at the end is say count minus one so count will become nine then it will become eight etcetera etcetera until the condition is valid it will keep executing it but at the end it will come out of the iteration and just to mark the end of iteration we have put a output statement there so here is a quiz when we execute these code lines the value of the count printed last one b ok anybody a nobody a so it can be eleven because I am counting down common sense b how many will say b several people will say b because b is one what was the reason well anybody for c large number of people for c anybody for d nobody for d nobody thinks it will be minus one ok a and d are obviously not correct answers and I am glad everybody got it the confusion is between b and c let us look at when you will come out of the loop please note the condition here says if count is greater than zero then keep doing this that is how we first place you got here when count was ten so when count was nine you will come down count was eight you will come down is the final value printed by this statement was to be one and one is greater than zero there is no way you could have come out of the loop if the last value of count was one then that value is greater than zero so you would have necessarily enter the loop for the last time when you execute this for the last time and you try to go back again that is when count is decremented now count becomes zero and when count becomes zero since zero is not greater than zero the far loop is terminated so when you come out and use this line to print out the value printed will be zero so c is the right answer please note that now after having heard this explanation things seem so simple but there were several of us who did not realize it instantly you must realize it instantly and therefore must understand perfectly the flow of control that happens on a far loop so let us revisit the control flow in the far loop it has four parts the first part is initial expression the second part is condition and the third part is end expression all the three are contained as a specification in the fourth statement the fourth part is the body itself which is to be executed again and again and again it is very important to understand the sequence in which these four parts are executed very very important there are three parts all written in the same line as four but not all of them are executed at one stretch and that is the crux of the far loop so let us look at the flow chart for this so notice there is a previous line from which you come to the far statement when you come inside the far statement the first thing that you do is execute the initial expression what was the initial expression what was the initial expression in our program count equal to ten so the count is set to ten here after this you execute the condition evaluation what are the condition is count greater than zero if the count is greater than zero condition will evaluate to true and then and only then you will get inside where you will execute the code block if it is not true then and only then you will come out of the entire far loop and go to the next line if you come inside the code block because the condition is true then you do not execute the third statement which is the incremental decrement you first execute the code block the iteration block is first executed so this is the crux of the problem the far statement says initialization condition and end of the loop expression but all the three are not executed in sequence the initialization is executed then the condition is checked after which if the condition is true code block is executed and only after the code block is executed you then execute the end expression what was the end expression in our case this time count equal to count minus one so count gets decremented only after executing the code block but before quitting so only after end expression is executed you actually go back and check the condition for the next loop and keep repeating this so now it is clear why that if the condition says count greater than zero when count is one actually one is greater than zero so you will enter here execute that code block now you will say end expression count equal to count minus one count will now become zero now when you go back condition count greater than zero is not satisfied because zero is not greater than zero and therefore you will come out and when you come out and in the next line you try to print the value of count it will be one less than what with what value the last loop was executed it is one less because the decrement is count equal to count minus one it could be if we had said count equal to count minus 23 it would be 23 less whatever the last value with which the loop was not executed will be the value that you will come out is this very clear everybody clear of this fine so we now go to our favorite array elements this was the program to find out the maximum of n numbers we just read n numbers from our input in a 100 max n i c in n for i equal to zero i less than n i plus plus this is the for loop I read the value of n numbers and I put them in an array so I read a 0 a 1 a 2 a 3 to find out maximum I start with a 0 for i equal to 1 i less than n i plus plus if a i is equal to max I say max is equal to a i and this simple iteration will permit me to examine each and every element sequentially at the end whatever is the value of max that is the largest value in that array it doesn't matter whether the array has seven elements or 7000 elements or 7 million elements the program will work fine is that clear you can see how the for loops are a must for handling arrays because array elements have to be typically examined from 0 to n or something like that and typically we have to examine them in sequence so this kind of for iteration for i equal to 0 to n for i equal to 5 to something whatever whatever is going to be most common while examining array elements and that is the reason why this loop is written like this suppose I wanted to find out who is the student who has maximum marks how will I do that assume that only one student has maximum marks that is the assumption so how do we find out the role number of the student who has maximum marks same program how will I modify I of course cannot use a 100 and so on but I can use the array that I had seen last time you remember this I just listed this array this in fact will be there in your lab sheets when you go there as a separate file text file you will also have the program which we wrote last time so that you can execute it to read that entire file into two arrays one array will be called role another array will be called marks imagine now we are running out the maximum of marks here so what will I do I will set up initially max is equal to right that is the first task so that means max will be value 72 now I will have my for loop which will say for I am using a different terminology where n STD stands for number of students actual variable use was different so what I want to do I want to keep examining every student marks keep comparing it against whatever max I have arbitrarily chosen and if that is different I want to reset it so what will be the instruction here if I am sorry this should be square brackets and not normal brackets if max I is greater than max what should I do I should set max is that correct now anytime I reset marks I know the position at that point in time so for example when I get the first number 72 I set it here I then examine for I equal to one please note that the value of I is 0 1 2 etc because the arrays in C plus plus start with 0 so that is why I have set mark maximum to mark 0 first in the first iteration marks 1 will be compared with mark so marks 1 is 45 is 45 greater than 72 no so nothing will happen I will carry on this is the end of my for loop however when I come to the next student 1 0 0 3 has 91 marks 91 is greater than max so max will now become 91 but I do I not know at this stage that this maximum corresponds to the second row number whatever it is it is therefore prudent to say position equal to is that correct so I not only mark the maximum I mark the position where the maximum is at that time I know carry on with this iteration I go to I equal to 3 is 86 greater than 91 no I will not do anything in fact if you look at these sample marks but had there been somebody 94 marks then I would have seen reset maximum to mark when I come out of this for iteration you will agree that max actually has the maximum marks but role I contains the role number of the maximum marks you agree with that how can you agree with that if I say this what is the value of I when I come out here I will be actually one more than number of student so I will be here and what is value there abracad abracad you want to print this funny role number remember we have marked the position position is therefore the index into this array what was the position in this case if maximum was 91 the position would have been 2 because that was the value of I at that point and that we have captured in position so correspondingly I must not write this but I must write what role indexed by position is this understood position was the index inside the array where the role number securing maximum mark says because that was the time when I had located 91 as max and therefore that time whatever was the value of I that is the position of this don't matter where I go further if I remember max I will remember the position if subsequently someone scores 98 then I will change max but I will also reset position to whatever value of I was at that point in time remember I is a loop variable I have no business outputting I for any reason whatsoever after this because the value of I is controlled and used for controlling the loop it is not of significance what is of significance is some position some value of I which was relevant while traversing the loop and that value I have to capture separately and use that so now if I say see out role position and max can I write instead of max can I write marks position yes or no so he has correctly pointed out the equal to symbol should not be only less than because the last element of the array to be examined suppose there are 7 students the last element will not be positioned as 7th element but as 6th element so the last element I examine must be one less than number of students sorry for the mistake very good for you to point it out so is this clear now how the loop will run this is how you need to execute it suppose I want you to find a given role number and his or her marks in this array how will you do that this is the array which I have given the sample array might look very small but in general the data that we handle for students could be very large for this class itself there will be 850 entries into this for students appearing for higher secondary examination of a state let's say Maharashtra how many students will be there about may be 4 5 10 lakh students when you have such large numbers how do you search for marks for a given student one simple way is just like we found max you read a given role number first and start examining the entire array from 0 1 2 3 4 5 is the given role number equal to this role number if it is then you have found it print the corresponding marks right this is called the linear search and I have a program which shows you that it's called find marks dot cpp so given a role number find the marks I define role number array marks array number of students and students in given role found marks position I and students is get data role comma marks that is the function that we wrote last time without bothering how exactly it does but it will read from the file and stuff my two arrays with role numbers and marks I need to appreciate only that at this juncture that means from this point onwards I have number of students given by students I have those many role numbers and those many marks now I am saying I read the given role somebody gives me a role number 1008 so I want to examine each element of the role number 1008 I should print the corresponding marks so what am I doing inside this for iteration I am just checking if role number I is equal to given role if it is I say found marks is equal to marks I if it is not I go back to the next iteration increase I by 1 and look at the next number look at the next number look at the next number when I come out whatever is the value of found marks will be the marks for the given role and I output this I will leave you with just two questions which I want you to think about will answer these tomorrow and you will also look at these in the lab first question what happens if the role number does not exist after all role numbers need not be sequential or somebody might have forgotten to insert the role number or a role may not exist for example if you have seen that list 1005 does not exist there and suppose I give as input 1005 please give me the marks what will this program produce it will take given role as 1005 and examine the entire array it will not find 1005 so what will it do whatever is the junk value in found marks because I initialized it so what will that labelled found marks will contain we do not know if it contains 100 what will it mean role number 1005 or 100 marks without appearing for example it should not happen right therefore if I do not find that fellow at all I must have an if statement afterwards how do I have an if statement I assume that all marks are zero at the most but nobody can score let us say minus 20,000 marks 20,000 is arbitrariness what I will do then is I will set found marks to minus 20,000 to begin with now I will go through the array search if I find actual legitimate marks of some student found marks will change but if at the end of iteration if found marks is still minus 20,000 I will say sorry the role does not exist do you agree that that is the prudent programming that is the one question I have told you the answer the second question is tougher if instead of 7 students in the array if I have 7 million students how much Godagiri are we making C++ number 2 how many comparisons 7 million comparisons imagine that all these 7 million numbers were not inside the computer's array but we are sitting in the desk and every time to make a comparison number has to go to the desk and read one number when it does not take a few microseconds but it takes maybe 20, 30 milliseconds to read a number from the desk multiply 30 milliseconds by 10 million to search for one role number even the fastest computer could take very long time clearly this searching strategy is not good enough now if the marks which are given to you and the role numbers which are given to you as in this example if the role numbers are arranged in this order all 1 million role numbers are arranged in this order does it not give you an idea that I can consider the first role number to be low role number the last role number to be high role number and the given role number is somewhere between these two can I therefore not use the bisection method of finding routes applying to an entirely different problem there is no function tossing positive negative values etc but I have the gem of an idea that if I consider the first role number to be low and the last role number to be high the only thing that I require is that array contains role numbers in strictly increasing order is roughly equivalent of saying that the function crosses 0 somewhere between low and high now can I not find the midpoint but what is the equivalent of midpoint here the midpoint here will be the middle position not the middle value what is the middle position n student plus 0 by 2 or n student minus 1 plus 0 by 2 whatever low and high I find out the middle position I will check whether the middle position now not just whether it is equal to the given role number but is it greater or is it smaller depending upon whether it is greater or smaller I now have the upper part of the array to be searched or the lower part of the array to be searched I have reduced the search space by half next iteration I will reduce it by half third iteration I will reduce it further by half since these are very finite numbers that we are talking of ultimately I will be looking at only one particular position if at that position the role number does not exist the joker has not enrolled for my course but if that number exists I have found it and how many comparisons will I have to make if n is the number of students log n to the best 2 is the number of comparisons I have to make because every time I am dividing this by half half half what is the log 10 million to the best 2 so if you have 1 crore people or 10 lakh people instead of making 1 crore or 10 lakh comparisons you can make so few comparisons this is called binary search we shall be doing this binary search implementation during this lab for which we will have a lab problem