Return to Volume I Explanations

 Problem 110 - Meta-Loopless Sorts, Explanations 

Algorithm

This problem is very confusing because you are asked to create code that creates code. To see what's really asked, just remove all the code and let only the permutations. Ah! it looks more easy, isn't it? The point is to generate permutations with only one check between each var.

Simply create a recursive function : put a, then put b before and after a, then for both cases, put c before, in between and after the two letters, and so on. When you've eventually put all the variables, you have generated a permutation. Simply add the pascal code in between and you are done!

Trick

Beware of the name of the variables and the number of ';' allowed.

If you are in trouble with the multi-entry input, read my how to read input.

Additional Input

110.in

Additional Output

110.out

Background

Sorting holds an important place in computer science. Analyzing and implementing various sorting algorithms forms an important part of the education of most computer scientists, and sorting accounts for a significant percentage of the world's computational resources. Sorting algorithms range from the bewilderingly popular Bubble sort, to Quicksort, to parallel sorting algorithms and sorting networks. In this problem you will be writing a program that creates a sorting program (a meta-sorter).

The Problem 

The problem is to create several programs whose output is a standard Pascal program that sorts n numbers where n is the only input to the program you will write. The Pascal programs generated by your program must have the following properties:

For those unfamiliar with Pascal syntax, the example at the end of this problem completely defines the small subset of Pascal needed.

The Input 

The input consist on a number in the first line indicating the number M of programs to make, followed by a blank line. Then there are M test cases, each one consisting on a single integer n on a line by itself with 1 $ \leq$ n $ \leq$ 8. There will be a blank line between test cases.

The Output 

The output is M compilable standard Pascal programs meeting the criteria specified above. Print a blank line between two consecutive programs.

Sample Input 

1

3

Sample Output 

program sort(input,output);
var
a,b,c : integer;
begin
  readln(a,b,c);
  if a < b then
    if b < c then
      writeln(a,b,c)
    else if a < c then
      writeln(a,c,b)
    else
      writeln(c,a,b)
  else
    if a < c then
      writeln(b,a,c)
    else if b < c then
      writeln(b,c,a)
    else
      writeln(c,b,a)
end.



Miguel Revilla 2001-05-25

Return to Volume I Explanations