Information Technology Reference
In-Depth Information
23. Sorting bubbles
We won't actually be sorting bubbles here, but instead use what is referred to as a
bubble sort. One common procedure in computer systems is the sorting of files of data.
We might need a report sorted by account number using a particular file and later desire
another report from the same file but sorted by a different field, such as last name. Many
systems will use the same file but sort it in a different order and produce different reports
for different people in an organization. There are also other times when it is advantageous
to sort a file before doing any processing.
Fortunately it is not difficult to sort a file of data. In some systems you can merely
go into edit mode in the file and enter a simple command and your file will be in a
different order. Another possibility is to have some kind of job control language that will
result in the file being sorted. We'll get into those methods later. For now suppose we had
a file that was in account number order, but needed to be sorted by zip code. Assume also
that we didn't have any way of sorting the file but had to rely on some technique that we
ourselves developed in a computer program. Simply put, we had to sort the file ourselves.
The way we could do this would be by reading the entire file into a table of data
and then use some logic to sort the table. Once done, we would write out the table to a
new file, which would be in the sorted order we desired, in this case by zip code. Our
report could then come from that new file. Another option would be to take the sorted
table of data and process it as the file we really wanted in our desired sort order to
produce the report.
Here we'll read the account number file and move the records to a table, sort the
table and then create the sorted file from the sorted table. I'll leave it up to you to produce
the needed report. Hint: just run the new file through the program
acctlist .
For simplicity sake we shall assume that our file has no more than 100 records,
although we don't know the exact count. The process to do all this - minus the processing
for the report - is accomplished by the following statements:
program-name : bubblesort
define work-array character (10000) element c-element character (100) occurs 100 times
define i-sub integer( 3)
define t-sub integer( 3)
define process-sw integer
define record-count integer( 3)
define file acctfile record account-record status acct-status structure
field character (100)
define file new file record new-record status new-status structure
field character (100)
define error-msg character (60) value spaces
define hold-line character (100)
process-sw = 0
i-sub = 0
Search WWH ::




Custom Search