Updated 2011-01-28 01:40:51 by RLE

1. Five-element sorting procedure

Write a procedure, sort5, that sorts a list. The list is presented as the only parameter to the procedure. The list will have exactly five elements, and each element will in turn be a list. The return value from sort5 should be a list with the sublists arranged in ascending order by their first elements, according to the rules for [string compare]. The sort must be stable; that is, if two lists with equal keys are present in the input, they must appear in the output in the same order as they appeared in the input.

In other words,
 [sort5 $x]

should return the same results as
 [lsort -ascii -index 0 $x]

However, you may not use any form of the [lsort] command in its implementation.

You need not check that the input is well formed. You may assume that it is a five-element list, and that each element is itself a well-formed list. (In other words, if you feel like tailoring a special sorting routine that's specifically targeted to five-element lists, go right ahead!) Submissions will be judged first on correctness, including stability, and second on speed.

Tcl 2002 programming contest: solutions to problem 1

The Great Canadian Tcl/Tk Programming Contest, eh?