View previous topic :: View next topic |
Author |
Message |
tcurrier Intermediate
Joined: 10 Feb 2006 Posts: 188 Topics: 68
|
Posted: Wed Aug 13, 2008 6:44 pm Post subject: COBOL binary search 'problem' ?? |
|
|
Hello,
I wouldn't normally do a binary search on a table with less than 5 rows of data, but by accident, I found out that the binary search doesn't work if this is the case.
I also have a problem when the table has more than 10 occurrences 'defined'.
I've tried this with both the VS COBOL II and Enterprise COBOL compilers, same results.
Does anyone know what could be going on with this ? |
|
Back to top |
|
 |
Terry_Heinze Supermod
Joined: 31 May 2004 Posts: 391 Topics: 4 Location: Richfield, MN, USA
|
Posted: Thu Aug 14, 2008 12:55 am Post subject: |
|
|
I'm afraid you're going to have to prove to me that binary search does not work for fewer than 5 elements or more than 10. Please show the table and your code and do not key it in; use copy and paste to prevent typos. _________________ ....Terry |
|
Back to top |
|
 |
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Thu Aug 14, 2008 5:19 am Post subject: |
|
|
Quote: |
Does anyone know what could be going on with this ?
|
yes, your code is wrong,
or you do not have your table sorted,
or the number of occurances for an ODO is wrong,
or you have a fixed table and have not initialized your unused items to high-values. _________________ Dick Brenholtz
American living in Varel, Germany |
|
Back to top |
|
 |
tcurrier Intermediate
Joined: 10 Feb 2006 Posts: 188 Topics: 68
|
Posted: Thu Aug 14, 2008 6:37 am Post subject: |
|
|
OK, here's a quote from one of my co-workers:
"I am not sure.. but I think (I might have read it some where, not necessarily for COBOL) for a binary search to work well, the table should be filled at least 50%."
It looks like he's right. I changed my program to 20 occurrences of the table and 'filled' 10 of them.... (Table was originally initialized to spaces). The binary search worked. I then ran it with 9 tables entries filled instead of 10. It didn't work. |
|
Back to top |
|
 |
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Thu Aug 14, 2008 7:12 am Post subject: |
|
|
Blind leading those who can't see.
If you want anything except harassment, you need to supply us with some data layout and the code and we will tell you why your results and opinion are wrong.
Binary seaches become more effecient that scans when the number of items exceeds 50 (somewhere in that area)
the efficency of a binary search has nothing todo with the percentage of items filled / declared. -
For a binary search to work:
an item must be higher on the coallating sequence than the preceding item.
Cobol is not the only language that has a binary search. Binary search is an algorithm, suggest you spend your time reading about facts instead of theory.
Once you understand how a binary search functions,
you may wish that you had not created this thread. _________________ Dick Brenholtz
American living in Varel, Germany |
|
Back to top |
|
 |
tcurrier Intermediate
Joined: 10 Feb 2006 Posts: 188 Topics: 68
|
Posted: Thu Aug 14, 2008 8:59 am Post subject: |
|
|
Thanks for your input, but I have a roll program that I need to complete by tomorrow and I don't have time to get into a lot of research and long discussions about how binary searches work.
I failed to mention before that the data that the table is being filled from is coming from an external file, so there will not always be the same number of entries in the table. Therefore, I got around the 'problem' by
using the 'occurs depending on' clause in the table definition. INPUT-SUB
is equal to the number of records read from the input file and loaded into the table.
Code: | 01 THE-TABLE.
05 THE-TABLE-ENTRY OCCURS 1 TO 5000 TIMES
DEPENDING ON INPUT-SUB
ASCENDING KEY IS THE-POLICY
INDEXED BY THE-PTR.
10 THE-POLICY PIC X(10).
10 FILLER PIC X(01).
10 THE-INPUT-CLAIM-NUMBER PIC X(09).
10 FILLER PIC X(01).
10 THE-INPUT-CLAIM-SYMBOL PIC X(03).
10 FILLER PIC X(01).
10 THE-INPUT-AMOUNT-PAID PIC 9(06).
10 FILLER PIC X(01).
10 THE-INPUT-ID-CODE PIC X(02).
10 FILLER PIC X(01).
10 THE-INPUT-DATE-PAID PIC X(08).
10 FILLER PIC X(37). |
Without using 'occurs depending on', right now, all I can say is that in all of the test runs I've done, if half of the table entries aren't filled, the search comes up empty.
I'll look into the logistics later after my roll program is done, tested, and installed. |
|
Back to top |
|
 |
jsharon1248 Intermediate
Joined: 08 Aug 2007 Posts: 291 Topics: 2 Location: Chicago
|
Posted: Thu Aug 14, 2008 9:05 am Post subject: |
|
|
I suspect you defined the table with a fixed number of elements, that is, a table without an ODO clause. I also suspect that you're initializing the keys for all the 'unused' high order elements to high-values. If that's the case, the binary search might work in some cases, but not others. The problem is that you might not want the 'unused' elements to participate in the search, but COBOL doesn't have any way of knowing they're 'unused'. Setting the key to high-values might give you the impression that these elements won't participate in the search, but that is not valid solution for a binary search. Note the word unique in the Binary Search notes from the manual. You're better off specifying an ODO clause when utilizing a binary search.
Code: | The results of a SEARCH ALL operation are predictable only when:
The contents of the ASCENDING/DESCENDING keys specified in the WHEN clause provide a unique table reference. |
|
|
Back to top |
|
 |
tcurrier Intermediate
Joined: 10 Feb 2006 Posts: 188 Topics: 68
|
Posted: Thu Aug 14, 2008 9:24 am Post subject: |
|
|
Here is a simplified example:
Code: | 01 the-table.
05 the-table-entry occurs 10 times
ascending key is the-name
indexed by the-ptr.
10 the-name pic x(06).
move spaces to the-table.
move 'Bill ' to the-name (1).
move 'Fred ' to the-name (2).
move 'George' to the-name (3).
move 'Tom ' to the-name (4).
search all the-table-entry
at end
display 'entry not found'
when the-name (the-ptr) = 'Tom '
display 'found Tom'
end-search. |
When I fill 4 out of the 10 entries, it doesn't find 'Tom'.
If I add :
move 'Victor' to the-name (5)
it finds 'Tom' |
|
Back to top |
|
 |
tcurrier Intermediate
Joined: 10 Feb 2006 Posts: 188 Topics: 68
|
Posted: Thu Aug 14, 2008 9:27 am Post subject: |
|
|
But yes, to answer your ODO question... as I mentioned in a previous post, the ODO clause takes care of the 'problem' .... |
|
Back to top |
|
 |
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Thu Aug 14, 2008 9:41 am Post subject: |
|
|
a binary search starts at item number (total items declared / 2).
compares. if the search key is less than, then the next item checked is item number ((total items declared - (total items declared /2) ) / 2.
in other words, then the 2nd item to check is either half way back or half way to the end.
had you populated the-table with high-values instead of spaces, it would have found TOM with 4 entries.
Since you are using ODO (which you should, since you have an undertermined amount of items) you will be ok. _________________ Dick Brenholtz
American living in Varel, Germany |
|
Back to top |
|
 |
tcurrier Intermediate
Joined: 10 Feb 2006 Posts: 188 Topics: 68
|
Posted: Thu Aug 14, 2008 10:02 am Post subject: |
|
|
Humm, you're right.
Initializing the 'fixed' table to high-values does produce the correct result.
Thanks ! |
|
Back to top |
|
 |
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Thu Aug 14, 2008 10:40 am Post subject: |
|
|
you are welcome.
had i taken the time to explain in my first post what I wrote in the third,
probably would have saved us both time.
apologize for being abrupt (maybe even rude). _________________ Dick Brenholtz
American living in Varel, Germany |
|
Back to top |
|
 |
Terry_Heinze Supermod
Joined: 31 May 2004 Posts: 391 Topics: 4 Location: Richfield, MN, USA
|
Posted: Thu Aug 14, 2008 3:49 pm Post subject: |
|
|
tcurrier,
I don't think you would even have asked this question if you had read the COBOL Language Reference Manual concerning SEARCH ALL or the Programming Guide concerning Handling Tables. _________________ ....Terry |
|
Back to top |
|
 |
tcurrier Intermediate
Joined: 10 Feb 2006 Posts: 188 Topics: 68
|
Posted: Thu Aug 14, 2008 5:02 pm Post subject: |
|
|
You're right. My bad. |
|
Back to top |
|
 |
|
|