PRASHNIKAप्रश्निका
‹ Back to the paper

[Arrays] Given below is an example of a double dimensional array with unequal number of rows and…

Computer Science202510 marksProgram
[Arrays] Given below is an example of a double dimensional array with unequal number of rows and columns. It must be arranged in such a way that the elements in the even rows are in ascending order and the elements in the odd rows are in descending order, using Insertion sort technique. Sample Input : Row=3 Column=4 Enter the array elements 1 2 3 4 9 8 7 6 4 5 6 7 Before sort: 1 2 3 4 9 8 7 6 4 5 6 7 After sort: 4 3 2 1 6 7 8 9 7 6 5 4 The members of the class are given below: Class name : Mix_sort Data members / instance variables: a[][] : to store integers in the double dimensional array. m : to store the number of rows. n : to store the number of columns. Methods / Member functions : Mix_sort(int row, int col) : parameterised constructor to initialise the m=row, n=col and allocate memory for a[][] void input() : to enter the integer elements in a [][] void ascEven(int x[]) : arrange the elements of x[] in ascending order using Insertion sort technique. void dscOdd(int x[]) : arrange the elements of x[] in descending order using Insertion sort technique. void arrange() : to arrange the rows in even position in Ascending order using ascEven(.. ) method and rows in odd position in descending order using dscOdd(..) method. Finally store the sorted elements into a[][] void display( ) : display the original a[][] and sorted a[][] . Define the class Mix_sort with details of the constructor, void input(), void arrange(), void ascEven(int []), void dscOdd(int []) and void display( ). Define a main() function to create an object and call the functions accordingly to enable the task.

Sample input/output

Sample Input :
Row=3
Column=4
Enter the array elements
1 2 3 4
9 8 7 6
4 5 6 7

Before sort:
1   2   3   4
9   8   7   6
4   5   6   7

After sort:
4   3   2   1
6   7   8   9
7   6   5   4

Answer

Answer

AI
import java.util.Scanner;

class Mix_sort
{
    int a[][];   // stores the integer array
    int m;       // number of rows
    int n;       // number of columns

    // parameterised constructor
    Mix_sort(int row, int col)
    {
        m = row;
        n = col;
        a = new int[m][n];
    }

    // accepts the integer elements into a[][]
    void input()
    {
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < m; i++)
            for (int j = 0; j < n; j++)
                a[i][j] = sc.nextInt();
    }

    // arranges x[] in ascending order using insertion sort
    void ascEven(int x[])
    {
        for (int i = 1; i < x.length; i++)
        {
            int key = x[i];
            int j = i - 1;
            while (j >= 0 && x[j] > key)
            {
                x[j + 1] = x[j];
                j--;
            }
            x[j + 1] = key;
        }
    }

    // arranges x[] in descending order using insertion sort
    void dscOdd(int x[])
    {
        for (int i = 1; i < x.length; i++)
        {
            int key = x[i];
            int j = i - 1;
            while (j >= 0 && x[j] < key)
            {
                x[j + 1] = x[j];
                j--;
            }
            x[j + 1] = key;
        }
    }

    // sorts each row: even (1-indexed) row positions ascending, odd row positions descending
    void arrange()
    {
        for (int i = 0; i < m; i++)
        {
            int row[] = new int[n];
            for (int j = 0; j < n; j++)
                row[j] = a[i][j];
            if ((i + 1) % 2 == 0)
                ascEven(row);
            else
                dscOdd(row);
            for (int j = 0; j < n; j++)
                a[i][j] = row[j];
        }
    }

    // displays the original array, then arranges it and displays the sorted array
    void display()
    {
        System.out.println("Before sort:");
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
                System.out.print(a[i][j] + "\t");
            System.out.println();
        }
        arrange();
        System.out.println("After sort:");
        for (int i = 0; i < m; i++)
        {
            for (int j = 0; j < n; j++)
                System.out.print(a[i][j] + "\t");
            System.out.println();
        }
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        System.out.print("Row=");
        int row = sc.nextInt();
        System.out.print("Column=");
        int col = sc.nextInt();
        Mix_sort obj = new Mix_sort(row, col);
        System.out.println("Enter the array elements");
        obj.input();
        obj.display();
    }
}
Explanation: ascEven() and dscOdd() are both plain insertion sort, just with the comparison reversed. arrange() copies each row into a temporary array, sorts it ascending if the row's 1-indexed position is even and descending if odd, then writes it back into a[][]. display() prints the array as first entered, calls arrange(), then prints the result. Tested (run for real) with the question's own 3x4 example (rows 1 2 3 4 / 9 8 7 6 / 4 5 6 7): output was exactly 'Before sort: 1 2 3 4 / 9 8 7 6 / 4 5 6 7' then 'After sort: 4 3 2 1 / 6 7 8 9 / 7 6 5 4', matching the sample in the question.
Arrays, Strings

From ISC Computer Science - Competency Focused Practice Questions (CISCE, August 2024), question 75.