0

For an assignment I had to do a Bubble Sort on a 2D array of states and their state capitals. The name of the array is stateCapitals. I am supposed to sort by the state capitals.

This is the 2D array:

String[][] stateCapitals = {
{"Alabama", "Montgomery"},
{"Alaska", "Juneau"},
{"Arizona", "Phoenix"},
{"Arkansas", "Little Rock"},
{"California", "Sacramento"},
{"Colorado", "Denver"},
...
};

(continues for all 50 states.)

This is what I have for the Bubble Sort:

for (int i = 0; i < stateCapitals.length - 1; i++) {
for (int j = 0; j < stateCapitals.length - i - 1; j++) {
if (stateCapitals[j][1].compareToIgnoreCase(stateCapitals[j + 1][1]) > 0) {
String[] temp = stateCapitals[j];
stateCapitals[j] = stateCapitals[j + 1];
stateCapitals[j + 1] = temp;
}
}
}

I have never done Bubble Sort on a 2D array before and am having trouble understanding it. How does this work? I understand how Bubble Sort works, but am having trouble understanding how the implementation works. What I mean is, how does this statement perform the sort step by step?

I've been looking at it and researching for a while and need a fresh perspective.

I would really appreciate some help.

Thanks!

4
  • 1
    in short: a 2D array (in Java) is just an array whose elements just happen to also be arrays (an array is just an object/instance) Commented Mar 7, 2024 at 22:45
  • Your problem should become a lot clearer if you convert the two-dimensional array of Strings into one dimensional array of State-objects that contain the name of the state and the capital city. Then you implement a bubble sort on the one dimensional array and use the capital city name as the comparison value. This was also a lesson in making readable and maintainable object oriented programs. I practically never use two dimensional arrays in professional work. When I see one it's almost always a sign that someone didn't know what they were doing. Commented Mar 8, 2024 at 7:23
  • @Torben you are absolutely right about 2D arrays. I usually never use them. But in this situation I'm working on an assignment and was given a prompt that specifically said I have to do it this way. If I don't, I will get points deducted for not answering the question. I really like your idea better. I will bear it in mind for future use. Thank you for your comment! Commented Mar 8, 2024 at 20:10
  • For some reason, the accepted answer was deleted. Commented Mar 18, 2024 at 16:28

1 Answer 1

0

The basic idea of bubble sort is to find the largest item, and move it step-by-step (bubble it) to the last position on the first iteration. The second iteration will bubble the 2nd largest item to the next to last position. The third iteration ...

You said you understand how bubble sort works, so this answer will be about 2D arrays.

Java doesn't have true 2D arrays. But, in Java, an array is an Object. And Java allows arrays of Objects.

A consequence is arrays of arrays are allowed. It's like nested arrays. Arrays can be "nested" to provide the functionality of 2D, 3D, 4D, etc. arrays.

Suppose code has Foo [][] arr = new Foo [5][7];. It may help to think of arr as having 5 rows, where each row has 7 columns. A row may be accessed as a single unit: Foo [] bar = arr [3]; Of course, a variable may be used to specify the row: bar = arr [rowIndex];

Note that that code does not result in copying. The variable bar is an alias for one of the rows of arr.

The variable stateCapitals in your code is an array of array of String. One row has two String Objects: A state name, and the name of the capital for that state.

String[] temp = stateCapitals[j]; 
stateCapitals[j] = stateCapitals[j + 1];
stateCapitals[j + 1] = temp;

This works the same as swapping any other pair of Objects:

Object a, b;
...
Object temp = a; // temp and a are aliases of each other
a = b;  // now a and b are aliases of each other. 
        // temp is no longer alias of a
        // but a still refers to what a used to refer to
b = temp;  // now b refers to what a used to refer to
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.