IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

please help me regarding binary search


IBM Mainframe Forums -> COBOL Programming
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
chidwilas

New User


Joined: 04 Aug 2010
Posts: 4
Location: hyderabad

PostPosted: Tue Aug 17, 2010 3:02 pm
Reply with quote

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
View user's profile Send private message
anshul_gugnani

New User


Joined: 02 Nov 2009
Posts: 73
Location: Mumbai

PostPosted: Tue Aug 17, 2010 3:36 pm
Reply with quote

Hi,

Please post the code you used and result you got.
Back to top
View user's profile Send private message
chidwilas

New User


Joined: 04 Aug 2010
Posts: 4
Location: hyderabad

PostPosted: Tue Aug 17, 2010 3:53 pm
Reply with quote

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
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8700
Location: Dubuque, Iowa, USA

PostPosted: Tue Aug 17, 2010 3:59 pm
Reply with quote

RTFM on SEARCH ALL
Back to top
View user's profile Send private message
Kjeld

Active User


Joined: 15 Dec 2009
Posts: 365
Location: Denmark

PostPosted: Tue Aug 17, 2010 4:35 pm
Reply with quote

Use the SEARCH verb to search arrays, instead of wasting time coding the search yourself.
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Tue Aug 17, 2010 4:48 pm
Reply with quote

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
View user's profile Send private message
chidwilas

New User


Joined: 04 Aug 2010
Posts: 4
Location: hyderabad

PostPosted: Tue Aug 17, 2010 5:24 pm
Reply with quote

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
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Tue Aug 17, 2010 5:32 pm
Reply with quote

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
View user's profile Send private message
PeterHolland

Global Moderator


Joined: 27 Oct 2009
Posts: 2481
Location: Netherlands, Amstelveen

PostPosted: Tue Aug 17, 2010 5:36 pm
Reply with quote

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. icon_eek.gif
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Tue Aug 17, 2010 5:48 pm
Reply with quote

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
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Tue Aug 17, 2010 8:04 pm
Reply with quote

stupid questions may not exists; but there surely are enough of inquisitive idiots.
Back to top
View user's profile Send private message
Bill O'Boyle

CICS Moderator


Joined: 14 Jan 2008
Posts: 2501
Location: Atlanta, Georgia, USA

PostPosted: Tue Aug 17, 2010 8:14 pm
Reply with quote

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
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8700
Location: Dubuque, Iowa, USA

PostPosted: Tue Aug 17, 2010 10:02 pm
Reply with quote

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
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Tue Aug 17, 2010 10:44 pm
Reply with quote

here is a link to some other studies,
www-949.ibm.com/software/rational/cafe/thread/3664?tstart=30

which suggests 240 some.
Back to top
View user's profile Send private message
Ronald Burr

Active User


Joined: 22 Oct 2009
Posts: 293
Location: U.S.A.

PostPosted: Wed Aug 18, 2010 12:01 am
Reply with quote

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
View user's profile Send private message
chidwilas

New User


Joined: 04 Aug 2010
Posts: 4
Location: hyderabad

PostPosted: Wed Aug 18, 2010 10:12 am
Reply with quote

hi dbzTHEdinosauer thanq for ur valuable suggestions finally i got the required output
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> COBOL Programming

 


Similar Topics
Topic Forum Replies
No new posts Format Binary file to EBCDIC JCL & VSAM 4
No new posts Mainframe Dataset Binary or ASCII Mainframe Interview Questions 4
No new posts Binary File format getting change whi... All Other Mainframe Topics 7
No new posts Search two or more word with FILEAID Compuware & Other Tools 17
No new posts Sortjoin and Search for a String and ... DFSORT/ICETOOL 1
Search our Forums:

Back to Top