Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7639065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:19:27+00:00 2026-05-31T08:19:27+00:00

This is the assignment: Write a method that sorts the elements of a matrix

  • 0

This is the assignment: Write a method that sorts the elements of a matrix with 2 dimensions. For example

sort({{1,4}{2,3}}) 

would return a matrix

{{1,2}{3,4}}.

I dont know what im doing wrong in my code cause the output i get is 3.0, 3.0, 4.0, 4.0.

This is what i have so far any help would be appreciated.

public static void main(String[] args) {
    double[][] array =  { {1, 4}, {2, 3} };
    double[][] new_array = sort(array);
    for(int i = 0; i < array.length; i++) {
        for(int j = 0; j < array.length; j++) {
            System.out.print(new_array[i][j] + " ");
        }
    }

}

public static double[][] sort(double[][] array) {
    double[] storage = new double[array.length];
    for(int i = 0; i < array.length; i++) {
        for(int j = 0; j < array.length; j++) {
            storage[i] = array[i][j];
        }
    }
    storage = bubSort(storage);
    for(int i = 0; i < array.length; i++) {
        for(int j = 0; j < array.length; j++) {
            array[i][j] = storage[i];
        }
    }
    return array;
}

public static double[] bubSort(double[] list) {
    boolean changed = true;
    double temp;
    do {
    changed = false;
    for (int j = 0; j < list.length -1; j++)
    if (list[j] > list[j + 1]) {
        temp = list[j];
        list[j] = list[j + 1];
        list[j + 1] = temp;
        changed = true;
    }
    } while (changed);
    return list;
}

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T08:19:29+00:00Added an answer on May 31, 2026 at 8:19 am

    The main problem that you are experiencing is how you are copying the values from the 2d array into the 1d array. You are actually only copy two values into an array of length 2. The length of a 2d array is not the full m x n length.

    I will give a small hint how you can go about copying from the 2d array into the 1d array, but it is up to you to figure out how to copy back from the 1d array into the 2d array. Also, how would you go about finding the full length of the array?

        double[] storage = new double[4];//You should calculate this value
        int k = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array.length; j++) {
                storage[k++] = array[i][j];
            }
        }
    

    Your bubble sort works fine, but then you are copying the values back wrong. Try printing the array storage after the sort and you will see that it is now correct.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program is in response to the assignment: Create a method named Sum() that
I am trying to write public instance method createParther() that returns an instance of
I'm taking a beginning Java class and an assignment requires that I write a
ok so this is the assignment that I'm working on: Implement a class Student
I have a homework assignment that I am supposed to write a http server
My class assignment is to write a program that has the user input a
This is part of a homework assignment. What we have to do is write
*My assignment is to write a program that stores an array of five int
First of all, I'll state that this is a university assignment so I'm not
For this assignment I had to create my own string class. I initially wrote

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.