BINARY CHOP

Faster than the eye can follow - this Spectrum routine will
speed your searching tremendously. It is yet another utility
from Diana Smith.


In the 9th April issue of HCW we gave you a very efficient routine for
sorting arrays called Quicksort. This could be used in your own database
programs to replace any slower sort method. Having sorted an array, you still
need to find a record in it for many applications. Here is the Binary Chop -
no relation to pork, nor a Karate blow but a very quick method of matching a
record in a sorted array.

Many published programs use a FOR...NEXT loop to check each record in a file
until a match is found. If the record you want is at the opposite end of the
file from which you start checking, you can have a long wait!

Binary chop works like this. Suppose you want to find a phone number for
Smith in a telephone directory. You open the directory halfway and might find
names beginning with H. You can immediately ignore the first half of the
directory. Look halfway through the remaining pages and you might find names
beginning with P. Smith is then in the last quarter of the directory. Look
halfway through this and you may find the Ws, so you then look halfway
between the Ps and the Ws.

By this time you have eliminated seven-eighths of the directory in only three
checks. Repeat this until you find the Smiths and you can then start checking
initials by the same method until you find the number or establish that it is
not in the directory.

This method of eliminating half the possible choices each time is very
efficient, especially for large numbers of records. The bracketed figures in
Table 1 demonstrate this.

The program listing allows you to compare the times of a sequential search
and a binary-chop search. It first sets up an ordered array, does a
sequential search and reports the time taken and the number of checks done.
It then repeats the search using the binary-chop routine.

Key in the program, press RUN and ENTER and answer the prompts. You may like
to try to reproduce the timings given in Table 1 which show how quick the
binary-chop can be, especially for large arrays. Indeed, for small arrays, it
is only the combination of the sequential search being earlier in the program
and using a FOR...NEXT loop that makes it faster than the binary-chop
subroutine with its slow GOTO statements.

The match string was chosen as the "middle-plus-one" record to represent an
average search without allowing binary-chop to find it on its first pass.

The binary-chop subroutine, starting at line 9000, can be easily modified for
use in your own programs. Delete lines 9004 and 9005 as they will be
unnecessary. Remember to set the variable "upperlimit" equal to the highest
record number before calling the routine. If the routine returns with
"matchfound" equal to 1, the match will be found at record "i" in the array.

For those speed-freaks who find this routine too slow in BASIC, I can supply
a machine-code version if you send 1 plus a large SAE to me at [address removed].


Table 1.
Comparison of sequential and binary-chop searches. Bracketed figures give the
number of checks made during the search.

                          Search time (seconds)
                        Sequential       Binary-
Records   Match string    Search          Chop
5            0004         0.14   (4)    0.16  (2)
50           0026         0.5   (26)    0.3   (5)
500          0251         4.26 (251)    0.42  (8)
5000         2501        41.3 (2501)    0.62 (12)


Hints for conversion
The binary-chop subroutine (lines 9000 on) should pose no problems.
The DEF FN in line 10 calculates the time in seconds from the computer's clock.
The POKE in line 1050 scrolls the print up the screen.


--
from Home Computing Weekly, 24th September 1985
