IN THE LAST few months I hope you have learned that programming is not an exact science but an art. There are no best ways to program — there are only guidelines. If you can think of a better way of doing something, or improving established methods, do not be worried by the so-called experts. Experts are not always correct.
There are many methods of searching data, just as last month we indicated that there is more than one way of sorting data items. No technique is better than another but in the proper circumstances one technique can be used in favor of another. The Binary Search with which I deal is one of the fastest searching routines and one of the easiest to learn.
Obviously, speed is important in any searching routine. Someone cannot wait for an hour for data contained in a list of 30 items to be retrieved by the computer. If that were the case it would be easier to search through a list on paper.
Figure 1
SEARCH KEYWORD: Sinclair
STAGE ONE:
——→ Memory aid
Read Only Memory
Sinclair Research
STAGE TWO:
Memory aid
——→ Read Only Memory
Sinclair Research
STAGE THREE:
Memory aid
Read Only Memory
——→ Sinclair Research
SEARCH COMPLETED
One method of searching a list would simulate a person doing it by hand very well. That is called the Serial Search — figures one and two — which can take up to half an hour to search a list of 100 names and addresses. The routines will run without the database but if you want to include one of them within the main program you use lines 2000 to 2040.
Figure 2
2000 SLOW
2002 REM SET UP DATA FILE
2004 DIM B$(10,12)
2006 REM DATA FILE ENTRY
2010 FOR K=1 TO 10
2020 INPUT B$(K)
2030 NEXT K
2032 PRINT "FILE ENTRY COMPLETE"
2034 PAUSE 100
2036 CLS
2038 REM SEARCH KEYWORD
2040 INPUT A$
2048 REM A = NUMEER OF FILE ELEMENT BEING MATCHED
2050 LET A=1
2058 FOR X=1 TO 10
2060 PRINT AT 10,10; "SEARCHING"
2062 NEXT X
2070 IF B$(A)(1 TO LEN A$) = A$ THEN GOTO 2110
2080 IF A=10 THEN GOTO 2098
2090 LET A=A+1
2092 GOTO 2070
2098 FOR X=1 TO 10
2100 PRINT AT 10,10; A$; " NOT FOUND"
2102 NEXT X
2104 STOP
2110 CLS
2112 PRINT AT 12,0; "ITEM LOCATED "; B$(A)
The computer scans the data list, item by item, trying to match the key, a name or telephone number entered by the user, with the items in internal memory. When a match is made the item will be displayed in full on the screen.
The length of time of the process will depend on the speed of the high-level language, such as BASIC, or the speed of the central processor if the program is written in machine code. The number of items in the list to be searched will also be a factor in the amount of time taken by the computer.
If you are planning to use only a few data items you could use the Serial Search technique. The coding is simple. Use a variable as a counter to point to each data item in the listing in turn. Increase the pointer by one every time an item is compared to the user’s entry and no match is made. When the match is made, print it to the screen.
The Binary Search is as easy to program as the Serial Search but the routine is much faster, as it does not have to search every data item on the list. The data must be sorted in alphabetical or numeric order and the computer will look at the element at the center of the list of data as the starting-point — figure three — cutting the list into two halves.
If the identification keyword or number, typed in by the user to trace an item in a file, and the element in the file do not match, the computer has to continue its search. The computer will find if the alphabetic character or number is higher or lower than the keyword typed in by the user. If it is lower the computer will take the last element examined and make that the end of the file, cutting away the other half.
If it is higher, the computer will take the last examined item as the beginning of the new, shorter file. The computer will then find how many elements are in the short file and divide it by two. The number found will be the element at the middle of the file and that is the element looked at next by the computer.
The process continues until a match is made. Then the computer will print out the full data item found in the file. The whole process is shown in figure three and a listing of the resulting program is shown in figure four. The listing can be used alone with a short data entry routine or with the database which has been discussed as an example throughout this series.
Figure 3
SEARCH KEYWORD: Sinclair
STAGE ONE:
Alphabet
Bubble
——→ Memory
Research
Sinclair
ZX-81
STAGE TWO:
Alphabet
Bubble
Memory
——→ Research
Sinclair
ZX-81
STAGE THREE:
Alphabet
Bubble
Memory
Research
——→ Sinclair
ZX-81
SEARCH COMPLETED
Line 2070 of figure four will check to see if the keyword entered at line 2040 is the same as the element pointed to by variable L in the data file. If it is less than the value of the file entry, the pointer, L, will move further up the list and if it is greater it will move down the list.
That part of the program is executed in lines 2060 to 2080. The number of the file element in L must be an integer, so INT is used in those lines. The 0.5 at the end of the lines mentioned must be added to the INTeger in L to round it up and not down, as the Timex Sinclair machines do automatically during mathematical operations.
Figure 4
2000 SLOW
2002 REM SET UP EXAMPLE DATA FILE
2004 DIM B$(10,12)
2006 REM DATA FILE ENTRY
2010 FOR K=1 TO 10
2020 INPUT B$(K)
2030 NEXT K
2031 PRINT "FILE ENTRY COMPLETE"
2032 PAUSE 100
2033 CLS
2034 REM A = LOWEST FILE NUMBER ELEMENT
2035 REM B = HIGHEST FILE NUMBER ELEMENT
2036 LET A=1
2037 LET B=10
2038 REM SEARCH KEYWORD
2040 INPUT A$
2048 REM BEGIN SEARCH IN THE MIDDLE OF FILE
2050 LET L= INT ((B-A)/2 + .5)
2052 GOTO 2060
2054 IF B$(L) (1 TO LEN A$) > A$ THEN
LET L = INT ((B-A)/2 + .5)
2056 IF B$(L) (1 TO LEN A$) < A$ THEN
LET L = INT ((B-A)/2 + .5) + A
2060 IF B$(L) (1 TO LEN A$) = A$ THEN
PRINT "ITEM LOCATED "; B$(L)
2062 IF B$(L) (1 TO LEN A$) = A$ THEN STOP
2070 IF B$(L) (1 TO LEN A$) > A$ THEN B=L
2080 IF B$(L) (1 TO LEN A$) < A$ THEN A=L
2090 GOTO 2054
In some cases it is necessary to display every item in a data file which has an entry which corresponds to the keyword input by the user. That can be done by adding an extra condition, IF … THEN, statement before line 40 so that if a match occurs the program will continue to search the file in case there are more corresponding items. If a long file of data is to be processed and displayed it is a good idea to introduce paging into the display routine.
The computer should display the data in pages, and at the bottom of each page a message to the user should be displayed asking whether he wants to see more. If the user types in ‘no’ the program control can be returned to the main menu.
The display of data is an important facet of any program, and the guidelines in the second article of this series apply to data output as well as data entry. When a menu is used in a program and is not displayed for some time while other operations are taking place, it is important to let the user know what other options are available at that time. That can best be done by displaying a band across the bottom of the screen with the options and the ‘return to main menu’ option on it.
The main point I want to stress is that not all users are programmers. That may be less so now than in the past with the advent of home computing but there are still some users who have difficulty even getting around the keyboard.
While it is important to know about techniques for file processing, such as those discussed in this and the previous article, it is more important to know about the user interface — how the user will interact with the computer and how he will cope with your program. Once you can see the likely reaction of the user and fit the program around your intended user, everything else will seem simple.
Next month, in the final article in this series, I will show how to develop techniques for ridding yourself of those little creatures which users do not see but which can make a hash of a program — the bugs.
Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.