12093 - Sorting 2D array   

Description

Today, TA wants to sort 2d array using qsort, yet he doesn't know how to complete the compare function. Therefore, he asks for student's help.

In this problem, we need to sort 2d array under situation that:

       1. The seond dimension's size is fixed to 2. (2d array's shape is always fixed to n by 2. Ex. array[5][2] , array[7][2]) 

       2. We consider each row (has two columns) to be individual and sort them. Ex. If we want to sort array in size 4 x 2, we divide array into 4 rows: array[0], array[1], array[2], array[3] (each has 2 elements) and sort these 4 rows. 

       3. When we sort 2d array and compare any two rows, we compare first element at first, and then compare second element, all in ascending order (遞增).

 

Ex. After sorting, {1, 2} is in front of {2, 1} (because first element of {1, 2} is 1, smaller than first element of {2, 1} = 2).

After sorting, {1, 2} is in back of {1, 1} (because first element of two arrays is equal = 1, but second element of {1, 2} is larger than second element of {1, 1} = 1).

 

Note: This is a partial judge problem. We have already handled the input and output for you.

All you have to do is to implement the function "compare".

#include <stdio.h>
#include <stdlib.h>

int compare(const void* pa, const void* pb)
{
    // do comparison 
    
}

 

Input

The first line contains an integer N, representing how many rows of 2d array you should read. (2 <= N <= 100)

The following N lines, each line contains two integers which is two elements of that row in 2d array. 

We have already handled the input for you.

Output

Please print the result of 2d array sorted by rules we establish above. 

We have already handled the output for you.

Sample Input  Download

Sample Output  Download

Partial Judge Code

12093.c

Partial Judge Header

12093.h

Tags




Discuss