Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups Profile Log in to check your private messages Log in
 

 

please help me regarding binary search

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
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    Post subject: please help me regarding binary search
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    Post subject:
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    Post subject: Reply to: hi anshul please find the code below
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: 7913
Location: Bellevue, IA

PostPosted: Tue Aug 17, 2010 3:59 pm    Post subject:
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    Post subject:
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    Post subject: Re: please help me regarding binary search
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    Post subject: Reply to: please help me regarding binary search
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    Post subject:
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: 2422
Location: Netherlands, Amstelveen

PostPosted: Tue Aug 17, 2010 5:36 pm    Post subject:
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    Post subject:
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: 1278
Location: Belgium

PostPosted: Tue Aug 17, 2010 8:04 pm    Post subject:
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: 2502
Location: Atlanta, Georgia, USA

PostPosted: Tue Aug 17, 2010 8:14 pm    Post subject: Reply to: please help me regarding binary search
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: 7913
Location: Bellevue, IA

PostPosted: Tue Aug 17, 2010 10:02 pm    Post subject:
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    Post subject:
Reply with quote

here is a link to some other studies,
http://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    Post subject:
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    Post subject: hi dbzTHEdinosauer thanq for ur valuable suggestions
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    IBMMAINFRAMES.com Support Forums -> COBOL Programming All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts Casting a Binary Number to Numeric in... Bob Steinkraus Java & MQSeries 6 Tue Aug 09, 2016 5:58 pm
No new posts Search a Load Module sreekanth1984 TSO/ISPF 8 Tue Jun 07, 2016 8:45 pm
No new posts MOVING COMP-3 into binary akodakka COBOL Programming 2 Tue Dec 01, 2015 5:02 pm
No new posts Copy one PDS to another PDS based on ... alluri12 JCL & VSAM 10 Tue Sep 29, 2015 8:10 pm
No new posts USS Shell script:Convert hex to binar... RSR2015 All Other Mainframe Topics 0 Fri Sep 18, 2015 2:00 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us