Sorting algorithms are essentially necessary in computer science. If there are n numbers then the best time to sort the numbers is O(n log n). Here you are asked to write a code that will sort the numbers in ascending order using only if
and else
statements.
Constraints
- Your code should contain a function named
print()
which prints the variables. There should be n parameters forprint()
and they area, b, c, ...
(ascending order). There should be aprintf
(as in C) function inside and should print all of them in ascending order. First three lines should describe theprint()
function. You can use spaces freely anywhere but the format insideprintf
should be correct. See the samples. - Your code should contain a function named
sort()
. The parameters should be same as theprint()
function. It should contain three kinds of statements:- if (x < y)
- else
- print(a, b, ...);
- Every statement should be in a single line. And no line should be empty.
- Nested if statements are allowed. And every if statement should have a corresponding else statement.
- Assume that the judge will test your code with distinct integers. That means for any two given inputs x, y you can assume that x ≠ y.
- For a code for n variables there should be exactly n! * 3 +3 lines (excluding the case).
- Check the samples very carefully for formatting.
Input
Input starts with an integer T (≤ 7), denoting the number of test cases.
Each case contains an integer n (1 ≤ n ≤ 7) denoting the numbers that will be given as input to your code.
Output
For each case, print the case number in a line. Then you have to print your code. Check the samples for the formatting details. There can be multiple solutions, print any valid solution.
Sample
Sample Input | Sample Output |
---|---|
3 1 2 3 | Case 1: void print( int a ) { printf( "%d\n", a ); } void sort( int a ) { print( a ); } Case 2: void print(int a,int b){ printf( "%d %d\n",a,b); } void sort( int a, int b){ if( b < a ) print( b, a ); else print( a, b ); } Case 3: void print(int a, int b, int c) { printf("%d %d %d\n", a, b, c); } void sort( int a, int b, int c ) { if (a < b) if (a < c) if (b < c) print(a,b,c); else print(a,c,b); else print(c, a, b); else if (b < c) if (a < c) print(b, a, c); else print(b, c, a); else print(c, b, a); } |