The C Corner # 2			     20/4/82
----------------------------------------------------


This month's column (sorry for the delay) is a hodge podge of
stuff that I have been working on or have seen. For the
advanced user, I offer a setjmp(3) and longjmp(3) package. For
the casual user I offer a simple but useful sort program.

Sorting occupies much of the time that computers have. At a
large commercial site up to 30% of the CPU may be taken up with
sorting, so the sorting must be efficient. There are many sorts
to chose from; slow but easy bubble sort, shell sort, merge
sort, and finally the more complex sorts like quicksort and
heapsort. The measure of an algorithm (program) is called it's
order. This gives an idea of the running time that you can
expect for that program. For expample the bubble sort is an
O(n*n) sort. O(n*n) is read as order n squared. This means that
the running time is proportional to the square of the number of
elements that you have to sort. A moments reflection will tell
you that this not good. How long would it take to sort 10
elements using the bubble sort? For arguments sake, lets say
that it takes 1 second to sort 1 item. (Unrealistic but easy to
work with...). We know that bubble sort is an O(n*n) algorithm.
That means that it will take 10*10 = 100 seconds to sort the
elements. By comparison, quicksort is an O(n log n) sort, and
it would take 10 * log (base 2) 10. One can easily see that
quicksort will outperform bubble sort. Quicksort becomes useful
when you have a fairly large list of items to sort (n > 10
say). Bubble sort is useful in applications where you must do
sorting and but not a lot of it. It's short and simple and can
be squeezed in anywhere. Quicksort, in most of it's forms, is
really gory plus it is difficult to understand. For those of
you who are brave, try quicsort with small values of n and
stand back... 

What does this have to do with C you might ask? Sorting is a
good example of where tool design becomes paramount. Both the
Software Tools books have an excellent sections on sorting.
When I analyze the problem of sorting, I find that I can break
it up into sections. One section is the comparison of two
elements. Another would be the effecient exchange of those two
elements. The section that binds those would be the body of the
sort. Using this module oriented design it becomes very easy to
change the design of the sort. With a change of the compare
function, you change the direction of the sort. It becomes
simple to sort in ascending or descending order. The data
exchange function can be tailored so that it will move whole
structures around which is sorted on one key say.

For some very lucid discussions on sorting try The Art of
Computing Programming, Vol. 3, Sorting and Search, D. A. Knuth.
Software Tools in Pascal and Software Tools in Ratfor,
Kernighan & Ritchie, give some excellent implementation notes.
The C Programming Manual, Thompson & Ritchie also has a section
on sorting.

The sorts that I present are from all those source. The
quicksort routines are suitable for inclusion into the BDS C
library functions. I must admit that I have not tested the code
under CP/M. The shell sort I use all the time. The input 
functions and shell sort are taken from Software Tools in
Ratfor. I use the shell sort for most simple sorts because it
is a good compromise between speed and complexity. It could be
expanded easily with features such as a faster sort, more
switches, ascending and descending options etc. I am working on
those and will release the sort when I am done.

For the more advanced user I give you setjmp and longjmp for
BDS C. I was trying to bring up ed from PWB. I was doing fine
until I ran into the setjmp problem. I could not port the
routines from PWB or V7 since they are machine dependent so I
wrote my own. I tested them out and they seem to work. Please
excuse the self-modifying code, I could not get round the
problem of running out of registers and suplying the return
code in HL. If anyone does, please send me the update via
USENET or the RCPM network. As to putting up ed, the BDS C seek
routines fell down in the crunch and I have not got round to
changing them. Does anyone have ed up on a micro?

Of additional interest to BDS C users, Leor Zoleman has
released more stuff into the public domain. He has come up with
a preprocessor, called CASM, to replace the MAC library that
comes with C. It makes life simpler and you don't need MAC to
write machine language functions. Of great interest is a new
linker called L2. L2 is written in C by Mark of the Unicorn who
have kindly placed it in the public domain.  They seem to be
putting out some excellent C stuff. I have seen a demo of an
editor by them called MINCE that was very impressive. L2 also
reduces the size of a COM file by up to 10%. It does this by
replacing the table of indirect function jumps with direct
jumps. It also has an excellent overlay manager in it. SID
works better as well since you don't have all the indirect
function calls to go through.

On the 16 bit side, there seem to be several 16 bit UNIX
systems on the market. Xenix (Microsoft) is not being sold
unless you are a hardware zendor. I have yet to read about a
system that uses Xenix in any of the trades. (If you have seen
some, I would be interested in what you have heard). Wicat,
Fortune and Dual all have UNIX (Bell v7/III, Berkley v7)
options for there systems. They are all based on the M68000.
Whitesmiths, makers of C compilers galore, has released Idris,
their v6 copy. You only need memory management and around 128k
to put it up. Make that 192k if you want to do C compiles as
well. Coronet by M. Williams Associates is being designed for
the Z8000. It should be realeased on an Ithica Intersystems
machine. (This maybe heresay...) They are also working on
version for the 68000. I also have heard rumours that UNIX is
being ported onto the IBM PC... It seems that you could have a
very useable UNIX system for $10k Canadian, less if you are
willing to forego a hard disk and the likes.

If you have any comments or questions you can reach me at the
Mississaga RCPM system or via USENET. My USENET address is
utzoo!utcsrgv!utcstvax!jacques. See you at the USENIX
conference in Boston.
