Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

USING 'SEARCH ALL' FOR COBOLII BINARY SEARCH

Status
Not open for further replies.

docker

Programmer
Mar 1, 2001
4
CH
I am running a binary search, using a table sorted, and indexed without duplicates, however it never finds the requested row. When I changed to a sequential search the search is successful.

Does anyone have any suggestions on what I can do to fix this?
 
Hi,

perhaps it is not sorted right. Or perhaps you need some high-values for not filled elements at the end of the table.

You can also make your own binairy search by doing something like this:

009300 ZOEK-H-HEX-OMSCHRIJVING SECTION.
009400 ZHH-01.
009500 MOVE ZERO TO SUB-FIRST.
009600 MOVE +255 TO SUB-LAST.
009700 PERFORM UNTIL SUB-FIRST > SUB-LAST
009800 COMPUTE SUB = (SUB-FIRST + SUB-LAST) / 2
009900 IF H-HEX-TEKEN >
010000 HEX-OMSCHRIJVING (SUB)
010100 COMPUTE SUB-FIRST = SUB + 1
010200 ELSE
010300 IF H-HEX-TEKEN <
010400 HEX-OMSCHRIJVING (SUB)
010500 COMPUTE SUB-LAST = SUB - 1
010600 ELSE
010700 GO TO ZHH-99
010800 END-IF
010900 END-PERFORM.
011000 ZHH-03.
011100 MOVE +9999 TO RETURN-CODE.
011200 ZHH-99.
011300 EXIT.

The sub-first is the starting position in the table and sub-last is the last position. You will probably need +1 as the first value but I use sometimes zero.

With your debugger, you can use this code and see where it goes wrong.

Regards,

Crox

 
Hi Docker,

Why don't you show us the table definition and the paragraph that the search all appears in. We may be better able to spot the problem.

Thanx, Jack.
 
Thanks guys for looking at this. Turns out you were right Crox.

The table I was searching was set to spaces, and then then x number of elements were sorted and loaded into the table. Only problem was that meant the table had fields going from lowest to highest, followed by serveral space elements, hence the table was not fully sorted.

I changed the sort order around to descending and ran the program again and it worked ok. So the problem was caused by the sort order of the table being invalid.

Thanks for you help.

 
When a table may not contain the full number of elements allowed for, you might want to use the OCCURS DEPENDING ON CLAUSE, instead of a plain OCCURS clause. This will allow you to change the exact number of items at run time, according to however many you actually have loaded. This is more efficient than adding or filling in &quot;dummy&quot; values at the end, because the SEARCH statement won't need to SEARCH the unused table area at all.

I realize it's too late for this to help this time, it's just something to think about for the future. Betty Scherber
Brainbench MVP for COBOL II
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top