Server Time: Sun Feb 23, 2020 8:42 am
Welcome ( logout
1091 - The Fastest Sorting Ever!
  PDF (English) Statistics Forum
Time Limit: 2 second(s) Memory Limit: 32 MB

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.

In your code, you should attach the function to print (as in sample) the numbers. In your codes you can use spaces freely, but be careful to use spaces in the printing format (as in the sample printf). It should be exact. And no lines should be empty.


1.      Your code should contain a function named print() which prints the variables. You can use spaces freely but the format in printf should be correct. There should be n parameters for print() and they are a, b, c, ... (ascending order). The printf (as in C) should print all of them in ascending order. First three lines should be the print function. See the samples.

2.      Your code should contain a function named sort().The parameters should be same as print(). It should contain three kinds of statements

a.      if( x < y )

b.      else

c.      print( a, b, ... );

3.      Every statement should be in a single line. And no line should be empty.

4.      Nested if statements are allowed. And every if statement should have a corresponding else statement.

5.      Assume that the inputs given to your code will be distinct. That means for any two given inputs x, y you can assume that x ≠ y.

6.      For a code for n variables there should be exactly n! * 3 +3 lines (excluding the case).

7.      Check the samples very carefully for other clarifications.


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.


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 Input

Output for Sample Input





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 );


           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 );


    print( a, c, b );


   print( c, a, b );


  if( b < c )

   if( a < c )

    print( b, a, c );


    print( b, c, a );


   print( c, b, a );



Problem Setter: Jane Alam Jan
Special Thanks: Samir Ahmed
Developed and Maintained by
Copyright © 2012
LightOJ, Jane Alam Jan