 So today what we are going to discuss, we have seen for example, organizing large number of elements in our arrays, one or two dimensional arrays. There are occasions when we have to search for some specific values in a large array. Large or small, it does not matter, but searching some given element in an array is a requirement that we face often. Today we shall see how exactly such searches are conducted. A specific problem that we will discuss is a problem of balancing load by exchanging some packages between two trucks, which to begin with have imbalanced load. We will of course use arrays to represent various package loads and we will see what kind of search if any is involved in such situations. We already have seen the arrangement of organizing let's say role numbers in one array and the corresponding marks in another array. Given a role number, we want to search for the marks. How best to do that? Ordinarily there are two types of searches in the second situation, one which is called a sequential search, that is you just scan the entire array, look for the given element, you find it that is good, if you don't you announce you haven't located anything. Now that search requires a complete scan of the array. We are motivated by reducing the search time. Since we have understood the notion of sorting arrays, we will see if an array is sorted, can we search a number faster. I had given you an example of searching for the results in a newspaper and the fact that we do apply faster search when we look for our role number in the results. We will try to see what is that method, it is actually called binary search. But to appreciate the binary search better, we will look at another computational function. This time we will revisit the problem of finding a root, we have seen the Newton-Raphson method earlier. Today we will discuss the bisection method. Are you familiar with the bisection method of root finding? Now we will look at that and we will see the similarity between that method and what we call the binary search. Of course in the end I will describe the proposed structure of the mid-same exam paper. So this is the problem. We imagine that there are two trucks and they contain packages. So we assume that there are two trucks basically which contain such packages. There are different number of packages in each truck and currently the trucks or wagons or whatever you may want to call have imbalance in the load. That means one of them carries larger amount of load, the other one carries smaller amount. We want to balance these. The volume of space that is available in each of the trucks is this color more visible. So the volume of space available is such that I cannot change arbitrary number of packages between these trucks. So I am restricted to exchange exactly one package. The question is the packages have their own weights so we do not know whether after exchanging packages the trucks will be balanced or not. We may not find any pair of such packages or we might find more than one pair of such packages. Point is how do you determine? Imagine instead of these trucks we have ships containing thousands of packages and the load balancing involves not exchanging exactly one pair of packages but an arbitrary number of packages. In fact later on you will study a whole lot of optimization techniques and problems where issues such as these would be discussed. So consider this to be a precursor. Here of course the emphasis is in trying to understand how we can search for some desired elements into arrays but first we have to identify what do we mean by desired element. Here for this problem we may perhaps use an array to store all the weights that you see in this truck. Let's say m packages so m weights and there are n packages here in this truck. So you can have two arrays of having elements m and n and we can see whether after having read the data about the package weights can we find what we wish to exchange so that the loads are similar. The problem stipulated is that we should be able to do so by swapping exactly one pair of packages not more. So we say that the best bet is to read the data into two arrays. We make some assumptions. We make an assumption that the packages in each truck can be maximum 100 and we will use integer arrays a and b of size 100. We will use m and n to represent the number of packages in each array. Having read these arrays how do we find elements to be swapped. So if you look at the weights let's say this truck 1 the total weight is someone and the truck 2 the total weight is sum 2. Assume that we have located two packages which if swapped will make these sums equal. Obviously currently they are not equal. If they are equal the problem is solved. Let us say that there is a load here x which if swapped with some load y that is x goes here and y goes here. Then the sum should be same if the load is balanced. What it means is that someone minus x because x is leaving this truck plus y because y is getting added to this truck must be equal to sum 2 which was the original sum minus y because y has left this truck and plus x. Not that while translating any problem for constructing an algorithm and then writing a program this kind of mathematical or formulation jugglery we have to do. Real life problems don't come up with such expressions automatically. But we have now found out an extremely important clue to how and what we are searching for in the arrays. We are effectively searching for some element x in this array for which there exists another element y in the other array such that if they are swapped then this relationship is true. Is that understood? This is what we are looking for. So we are stating it in a slightly more organized formulation that there may exist a pair of elements of the type x and y such that x is an element of array a and y is an element of array b and if these are swapped then the resultant arrays will have the property that sum of the elements in new array a is equal to sum of elements in new array b. Having concretized the formulation how do we search? So one possibility is that we set up a iteration to go over the elements of a. We have an array a, we have an array b suppose we take the element of array a, assume that that is a possible contender for value x. What we have to do is for that element we have to search the entire array y to find out whether there exists a y which can be exchanged. If we don't find any then we go to the next element of array a again do the same thing. Third element of array a again do the something and so on. The algorithm is that way very straight forward once we identify what is to be done. But I have tried to use this example to illustrate one more aspect which I did not find in the answers that you have attempted in the take home test and that is properly writing your programs indenting for example nested iterations or nested blocks writing commands is not exactly a normal thing that occurs to you under examination time constraints am I right? If that is so then I would even venture to say that it does not occur to you even when doing your laboratory assignments. Go back to check your laboratory assignments do you follow these rules? No there is another type of time constraint namely the lab type. The thing is there is no period in life when there is no time constraint but certain basic things have to be done because if you don't do these with small programs you will never do these with larger programs. In the mid semester exam it will be obligatory to properly indent your program and it will be obligatory for your program to at least have some minimal commands please do not forget while we write our programs for the computer to understand for the compiler to translate but we also write our programs for other human beings to read those programs and understand them. Imagine if all the textbooks in programming contain programs written in the same muddled fashion that you write would you be able to understand them. So that is a necessary prerequisite discipline that we must build in as prospective programmers and therefore I have taken this example to sort of give one model I have tried to do the same thing in the model answers that I have posted please read them please also read these slides when they get uploaded. So this is a program of course I don't expect you to be so elaborate in the exam conditions but certain things must be mentioned in your program for example this is only the statement of the problem you need not write it because that is contained in the question itself but the fact that there is certain input that the program will read and there is some output that the program will produce should it not be written at the outset in fact if you write it at the outset you will be reminded while writing your program that you have committed to collect this input and you are committed to write this output. Does your program contain those statements properly or not? So this becomes a cross check for you yourself here it says input is values of feminine weights of individual packages in each truck packages are assumed to be numbered 0 to m minus 1 and 0 to n minus 1. This particular aspect is not used anywhere in the program this is how the program develops initially I think something and the comments is the best way to write down your thinking at the early stage of formulation of your algorithm. Not later after you have written the program this is weights of one package from each truck such that their exchange will have both trucks with same load so this is another output that the program is expected to produce do not forget that when you are writing a complete program you cannot start your program with int main a complete program must have the preprocessor statements I have just for the sake of it I have included c math which is a standard c like library of mathematical functions there could be other libraries which could have been included whatever is required either you know them from the context by reading the c plus plus manuals or you are told about them in the class or they are specified in the paper but they must be included after any major data structure declaration there is a small command these areas hold values of weights of packages in two trucks someone and some two are sums of weights in each truck x and y are weights of packages which can be potentially exchanged and all these other variables i j diff found count indexing another temporary variables again to what extent you elaborate information with commands of course depends upon the time that is available to you but you will agree that zero command does not mean anything because human beings can't assimilate your intention behind using certain names of data structures automatically unless you tell people that this is what you intend to do observe how a separate comment is written to say this is input section it is useful to consolidate all your input statements in a program at one place particularly when you solve larger problems we had seen this example earlier once when we had a whole lot of input health check being done for example on high precision numbers being read whether individual digits are between zero to nine or not whether the number of digits are less than hundred or not and if things were not right we were using a flag called input flag if the flag is not zero we presume something is wrong when you have to read a very large amount of data then while you cross check for some error at every stage many times you do not want to immediately abandon the program execution just because you have found some error because you would like to continue to read the remaining part of input to the extent that you can and want the user once and for all for all the possible errors that the input has otherwise the user will correct one error in data you run it and now the next element of data has some problem etc etc in any case it is useful to create an input section which will have appropriate messages before every input statement and of course the correct input statement for example for i equal to zero to m minus one I am running an iteration to read values of load packages load of packages m minus m packages so I am inputting t1 i I am immediately outputting that observe a small blank here after this because in an iterative fashion when I execute individual loads will come after another get printed and if there is no blank in between they will not be readable so any output that you produce should at least be readable again I have mentioned that we have not studied formatted output properly yet but even the plain C out statement permits you to write your output decently the minimum decency should be observed at least in the programs that you write for your labs and so on and of course there is no harm if you do the same thing while answering questions in your exams so this is the input section then this is the main algorithm since we have to equate the sums of loads into two trucks we begin by finding the existing sums of the loads so we calculate sum 1 and sum 2 by two iterations and we find the difference between these two we output these two sums the difference is written as ABS in bracket sum 1 minus sum 2 ABS is a built-in function we do not have to write ABS, ABS calculates absolute value this was the reason why you have that include CMath up there in the beginning of the program because this is part of the standard library that is provided by CC++ system what will absolute value do even if any one of them is higher the difference will always be positive so that is the absolute value now we make a curious observation here if diff percent 2 that means modulo 2 remainder of diff is not equal to 0 what does it mean the difference is odd the difference of sums is not even desired pair of packages does not exist why do we conclude this yes because if you exchange or if you swap loads and if you see the balanced equation you have sum minus x plus y is equal to sum 2 minus y plus x so you will actually have to divide sum 1 minus sum 2 by 2 and since x and y are integers you have to have the difference as even otherwise it will not be divisible by 2 and that is why this observation so that means we do not have to do anything further our search terminates here whenever you are searching for a value in general not only in the context of this problem whenever you are searching for a value and this is applicable more when you have very large arrays to search for and by large I mean let's say 1 million elements 10 million elements then the on the first opportunity where you can conclude that the desired element is not likely to be found in that array you would like to stop searching so this is an important lesson in any searching that we carry with us the rest of the program is actually simple but again outline of the basic algorithm observe that whatever I described to you and whatever was written on the first two slides suppose it had occurred to me while solving the problem in my mind and I could have just written the program you as a reader of the program may not easily understand what I have in mind this particularly true for more complex algorithms and that is the reason why never ever a program containing any new or different idea is written without appropriate comments explaining what the logic of your algorithm is never it's not done period so I would like to emphasize one thing writing programs is as much about writing correct CC plus plus syntax and semantics as it is about writing correct English Hindi Marathi Telugu whatever to explain what you have in your mind that is the objective of these comments a comment less program is of absolutely no value to the world it might work correctly it will work correctly and since you have written it you will possibly understand it but nobody else in the world will be able to modify that program will be able to extend that program and will even be able to use that program after some time if you are not around to explain what that program does so this is something I want to emphatically state and that's the reason why I have sort of given this exam so look at these comments this whole page is just comments now if you are going to spend 20 minutes solving a problem even in exam don't you think it is all right to spend 3 minutes in writing plain English here you don't have to worry about syntax don't worry about semicolons just slash star and star slash but you don't write it automatically now this is a habit which not only I but almost all professional programers believe that it is mandatory and therefore I am going to award some marks in the examination because you are sensitive to marks is what I have understood so if there are some marks you will possibly do something extra in your mind and in your work so remember this in general programs without explanatory commands is absolutely useless for human beings although it might be absolutely perfect program, correct program and in fact even maybe an elegant program but if that elegance is not understood by other human beings it is not useful so here we actually say whatever we have earlier discussed for example the resulting sums sum 1 plus y minus x must be equal to sum 2 minus y plus x after exchanging or 2 y is equal to this plus 2 x and therefore y is equal to sum 2 minus sum 1 by 2 plus x and in this we also outline our strategy for searching the value we say that in general for sum i if we are looking at i th element of t 1 then for sum j we must have the j th element of t 2 equal to i th element of t 1 minus sum 1 minus sum 2 by 2 exactly this one that is if i th element of t 1 is what I am looking at assuming it is x then I must find y which should be t 2 j if I locate one such element in t 2 I have solved the problem of balancing if I haven't I will take next value of i and start with the second element so once this much is clear the main program is actually very simple I am starting with i equal to 0 to m minus 1 this is presumably my x which is t 1 i find y which is a possible replacement of t 1 i since after knowing t 1 i and after knowing the sum someone and sum 2 I can actually calculate what y should be so y should be equal to someone minus sum 2 by 2 plus t 1 i now I will search for y in the arab so I have another iteration j equal to 0 to n minus 1 if t 2 j is y I increment a found count by 1 why am I doing that because I might find more than one pairs and I might as well give the option to the end user that look boss these two trucks can be balanced by either exchanging weight number this with weight number this or this weight in that and we also output t 1 i to be exchanged with t 2 i we complete this iteration for j if we don't find anything or even if we find anything we look at the next element of i t 1 2 or t 1 1 then t 1 2 etc and for each of these we try to do this search you will agree that at the end of this complete nested iteration I would have printed all possible pairs which can be exchanged however I do not know the program knows but I do not know whether anything was printed or not whether the program will print anything or not because that depends on the data so when I come out I once again check and how do I check that is the reason why I have maintained a count if the found count is still 0 that means none of these iterations have ever executed this output statement because none of these iterations could ever find a jth element of t 2 which was equal to y so I declare failure and come out I would expect your programs to be organized in this fashion whether it is exam whether it is assignment or whether you are writing programs for your own work for your own computational work later I went through a lot of submissions that you have made and not only this point stuck me in general your way of approaching exams is I could see that you are trying to complete some job in a given time that time seems to weigh very heavily on your mind when you are attempting question paper is that right it is bound to happen because there is a time constraint but you have to learn to do some basic minimal things and more importantly when you do your lab assignments and subsequently when you do your lab projects this kind of thing will simply not do I have an offer from a colleague of mine who works as the part-time program director for our Eklavya project Avinash Haute we take him take from the same institute many years ago he has laid groups of 1500 people in large software projects for TCS he is doing some social work when I called him and he retired from TCS and he is helping us so yesterday evening and night he was around and he was looking at it and he says Fadak your people will not be ever employed by TCS and if they are they will be thrown out within one day so I asked him why he says they are using variables I, J, K, L for indexes I said yes I also do then he said you will also not get a job in TCS there cannot be a variable which has only one character period there cannot be rule C++ will admit it but TCS programmer will not do it and in fact that is correct because when I teach software engineering I teach people to necessarily use larger names which are meaningful but for particular indexing you observe that if you will notice your I and J are indecipherable in print and possibly on your screen you agree sometimes there is a confusion with what is I what is J yesterday we had this problem he says you can use index 1, index 2 better you can use first index, second index in words so that there is never ever any confusion now this is something we cannot do during exam times this is something we cannot possibly do when we are constructing a program for assignment in 20 minutes but if we have laid the lab hand out earlier and if we have thought about what kind of program construction will we do then while writing at leisure at least we should follow these rules I have requested him sometime after the mid-sem to take a lecture on best software engineering practices for young programmers he has agreed to do that we will do that later but this is just an illustration and I would effectively state that in the mid-sem exam I would like your programs to be written at least on these lines not necessarily as elaborate as these but at least on these lines consider this I could have written all these statements starting at this point but the fact that these are indented to the right means this is an estate iteration inside it there is an F inside it there are statements if you don't follow this indentation you will not only lose marks for not indenting your program correctly but if my TAs cannot understand your logic you will have no right to claim that my program actually does this the meaning of the program should be self-evident as much as is possible and that should be remembered so here is the sample load data in two trucks can execute that program I'll put the .cpp file later on the module you can see that two loads can be exchanged here and the trucks will balance the problem is not about balancing loads in trucks the problem is about certain basic properties of elements in multiple arrays and these properties you will be looking for for different reasons not for just balancing loads so we go over to the discussion of another search problem searching for a value in an array here is some sample data roll and marks so roll number and marks are stored in two arrays this is just an illustration just some seven roll numbers and seven marks we have visited this problem earlier we have seen how roll numbers are arranged in an array corresponding elements of another array could store marks in fact we use this to illustrate the difference in execution times of programs which sort arrays of 25,000 students arrays for 50,000 students etc etc etc here the problem that we look for is different we are trying to see how if such an array is given and this array could contain let's say 20,000 students who have passed in some exam how do I find out whether my roll number exists inside that array or not so given this data and I have let us say a given roll number suppose this given roll number is 1005 of course the data is so small that you can scan it and you can say that if this is my roll number I have not passed in this exam on the other hand if the given roll number is 1008 then you will say yes located here and the student has got 53 marks the point is how did you do this search the ordinary search is done by sequentially scanning the array just as we scan the array sequentially for solving the truck balancing problem because there is no other clue if we had some specific arrangement of the elements of the array then perhaps we could have searched in a different way this is exactly what we wish to illustrate by first looking at the simple algorithm which will search the array in a very conventional fashion what is the convention take the given roll number go to the first element is this equal to 1008 no is this equal to 1008 so I will scan this is it 1008 no is it 1008 no so far yes there is 1008 the moment we find 1008 we locate the marks for that student announce the result if the number given was 1005 I keep searching 1001, 1002, 1003, 1004, 1006, 1008, 1009 please remember although the given numbers are written in ascending order there is no such assumption we are making so if the roll numbers are written in a jumbled fashion then even if this student does not exist in this list I will have to search the entire array before I conclude that the student has failed in fact that is the meaning of what I stated the other day when I was explaining the search problem that if roll numbers printed in a newspaper as results were to be listed in absolutely arbitrary fashion in order to determine whether I have passed or not I will have no choice but to read each and every roll number compare it with my roll number and if I have 20,000 roll numbers God bless me I will keep reading those 20,000 but that is not what I do in real life so we will illustrate what is the algorithm that we use for that this is a program which will do the mundane search makes no assumption on the arrangement of values inside the roll number array but given an array of 100 roll numbers and 100 marks I have a given roll number which represents whatever is to be searched if I find it I calculate I move the marks into something called found marks and I may want to also find the position of where that number is in the array I is of course a variable that I used to scan first I read all the data in arrays now I read the given roll number note that when I say read all data in arrays I have not written the code for this in the slides in the actual program there will be statements which will read that I read the given roll number and I search for it by setting up an iteration for I equal to 0 to n students minus 1 so I go over all the elements of the array if ith element of roll is equal to given roll I say found marks is equal to marks I and I will come out this program says marks for given roll are found marks appears to be a very straight forward program I search through this but there is a flaw in this program what is the flaw suppose my roll number which I have given as given roll number represents a failure I have failed the exam what will this program do it will it will take my roll number 1004 5 whatever it will go through this search will it ever match this condition no because my roll number is not there are fail it will come out marks for given roll number it's printing by marks you see are found marks suppose found marks at 91 value you see the point the output statement is executed independent of whether I ever found anything in the array or not what I must do if at all I want to run it like this before starting this execution of this iteration after reading given roll I say found marks equal to 0 so at least I will not go back home thinking that I have passed or scored 90 marks of course I should not have scored 0 that's a different score correctly what you should do is like a flag variable you should keep if the flag variable is 0 or if the flag variable is minus 9 then you give a message saying roll number not found in the array otherwise it should represent the correct you remember the sentinel technique that we had used for searching we could put some kind of a sentinel value in the found marks itself found marks equal to minus 999 and then if found marks equal to minus 999 see out sorry you have not passed the exam else print found marks so some such thing has to be done to carefully give the output anyway the point here is that I would have spent a complete scan of all the elements of the array considering that earlier algorithms that we had visited for sorting and all are order n square that means they take time proportion to n square if n is the size of the problem this appears to be a small expenditure in execution effort there are n elements and scanning all of those n elements if n is very large and if I have to search that array very very frequently then even this search counts let me give you an example set bank of India has now consolidated savings bank accounts stored in a single server each account has a account number many of you would have accounts in banks okay when you withdraw money from an ATM or from anywhere your account number is read inside the disk of the large server there is a file containing the account balances of the account numbers now if you are withdrawing withdrawing say 300 rupees the computer must first verify whether I have more than 300 rupees in my account or not so it will take my account number and search the array containing account numbers to first locate me if it does not find me it will announce that I am a fraud and probably ring a police if it finds me then it will locate the balance and then deduct 300 rupees from that balance write that balance back onto the files and then give me that how many accounts does a state bank of India computer contain about 20 crore now imagine every time I go to ATM and push my card it starts scanning 20 crore elements and after about 20 crore is a large number even if you have a very fast machine it will take non-trivial time and remember while I am trying to withdraw money somebody else in Dadar is trying to withdraw money somebody else in New Delhi is trying to withdraw money so the machine will start searching one say wait then another so probably the Coimbatore fellow will get the money next day it's not acceptable the example illustrates that even a seemingly simple problem of scanning in elements will become extremely complex and important if such searches have to be done very frequently and if the size of N itself is very large and this is an example you can think of any kind of example exactly the same thing will happen you do a railway reservation in the railway reservation you give a train number you want your machine has to search whether birds are available in that train or not again searches everywhere you go to life insurance corporation to take a loan on your policy you enter your policy number policy information has to be searched everywhere in the world such searches are common and therefore even the scan of N elements is often not acceptable in fact that is the fundamental reason why we do sorting and then searching we are leading to that and that is why these examples but in the better way of searching okay this of course discusses the problem that I mentioned so this you can sort out by correcting the program the point being made here is this algorithm scans all N elements once I have underlined it to say that it appears it's okay it is not okay in most situations we would like to look at something better to find out something better we go back to our mathematical computations and we look at the finding root problem of a given equation imagine that this is some function this function happens to have a root here we don't know that of course we are given a function definition one of the ways of finding this root we have studied the Newton-Raphson technique you remember that what we do is we start with some value and we approximate by a tangent and find out the intercept on x axis and keep moving towards the root this is a different method this is called bisection method and is usable if we can locate not one starting point but two starting points two starting points two values of x for one of which the function is negative and for another of which the function is positive these are represented here by L O and H I standing respectively for L O W or lower value and H I G H E R or higher value lower and higher are only with respect to the x axis this is on this side this is on this side what are we looking for again to consolidate the method that we are going to discuss the bisection method works provided we can locate two possible values of x such that the function of x as one value is negative as another value is positive because what does it signify it means that there must be a real root somewhere in between if it is a continuous function of course that is what we are talking about there must be one real root and now we are saying that if we are armed with these two values can we locate that root very fast look at the equivalent of the linear search of all elements that we did imagine this x axis has been broken into ten thousand points like ten thousand elements of an array I could in a very mundane way starting either from here or from here just look at the next value of x calculate function x is it zero is it zero is it zero is it zero is it zero ah here it is zero that would be linear search what this technique does is called a bisection method because what it does is it simply finds the arithmetic mean of low and high and this is the midpoint of these low and high values the function is evaluated at that midpoint at this point you will notice that the function happens to be positive the function on high was also positive this is also positive what is the conclusion the root lies in this region the root cannot be on this side because if there are multiple roots there could be but there exists at least one root on this side what have we done we had originally to search between low and high now we have to search only between low and mid if this value was negative we would have searched between mid and high effectively we have narrowed down the search space by half imagine out of twenty crore accounts in one shot you have said I need to search only for ten crore now this technique can be repeated if the elements of x axis are ordered which they are because x increases from this side to this side after finding out this midpoint I can set this midpoint to high I will assume as if this is high and again calculate the midpoint and again apply the same logic when I do that I would have further reduced that search space by another half from ten crores to five crores from five crores to two point five crores it appears very slow but it converges very rapidly because the scale is logarithmic you remember the story of the person who asked for grains on a squares of chess board one grain two grains four grains etc etc very rapidly the grains became enormous with the same rapidity the grains will reduce also so if you are searching in thousand elements first time you will reduce it to five hundred and twelve then you will reduce it to two fifty six then one twenty eight then sixty four etc in eight or ten searches so basically if you have n elements in log of n to the base two amount of searches you will be able to locate either locate the desired element or in case of areas declare that this fellow has failed you get the point this is the importance of the bisection search or midpoint search in any case since we are discussing this it is well worth visiting the root finding problem as well so I start with L O and H I such that function value at L O multiplied by function value of high is negative what it means is that the values have different signs it could well be that the function value at low is positive and function value of high is negative that is fine as long as they have opposed it said basically in case of mathematical function all that I am trying to say is that there does exist at least one root in this range that's all the way I find it is I compute the midpoint and I compute the function at that midpoint if the function at that midpoint is greater than zero as it is of course I will say some threshold value because I will never get exact zero in computational problems then I locate the next interval to be either low mid or mid high depending upon whether this value is positive or negative right so what I have to do I start with some low and some high calculate midpoint calculate the function value find out which sign it is and either I reassign a new value to H I or I reassign a new value to a low in this case I will say high is equal to mid now originally high was whatever once high is equal to mid I know back to square bar and that means I can set up an iteration an iteration which starts with some low and high value an iteration which reassigns either low or high to the midpoint and keeps coming back till what time till such point that I have found out the root consider x axis as discrete points and x axis is increasing zero one two three four five six seven eight nine ten etcetera etcetera I am trying to give a simile to the search by inverting this in this fashion so imagine this is the x axis just as values of x are increasing in this fashion this is low this is high similarly an array here the array elements which I am searching shown by this arrow are increasing in this order that is important if they are not sorted none of the logic will apply but if they are sorted here thousand one thousand two thousand three etcetera then I can indeed do exactly the same thing just like in the midpoint what I will do is this is the first element this is the last element that means zero and seven what is the midpoint of zero and seven well either three or four depending upon how you calculate the midpoint what I could do is I could try to see the given roll number whether it is equal to this element if it is equal I have found the element if it is not equal suppose I am searching for one thousand five I come here this is one thousand six well one thousand five is definitely not on this side because I started with one thousand eleven so I have reduced the search space by half I keep doing exactly the same thing and I will converge very quickly either in locating that roll number or saying that the roll number does not exist this then is the algorithm for binary search I have an array with n elements I start with low equal to zero and high equal to n minus one I calculate the midpoint low plus high by two just an integer division and I do not care whether the difference is even or all I will go some place I do not need to be exactly at midpoint roughly the search space will be divided into two halves I just prepare the roll number at that midpoint is greater than given roll number and high is greater than low then I have to recalculate mid if roll mid is greater than given roll roll is towards upper half of the array else low is equal to mid mid is equal to low plus high by two go back so this if will set either high to mid or low to mid in either case I will recalculate the new midpoint just as I did it outside the iteration because every iteration will yield a new midpoint I will keep doing this and how long I will do this till this condition as long as high remains greater than low because sometimes the indexes will get trapped high may become smaller than low or high may become equal to low when that happens either I have found the fellow or he is not there so I get out here and if the mid roll number at this point is equal to given roll I set found flag equal to one some notation which I can use to print the appropriate roll number I have written this slide you might want to jot it down and write down what happens to various scans that is first time when you do a scan what is the midpoint what is the low what is the high next what is recalculated as low mid etcetera you will find that it will converge in a logarithmic order this will not give you the exact nature because there are only eight elements here but you can try with 16 elements or some such thing better still we can write a program you can have a sizable value of n and you can actually output every time high or low is reassigned so that you know so many scans have been executed ok so this is what I wanted to discuss