Question

CONVERT CODE TO JAVA // A C++ program to Print all elements in sorted order from...

CONVERT CODE TO JAVA

// A C++ program to Print all elements in sorted order from row and

// column wise sorted matrix

#include<iostream>

#include<climits>

using namespace std;

  

#define INF INT_MAX

#define N 4

  

// A utility function to youngify a Young Tableau. This is different

// from standard youngify. It assumes that the value at mat[0][0] is

// infinite.

void youngify(int mat[][N], int i, int j)

{

    // Find the values at down and right sides of mat[i][j]

    int downVal = (i+1 < N)? mat[i+1][j]: INF;

    int rightVal = (j+1 < N)? mat[i][j+1]: INF;

  

    // If mat[i][j] is the down right corner element, return

    if (downVal==INF && rightVal==INF)

        return;

  

    // Move the smaller of two values (downVal and rightVal) to

    // mat[i][j] and recur for smaller value

    if (downVal < rightVal)

    {

        mat[i][j] = downVal;

        mat[i+1][j] = INF;

        youngify(mat, i+1, j);

    }

    else

    {

        mat[i][j] = rightVal;

        mat[i][j+1] = INF;

        youngify(mat, i, j+1);

    }

}

  

// A utility function to extract minimum element from Young tableau

int extractMin(int mat[][N])

{

    int ret = mat[0][0];

    mat[0][0] = INF;

    youngify(mat, 0, 0);

    return ret;

}

  

// This function uses extractMin() to print elements in sorted order

void printSorted(int mat[][N])

{

   cout << "Elements of matrix in sorted order n";

   for (int i=0; i<N*N; i++)

     cout << extractMin(mat) << " ";

}

  

// driver program to test above function

int main()

{

  int mat[N][N] = { {10, 20, 30, 40},

                    {15, 25, 35, 45},

                    {27, 29, 37, 48},

                    {32, 33, 39, 50},

                  };

  printSorted(mat);

  return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// A Java program to Print all elements

// in sorted order from row and

// column wise sorted matrix

class GFG

{

    static final int INF = Integer.MAX_VALUE;

    static final int N = 4;

  

    // A utility function to youngify a Young Tableau.

    // This is different from standard youngify.

    // It assumes that the value at mat[0][0] is infinite.

    static void youngify(int mat[][], int i, int j)

    {

        // Find the values at down and right sides of mat[i][j]

        int downVal = (i + 1 < N) ?

                    mat[i + 1][j] : INF;

        int rightVal = (j + 1 < N) ?

                     mat[i][j + 1] : INF;

  

        // If mat[i][j] is the down right corner element,

        // return

        if (downVal == INF && rightVal == INF)

        {

            return;

        }

  

        // Move the smaller of two values

        // (downVal and rightVal) to mat[i][j]

        // and recur for smaller value

        if (downVal < rightVal)

        {

            mat[i][j] = downVal;

            mat[i + 1][j] = INF;

            youngify(mat, i + 1, j);

        }

        else

        {

            mat[i][j] = rightVal;

            mat[i][j + 1] = INF;

            youngify(mat, i, j + 1);

        }

    }

  

    // A utility function to extract

    // minimum element from Young tableau

    static int extractMin(int mat[][])

    {

        int ret = mat[0][0];

        mat[0][0] = INF;

        youngify(mat, 0, 0);

        return ret;

    }

  

    // This function uses extractMin()

    // to print elements in sorted order

    static void printSorted(int mat[][])

    {

        System.out.println("Elements of matrix in sorted order n");

        for (int i = 0; i < N * N; i++)

        {

            System.out.print(extractMin(mat) + " ");

        }

    }

  

    // Driver Code

    public static void main(String args[])

    {

        int mat[][] = {{10, 20, 30, 40},

                       {15, 25, 35, 45},

                       {27, 29, 37, 48},

                       {32, 33, 39, 50}};

        printSorted(mat);

    }

}

  

Add a comment
Know the answer?
Add Answer to:
CONVERT CODE TO JAVA // A C++ program to Print all elements in sorted order from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each...

    c++ please read all question edit the program to test different random sizes of the array and give me the time in a file will be like random size of the array and next to it the time it took for each size Im trying to do time analysis for Quick sort but i keep getting time = 0 also i want edit the program to test different random sizes of the array and give me the time in a...

  • Given a matrix, clockwise-rotate elements in it. Please add code to problem3.cpp and the makefile. I...

    Given a matrix, clockwise-rotate elements in it. Please add code to problem3.cpp and the makefile. I have also given the main_problem3.cpp and problem3.h to test. problem3.cpp::: #include "problem3.h" // A function to rotate a matrix mat[][MAX] void rotatematrix(int m, int n, int mat[][MAX]) { // your code here    } Makefile::: all : problem3.o    g++ -o mainp3 # your code here problem3.o : problem3.h problem3.cpp    # your code here clean:    rm -f *.o mainp3 main_problem3.cpp::: #include <iostream>...

  • I need a program in c++ the same as below code but I want it to...

    I need a program in c++ the same as below code but I want it to prompt the user to enter the number of elements after that I want it to ask the user to enter the array elements #include<algorithm> #include<stdio.h> #include<string.h> #include<iostream> using namespace std; int a[50]={2,5,4,3}; bool x[100]; int N=4;//number of elements    int k=10;//target sum int sum;//current target sum int cmp(const void *a,const void *b) { return *(int *)b-*(int *)a; } void backtrace(int n) { if(sum>k) return...

  • PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement...

    PLEASE CODE IN C++ AND MAKE IT COPYABLE! In this project, you will design and implement an algorithm to determine the next greater element of an element in an array in Θ(n) time, where 'n' is the number of elements in the array. You could use the Stack ADT for this purpose. The next greater element (NGE) for an element at index i in an array A is the element that occurs at index j (i < j) such that...

  • the code needs to be modifed. require a output for the code Java Program to Implement...

    the code needs to be modifed. require a output for the code Java Program to Implement Insertion Sort import java.util.Scanner; /Class InsertionSort * public class Insertion Sort { /Insertion Sort function */ public static void sort( int arr) int N- arr.length; int i, j, temp; for (i-1; i< N; i++) j-i temp arrli; while (j> 0 && temp < arrli-1) arrli]-arrli-1]; j-j-1; } arrlj] temp; /Main method * public static void main(String [] args) { Scanner scan new Scanner( System.in...

  • /* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector>...

    /* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; class Student { public:    // default constructor    Student()    {        studentID = 0;        studentFirst = "First";        studentMiddle = "Middle";        studentLast = "Last";    }    void SetStudentID(int number) { studentID = number; }    void SetStudentFirst(string name) { studentFirst = name; }    void SetStudentMiddle(string name) { studentMiddle =...

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

  • The goal is to generate some random number and output a sorted list by quicksort. what...

    The goal is to generate some random number and output a sorted list by quicksort. what do I need to add in my main to accomplish that? i also want it to output the run time. thank you #include "pch.h" #include <iostream> using namespace std; /* C implementation QuickSort */ #include<stdio.h> void swap(int* a, int* b) {    int t = *a;    *a = *b;    *b = t; } int partition(int arr[], int low, int high) {   ...

  • in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> //...

    in c++ please program for this code #include <iostream> #include <fstream> #include <string> #include <cstring> // for string tokenizer and c-style string processing #include <algorithm> // max function #include <stdlib.h> #include <time.h> using namespace std; // Extend the code here as needed class BTNode{ private: int nodeid; int data; int levelNum; BTNode* leftChildPtr; BTNode* rightChildPtr; public: BTNode(){} void setNodeId(int id){ nodeid = id; } int getNodeId(){ return nodeid; } void setData(int d){ data = d; } int getData(){ return data;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT