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
 

 

COBOL Binary Search - basic question

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> COBOL Programming
View previous topic :: :: View next topic  
Author Message
vijikesavan

Active User


Joined: 04 Oct 2006
Posts: 118
Location: NJ, USA

PostPosted: Fri Aug 08, 2008 12:34 am    Post subject: COBOL Binary Search - basic question
Reply with quote

Hi Cobol experts,
My question is very basic question in binary search.

I have a table
Code:
05 WS_TABLE occurs 0 to 100 depending on WS-MAX
ASCENDING KEY  WS-KEY
INDEXED BY WS-INDEX
VALUE HIGH VALUES.
10 WS-KEY   X(02).
10 WS-DATA X(10)

My table is a variable length table. (Though the table is defined for max 100 I might have values only 20-25). Lets assume I have only 25 values.WS-MAX has 25.
While using SEARCH ALL, when does AT END condition happens, when it encounters 26th record or it goes till 100th record?

I am sorry for wasting your time on such silly questions, but I need this to be clarified.
Thanks
Back to top
View user's profile Send private message

Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Fri Aug 08, 2008 12:41 am    Post subject:
Reply with quote

AT END occurs when when SEARCH ALL does not find the value it is searching for. I would suggest you read the cobol manual.
Back to top
View user's profile Send private message
vijikesavan

Active User


Joined: 04 Oct 2006
Posts: 118
Location: NJ, USA

PostPosted: Fri Aug 08, 2008 12:43 am    Post subject: Reply to: COBOL Binary Search - basic question
Reply with quote

Thanks. I will read the manual for sure. Sometimes, you work on so many new features and forget the basics.
Thanks again.
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: Fri Aug 08, 2008 1:14 am    Post subject: Re: COBOL Binary Search - basic question
Reply with quote

IMHO, due to an absolute max of 100 entries, you'd be better off using either the SEARCH Verb or write your own In-Line PERFORM.

Generally, if the number of entries does not exceed 128, I use a sequential search.

Remember, to invoke a COBOL Binary Search, a CALL must be issued to a Run Time routine and therefore, you need to weigh this overhead against a sequential search, which would must likely (SEARCH verb) be resolved in-line (an In-Line PERFORM would undoubtedly be resolved in-line).

Just my .02 cents.... icon_wink.gif

Bill
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Fri Aug 08, 2008 1:31 am    Post subject:
Reply with quote

Hello,

Quote:
While using SEARCH ALL, when does AT END condition happens, when it encounters 26th record or it goes till 100th record?
Using your example, there are only 25 entries in the array. Items 26 - 100 would not "exist" and would not be "searched".

A couple of thoughts about performance improvements looking for a match in an array.

If the current value is the same as the value last searched, there should be no need to search. The required entry is already found (or not if it does not exist). Either way, no need to search again for the same "key".

When doing a sequential search, continuing from the current position is often very much faster than starting from the beginning when the new "key" is greater than the previous search key.

There is also a way to improve the search if the current key is less than the previous search key. Depending on how far the previous key is into the array, the search might be done by decrementing from the current positon or by starting over at the beginning and incrementing.
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Fri Aug 08, 2008 2:04 am    Post subject:
Reply with quote

Bill and Dick have made very good points. One thing to remember about one of Dick's comments "If the current value is the same as the value last searched, there should be no need to search. The required entry is already found." is that the required entry might have been a "not found" condition. I made that mistake once. When comparing the current search argument with the previous search argument, also keep track of the fact that the previous search might have resulted in the "at end" condition.
Back to top
View user's profile Send private message
dick scherrer

Site Director


Joined: 23 Nov 2006
Posts: 19270
Location: Inside the Matrix

PostPosted: Fri Aug 08, 2008 2:19 am    Post subject: Reply to: COBOL Binary Search - basic question
Reply with quote

Hello,

My bad icon_confused.gif

I should have specifically mentioned that. Hopefully, the code already "marks" a not found and that would still be available as well.
(Earlier post enhanced)

Good catch, Terry and thanx for the follow-up icon_smile.gif

d
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8057
Location: East Dubuque, Illinois, USA

PostPosted: Fri Aug 08, 2008 7:42 am    Post subject:
Reply with quote

Quote:
While using SEARCH ALL, when does AT END condition happens, when it encounters 26th record or it goes till 100th record?
To me, this says vijikesavan does not understand binary search. With 100 elements in the table, binary search will not do more than 7 iterations, comparing no more than 9 or maybe 10 items. Talking about encountering a particular record, especially the records at the end of the table, just doesn't make sense.
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Fri Aug 08, 2008 8:48 am    Post subject:
Reply with quote

Yes, as Dick alluded to, the 1st element to be compared with the search argument will be the element midway between the beginning of the table and the end (occurrence 13? of a 25 element table). The 26th occurrence will never be compared to the search argument. The Language Reference and Programming Guide tell you everything you need to know and have examples, but they don't explain the mechanics of a binary search. The way a binary search works might be found by Google.
Back to top
View user's profile Send private message
vijikesavan

Active User


Joined: 04 Oct 2006
Posts: 118
Location: NJ, USA

PostPosted: Mon Aug 25, 2008 10:56 pm    Post subject: Reply to: COBOL Binary Search - basic question
Reply with quote

Thanks everyone for all your valuable replies. Yes. I know binary search would not go to 26th search at all. Just to be sure..Its just one of my bad brain functioning days!.once again thanks everyone.
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
This topic is locked: you cannot edit posts or make replies. Cobol GUI (like Editor) Kala Mainframe COBOL Programming 14 Wed May 10, 2017 12:30 pm
No new posts IMS T-Pipe queue counts in a COBOL Pr... Siva NKK Kothamasu IMS DB/DC 0 Tue May 09, 2017 6:31 pm
No new posts COBOL Code Parsers and Lineage Establ... balimanja COBOL Programming 2 Tue May 02, 2017 3:30 am
No new posts Cobol upgrade - source code missing f... gthmrj IBM Tools 1 Wed Apr 26, 2017 6:04 pm
No new posts QUESTION: RETURN CHANNEL pahiker CA Products 21 Thu Apr 20, 2017 12:31 am


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