Question:
explain me with this?
priya .
2007-01-07 01:04:34 UTC
// Program to depict the usage of Binary Search Technique.It is assumed that the list of numbers in an array is stored in ascending order.
#include
#include
main()
{
int num[10],m,min,max,mid,pp;
clrscr();
cout<<"Enter any 10 no. as elements of an array in ascending order "<for (int i=0;i<10;i++)
{ cin>>num[i];
}
cout<cin>>m;
cout<min = 0;
max = 9;
pp = -1;
while ((min<= max)&&(pp == -1)) 0
{
mid = (min+max)/2; 1
if (num[mid] == m) 2
pp = mid; 3
else 4
if (num[mid]min = mid + 1; 6
else 7
max = mid - 1; 8
}
if (pp>-1)
cout<<"The element "<else
cout<<"The element does not lie in the array";
cout<getch();
return 0;
}
plz give me the calculations of the lines marked
Four answers:
preeti
2007-01-07 01:58:34 UTC
0) 0<9

1) mid=0+9/2

mid=4

5) true

min=4+1;

0) 5<9

1) 5+9/2

mid=4;

5) true

min=5+1

min=6

0)6<9

1) 6+9/2

mid=7

5) true

and so on

hope that help...
2007-01-07 09:16:29 UTC
Are you asking what your lines 1-8 are doing? If so then...



In Binary Search or in what is arguably the most efficient way to search it is all about a literal divide and conquer...if the list is already sorted then this is straight forward....



In line 1 we find our mid point index (not a value but the mid point area of where that value lies)...



In line 2 we check to see if that mid point value is equal to what we are searching for...



In line 3...if we have a match then we are done...we found the location of our match...binary search stops here.



In line 4 we find that the mid point value is not equal so now we have to decide



In line 5 our first check is to find if our search value is GREATER than what is at the mid point area value



In line 6 if it was the case that line 5 was true then clearly all the values in our search list array less than 'mid' is useless to look at (divide and conquer) and so we ignore them now and we make our next set of ranges for searching from mid+1 (the index just 1 above).



In line 8 we alternatively find that m is LESS than the mid value and so we must set our max search range to equal 1 less than mid...What this is saying is all those values in the array list we are searching that are greater are useless to look at....Recall that the array list is already sorted so this is correct assumption..
red scar
2007-01-07 10:30:08 UTC
0.) It tries to check whether min (value: 0) is less than or equal to max (value:9). It checks if pp (value: -1) is really equal to -1. The purpose for this is to check whether the condition is met and the program must execute it. While loop is a loop so terminating conditions were given.



1.)Remember that a variable of type integer cannot possess a value which is a floating point. (mid = 0 + 9) / 2, that is mid = 9 / 2, therefore mid = 4 and NOT 4.5. Purpose for this statement is to find the MIDDLE NUMBER and NOT THE MIDDLE ELEMENT VALUE.



2.)Use the MIDDLE NUMBER (mid) as address to find the middle element in array num. Then compare it with the item that you want to find. e.g. num[4] == m.



3.)If number 2 is True, then assign mid to pp.



5.)If number 2 is False, then Compare middle element if it is less than the item that you want to find.



6.)If number 5 is True, then Add 1 to middle number and assign it to min. min = 4 + 1, therefore min is now 5. Remember that item that you want to find is BIGGER than the middle element in the array num, therefore min value must increase its value. To make it clearer, find that value within the range of 5(min) to 9(max).



8.)If number 5 is False, Then Subtract 1 to middle number and assign it to max. max = 4 - 1, therefore max is now 3. Remember that item that you want to find is SMALLER than the middle element in the array num, therefore max value must decrease its value. To make it clearer, find that value within the range of 0(min) to 3(max).



Hope this helps!
RMS4EVER
2007-01-07 09:12:54 UTC
Not sure what you are asking here, but this looks like code used to teach the idea of arrays in C++. Since there is poor documentation, and the variables listed are done so poorly, I do not know 100% what the use for everything is. This looks more like teaching code than something someone would actually write in a useful program.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...