# Matrix problem in java using bubble sort

• Comp Sci
• ishika17
In summary, the program is designed to take a matrix as input and store it in A[][] with the specified number of rows and columns. It also includes methods to display the matrix, calculate the sum of the boundary elements, find and store the maximum and minimum values, and sort the matrix in descending order using bubble sort.
ishika17
Homework Statement
We have to design a program in Java to accept the dimensions of a matrix such that the dimensions are greater than 2 and less than 8.Then we have to allow the console to enter integers in the array.
we have to display the original matrix with the sum of it's boundary matrix and Also display the matrix and the sum of boundary elements after sorting the elements in descending order using a Bubblesort technique.
Relevant Equations
none
I have taken the variables as follows:
A[][]=the matrix
max=to store the maximum integer value present in the matrix
min=to store the minimum integer value present in the matrix
sum=to store the sum of boundary elements
display()=methos to print matrix
sort()=method to sort matrix in descending order using bubblesort
store()=to find and store maximum and minimum values in max and min
bsum()=to calculate the sum of boundary elements

I've fixed the problem.If you want to have a look

Java:
import java.util.*;
class d {
int A[][];
int M, N, sum, max, min;
d(int mm, int nn) {
M = mm;
N = nn;
A = new int[M][N];
max = 0;
min = 0;
sum = 0;
}
void Input() {
Scanner sc = new Scanner(System.in);
System.out.println("ENTER THE ELEMENTS OF THE MATRIX");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
A[i][j] = sc.nextInt();
}
}
}
void display() {
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
System.out.print(A[i][j] + "\t");
}
System.out.println();
}
}
void bsum() {
sum = 0;
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (i == 0 || j == 0 || i == M - 1 || j == N - 1)
sum = sum + A[i][j];
}
}
System.out.println("THE SUM OF BORDER ELEMENTS IS:" + sum);
}
void store() {
max = A[0][0];
min = A[0][0];
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (A[i][j] > max)
max = A[i][j];
if (A[i][j] < min)
min = A[i][j];
}
}
System.out.println("MAX=" + max + "MIN=" + min);
}
void sort() {
int in1 = 0, in2 = 0, temp = 0;
for (int i = max; i >= min; i--) {
for (int j = 0; j < M; j++) {
for (int k = 0; k < N; k++) {
if (A[j][k] == i) {
temp = A[in1][in2];
A[in1][in2] = A[j][k];
A[j][k] = temp;
if (in2<=N-2)
in2++;
else
{
in2 = 0;
in1++;
}
}
}
}
}
}
public static void main(String args[]) {
int m, n;
Scanner sc = new Scanner(System.in);
do {
System.out.println("ENTER THE ROWS AND COLUMNS BETWEEN 2 AND 8");
m = sc.nextInt();
n = sc.nextInt();
System.out.println("m=" + m + "n=" + n);
if (m <= 2 || n <= 2 || m >= 8 || n >= 8)
System.out.println("OUT OF RANGE");
}
while (m <= 2 || n <= 2 || m >= 8 || n >= 8);
d ob = new d(m, n);
ob.Input();
System.out.println("THE ORIGINAL MATRIX IS:");
ob.display();
ob.bsum();
ob.store();
ob.sort();
System.out.println("THE SORTED MATRIX IS:");
ob.display();
ob.bsum();
}
}

Last edited:
ishika17 said:
Homework Statement:: We have to design a program in Java to accept the dimensions of a matrix such that the dimensions are greater than 2 and less than 8.Then we have to allow the console to enter integers in the array.
This confused me at first. The matrix is two-dimensional. What you're calling dimensions are actually the row size and column size.
ishika17 said:
we have to display the original matrix with the sum of it's boundary matrix and Also display the matrix and the sum of boundary elements after sorting the elements in descending order using a Bubblesort technique.
Relevant Equations:: none

I have taken the variables as follows:
A[][]=the matrix
max=to store the maximum integer value present in the matrix
min=to store the minimum integer value present in the matrix
sum=to store the sum of boundary elements
display()=methos to print matrix
sort()=method to sort matrix in descending order using bubblesort
store()=to find and store maximum and minimum values in max and min
bsum()=to calculate the sum of boundary elements
bsum() was also confusing at first until I saw the code. Your comment should state that bsum() calculates the sum of the first elements of each row and the last elements of each row.
ishika17 said:
This is my attempted code

Java:
import java.util.*;
class d {
int A[][];
int M, N, sum, max, min;
d(int mm, int nn) {
M = mm;
N = nn;
A = new int[M][N];
max = 0;
min = 0;
sum = 0;
}
void Input() {
Scanner sc = new Scanner(System.in);
System.out.println("ENTER THE ELEMENTS OF THE MATRIX");
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
A[i][j] = sc.nextInt();
}
}
}
void display() {
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
System.out.print(A[i][j] + "\t");
}
System.out.println();
}
}
void bsum() {
sum = 0;
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (i == 0 || j == 0 || i == M - 1 || j == N - 1)
sum = sum + A[i][j];
}
}
System.out.println("THE SUM OF BORDER ELEMENTS IS:" + sum);
}
void store() {
max = A[0][0];
min = A[0][0];
for (int i = 0; i < M; i++) {
for (int j = 0; j < N; j++) {
if (A[i][j] > max)
max = A[i][j];
if (A[i][j] < min)
min = A[i][j];
}
}
System.out.println("MAX=" + max + "MIN=" + min);
}
void sort() {
int in1 = 0, in2 = 0, temp = 0;
for (int i = max; i >= min; i--) {
for (int j = 0; j < M; j++) {
for (int k = 0; k < N; k++) {
if (A[j][k] == i) {
temp = A[in1][in2];
A[in1][in2++] = A[j][k];
A[j][k] = temp;
if (in2 < N - 1)
in2++;
else
in1++;
if (in2 == N)
in2 = 0;
if (in1 == M)
in1 = 0;
}
}
}
}
}
public static void main(String args[]) {
int m, n;
Scanner sc = new Scanner(System.in);
do {
System.out.println("ENTER THE ROWS AND COLUMNS BETWEEN 2 AND 8");
m = sc.nextInt();
n = sc.nextInt();
System.out.println("m=" + m + "n=" + n);
if (m <= 2 || n <= 2 || m >= 8 || n >= 8)
System.out.println("OUT OF RANGE");
}
while (m <= 2 || n <= 2 || m >= 8 || n >= 8);
d ob = new d(m, n);
ob.Input();
System.out.println("THE ORIGINAL MATRIX IS:");
ob.display();
ob.bsum();
ob.store();
ob.sort();
System.out.println("THE SORTED MATRIX IS:");
ob.display();
ob.bsum();
}
}
Your store() method is more complicated than it needs to be. All it needs to do is to find the largest value in the matrix and the smallest value. If you call sort() first (once you get it working correctly), the largest value will be at A[0][0] and the smallest value will be at A[M-1][N-1]. You don't need any loops at all.
ishika17 said:
https://www.physicsforums.com/attachments/281126

Although it's giving the output but he elements are not sorted completely

It should be:
11 9 8
6 5 4
3 2 -3

So guys, I'm hoping you could help me point out where the problem is.
Thank You.
Pretty obviously, there is something wrong in your bubble sort. I'm reasonably sure you have access to a debugger, so I suggest you use it to single-step through your code in your sort() method, and see where things are going wrong. I suspect, but don't know for sure, that your problem is here:
Java:
      for (int j = 0; j < M; j++) {
for (int k = 0; k < N; k++) {
Namely, that you have M and N switched. Try your code out on a small matrix, say 2 rows of 3 columns, and see if the code does something wrong.

Other comment
Your choice of names for variables is not good. You have too many one-letter variable names, which makes it difficult for a reader (me) to understand what they are used for. I've taught many programming classes, and I always encourage my students to use meaningful names (by deducting points if they don't do so).
Instead of M and N, use something like RowSize and ColSize. Single letter names for loop variables such as i, j, and k, are fine, but all other variables should have a meaningful name.

Your program uses d (terrible name for a class), m, n, mm, nn, M, N, A -- all should be more descriptive. Not quite as bad are in1, in2, and ob.

ishika17
Your comment should state that bsum() calculates the sum of the first elements of each row and the last elements of each row.
but it actually calculates the border elements which also includes the elements in first and and last columns and not just rows.

If you call sort() first (once you get it working correctly), the largest value will be at A[0][0] and the smallest value will be at A[M-1][N-1]
I'm calculating maximum and minimum values so i can sort them in descending order because i couldn't think of how to sort the the elements which are arranged in both rows and columns.

Your choice of names for variables is not good. You have too many one-letter variable names, which makes it difficult for a reader (me) to understand what they are used for
even i don't like it.
But sadly, when this is what you've been taught since your first programming class,this is what happens.
they would ask us not to take names of variable and class which are long (like index,Maxvalues,etc.) even though they are sensible.
I'll take note of it.

by deducting points if they don't do so
i agree.
Your program uses d (terrible name for a class), m, n, mm, nn, M, N, A -- all should be more descriptive. Not quite as bad are in1, in2, and ob.
thank you.I'll take note of it.

This is why I'm actually here and i hope to improve further.

ishika17 said:
but it actually calculates the border elements which also includes the elements in first and and last columns and not just rows.
The set of first elements in each row is the set of elements in the first column. Similarly, the set of last elements in each row is the set of elements in the last column.

ishika17
Mark44 said:
Your program uses d (terrible name for a class), m, n, mm, nn, M, N, A -- all should be more descriptive. Not quite as bad are in1, in2, and ob.
ishika17 said:
they would ask us not to take names of variable and class which are long (like index,Maxvalues,etc.) even though they are sensible.
I'm very sorry to hear that your instructor is giving you such bad advice. As I mentioned, I taught programming classes for a long time ( > 25 years), plus I worked in the software industry. Every textbook and every development team I worked with emphasized using informative variable names. They don't have to be long, just long enough to make it easy for someone else reading your code to understand what the variable is being used for. Your instructor doesn't know what he or she is talking about. It might not be a good strategy to convey my opinion to your instructor, but that is my considered opinion.
ishika17 said:
This is why I'm actually here and i hope to improve further.
Good for you! There are quite a few people here who are very knowledgeable in all sorts of programming languages.

ishika17

## 1. What is a matrix problem in java?

A matrix problem in java refers to a programming challenge where a two-dimensional array or matrix needs to be sorted using a specific algorithm. In this case, the bubble sort algorithm is used to arrange the elements of the matrix in a specific order.

## 2. How does the bubble sort algorithm work?

The bubble sort algorithm works by repeatedly comparing adjacent elements in an array and swapping them if they are not in the correct order. This process is repeated until the entire array is sorted in the desired order.

## 3. Why is bubble sort commonly used for matrix problems in java?

Bubble sort is commonly used for matrix problems in java because it is a simple and easy-to-implement sorting algorithm. It also has a relatively low time complexity, making it efficient for sorting large arrays.

## 4. What is the time complexity of bubble sort?

The time complexity of bubble sort is O(n^2), where n is the number of elements in the array. This means that as the size of the array increases, the time taken to sort it also increases exponentially.

## 5. Are there any drawbacks to using bubble sort for matrix problems in java?

Yes, there are some drawbacks to using bubble sort for matrix problems in java. It is not efficient for sorting large arrays, and it may take a long time to sort if the array is already mostly sorted. Additionally, it is not suitable for sorting complex data structures such as linked lists.

• Engineering and Comp Sci Homework Help
Replies
18
Views
1K
• Engineering and Comp Sci Homework Help
Replies
3
Views
1K
• Engineering and Comp Sci Homework Help
Replies
12
Views
1K
• Engineering and Comp Sci Homework Help
Replies
7
Views
2K
• Engineering and Comp Sci Homework Help
Replies
5
Views
2K
• Engineering and Comp Sci Homework Help
Replies
1
Views
1K
• Engineering and Comp Sci Homework Help
Replies
14
Views
3K
• Engineering and Comp Sci Homework Help
Replies
1
Views
1K
• Programming and Computer Science
Replies
8
Views
1K
• Engineering and Comp Sci Homework Help
Replies
2
Views
3K