View previous topic :: View next topic
|
Author |
Message |
chidwilas
New User
Joined: 04 Aug 2010 Posts: 4 Location: hyderabad
|
|
|
|
i have 4 values " 6 8 25 30" in table i want to search for 00 value by using binary search in that table even 00 is not there it was showing 00 is found at 00 position so wat to do |
|
Back to top |
|
|
anshul_gugnani
New User
Joined: 02 Nov 2009 Posts: 73 Location: Mumbai
|
|
|
|
Hi,
Please post the code you used and result you got. |
|
Back to top |
|
|
chidwilas
New User
Joined: 04 Aug 2010 Posts: 4 Location: hyderabad
|
|
|
|
Code: |
PERFORM UNTIL NUMB-FOUND-Y OR NUMB-FOUND-N
COMPUTE MID = ( LOWER + UPPER ) / 2
EVALUATE TRUE
WHEN NUMBERS(MID) = SEARCHNUM SET NUMB-FOUND-Y TO TRUE
DISPLAY 'NUM WAS FOUND : ' NUMBERS(MID)
DISPLAY 'POS IS : ' MID
WHEN LOWER > UPPER SET NUMB-FOUND-N TO TRUE
DISPLAY 'NUM WAS NOT FOUND : ' SEARCHNUM
WHEN NUMBERS(MID) < SEARCHNUM COMPUTE LOWER = MID + 1
WHEN NUMBERS(MID) > SEARCHNUM COMPUTE UPPER = MID - 1
END-EVALUATE
END-PERFORM
STOP RUN. |
|
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
RTFM on SEARCH ALL |
|
Back to top |
|
|
Kjeld
Active User
Joined: 15 Dec 2009 Posts: 365 Location: Denmark
|
|
|
|
Use the SEARCH verb to search arrays, instead of wasting time coding the search yourself. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
chidwilas wrote: |
i have 4 values " 6 8 25 30" in table i want to search for 00 value by using binary search in that table even 00 is not there it was showing 00 is found at 00 position so wat to do |
1.. You are not performing a cobol binary search.
you have your own bug infested code which is returning an incorrect result.
I am not going to show you why you need to insert this line to save your garbage, just show you what you need to do:
the first WHEN clause of your evaluate should be:
WHEN MID < 1 SET NUMB-FOUND-N TO TRUE
in cobol you can have negative subscripts (and indexes) |
|
Back to top |
|
|
chidwilas
New User
Joined: 04 Aug 2010 Posts: 4 Location: hyderabad
|
|
|
|
i need to find out the position of value in that table but not the value, so i used the code
every thing is fine but when i gave 0 which is not there in table it was showing 0 position
please find output below
numbers : 06
numbers : 13
numbers : 25
numbers : 30
num was found and position is 0 |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
hey, I am not going to take the time to show you why you had a bug.
but, I already told you that subscripts can be negative
(0 would equate to item -1)
the working storage before the table, obviously had zeros.
you had a terminator in your evaluate to prevent going beyond the table,
the line of code that I gave you will prevent you going 'in front of the table'.
you are lucky. if you were looking for something that did not exist 'in front of your table'
you would have ended up with a SOC4 or 7.
i debugged your stuff with rexx. I just wrote a routine using stems and SELECT statement to mimic your code and determine what
you failed to take into consideration in your program logic
if you want more detailed info, send me a PM so that I can give you my PAYPAL account into which you can provide the appropriate fee. |
|
Back to top |
|
|
PeterHolland
Global Moderator
Joined: 27 Oct 2009 Posts: 2481 Location: Netherlands, Amstelveen
|
|
|
|
Quote: |
if you want more detailed info, send me a PM so that I can give you my PAYPAL account into which you can provide the appropriate fee.
|
Dick,
didnt know you are a money-grubber. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
PeterHolland
wrote: |
Dick,
didnt know you are a money-grubber.
|
more of a
'stop being so lazy and asking stupid questions; figure it out for yourself'
grumbler. |
|
Back to top |
|
|
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
stupid questions may not exists; but there surely are enough of inquisitive idiots. |
|
Back to top |
|
|
Bill O'Boyle
CICS Moderator
Joined: 14 Jan 2008 Posts: 2501 Location: Atlanta, Georgia, USA
|
|
|
|
If you use the COBOL BINARY SEARCH for four entries, then this is a good example of a bad idea.
The overhead incurred is a waste of resources.
Why not jut use a simple sequential SEARCH, because the compiler will probably resolve it in-line. Invoking a SEARCH ALL will cause the compiler to CALL a COBOL Run-Time routine.
Bill |
|
Back to top |
|
|
Robert Sample
Global Moderator
Joined: 06 Jun 2008 Posts: 8700 Location: Dubuque, Iowa, USA
|
|
|
|
I'm not sure if it is still true, but many years back (30 or so), I was working at a place where some extensive studies were done. For less than 50 entries in the table, COBOL sequential search was faster than binary search. |
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
Back to top |
|
|
Ronald Burr
Active User
Joined: 22 Oct 2009 Posts: 293 Location: U.S.A.
|
|
|
|
According to the IBM Enterprise COBOL Version 3 Release 1 Performance Tuning Manual:
* using a binary search (SEARCH ALL) to search a 100-element table was 6% faster than using a sequential search (SEARCH)
* using a binary search (SEARCH ALL) to search a 1000-element table was 83% faster than using a sequential search (SEARCH)
(assuming that both searches are searching tables with roughly equal distributions of argument values and that both use INDEXes rather than SUBSCRIPTS (another of the Performance Tuning tips)) |
|
Back to top |
|
|
chidwilas
New User
Joined: 04 Aug 2010 Posts: 4 Location: hyderabad
|
|
|
|
hi dbzTHEdinosauer thanq for ur valuable suggestions finally i got the required output |
|
Back to top |
|
|
|