eta-Loopless Sorts
eta-Loopless Sorts |
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:- They must begin with program sort(input,output);
- They must declare storage for exactly n integer variables. The names of the variables must come from the first n letters of the alphabet (a,b,c,d,e,f).
- A single readln statement must read in values for all the integer variables.
- Other than writeln statements, the only statements in the program are if then else statements. The boolean conditional for each if statement must consist of one strict inequality (either < or >) of two integer variables. Exactly n! writeln statements must appear in the program.
- Exactly three semi-colons must appear in the programs
- after the program header: program sort(input,output);
- after the variable declaration: ...: integer;
- after the readln statement: readln(...);
- No redundant comparisons of integer variables should be made. For example, during program execution, once it is determined that a < b, variables a and b should not be compared again.
- Every writeln statement must appear on a line by itself.
- The programs must compile. Executing the program with input consisting of any arrangement of any ndistinct integer values should result in the input values being printed in sorted order.
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$](http://uva.onlinejudge.org/external/1/110img1.png)
![$ \leq$](http://uva.onlinejudge.org/external/1/110img1.png)
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.
#include <iostream>
#include <cstdio>
using namespace std;
char str[] = {'a','b','c','d','e','f','g','h'};
int arr[] = {0,1,2,3,4,5,6,7};
int n_;
void printStr(int n) //输出字母排列
{
for(int i = 0; i < n; i++){
cout << str[arr[i]];
if(i < n-1) cout <<',';
}
}
void sort(int n) //从第0个数字排到第n_-1个数字
{
if(n == n_){ //输出并返回
for(int i = 0; i < n;i++) cout << " ";
cout << "writeln(";
printStr(n);
cout <<")\n";
return;
}
int arr1[8];
for(int i = 0; i < 8; i++) arr1[i] = arr[i]; //复制保存当前状态
arr[n] = n;
if(n == 0) sort(n+1); //若当前是第0个数字,直接放入无需比较
for(int t = 0; t < n; t++) cout << " ";
for(int i = n-1; i >= 0; i--){ //当前第n个数字,共可能放在n个位置,依次和放在前面的数字比较大小,依次往前放
printf("if %c < %c then\n", str[arr[i]], str[arr[i+1]]); //因为是从小排到大,假如是大于那么第n个放入的数字就确定了,小于的话继续往前比较
sort(n+1);
for(int t = 0; t < n; t++) cout << " ";
if(i) printf("else ");
else printf("else\n");
swap(arr[i], arr[i+1]);
if(i == 0) //如果比到了第0个数字,则也不用继续比较了
sort(n+1);
}
for(int i = 0; i < 8; i++) arr[i] = arr1[i]; //恢复原状
}
int main()
{
int m;
cin >> m;
while(m--){
cin >> n_;
cout << "program sort(input,output);\nvar\n";
printStr(n_);
cout << " : integer;\nbegin\n readln(";
printStr(n_);
cout << ");\n";
sort(0);
cout << "end.\n";
if(m) cout << endl;
}
return 0;
}