student with a question on java with array of arrays and bobble sorting
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
student with a question on java with array of arrays and bobble sorting
I have been taking java classes now for about 6 weeks now. I have been doing fairly well with figuring things out on my own. Now I have a question that I could not find the answer to in my book or solutions to similar problems through google.
here is my code for my current sort:
<code>
{
double rTemp; //rTemp is to hold values for the row
double cTemp; //cTemp is to hold values for the column, not used yet
int r, c, x, y;
for (r = 0; r < table.length - 1; ++r)
{
for (c = 0; c < table[r].length - 1; ++c)
{
for (x = 0; x < table.length - 1; ++x)
{
for (y = 0; y < table.length - 1; ++y)
{
if (table[r][c] > table[r + 1][c])
{
rTemp = table[r][c];
table[r][c] = table[r + 1][c];
table[r + 1][c] = rTemp;
}
}
}
}
}
</code>
my out out from this is as follows:
<blockquote>
1.0, 2.0, 6.0
3.0, 5.0, 4.0
Sum => 0.0, Average = > 0.0
</blockquote>
what I need to do is this:
<blockquote>
1.0, 2.0, 3.0
4.0, 5.0, 6.0
Sum => 0.0, Average = > 0.0
</blockquote>
I'll give it a shot. I just started working with the bubble sort. I am starting to grasp the concept, but with the text book doesn't seem to explain everything.
Thanks for the suggestion I will definitely try it.
PS
I need to work on my flow charting for issues like this.
Maybe try dumping them into the one array then putting them back after the sort. It's inefficient, but it's a lot easier than try to bubble sort two dimensions:
Code:
Double tempArray[] = new Double[table.length*table[0].length]();
Integer i = 0;
for(Double d[] : table){//Grow up
for(Double dd : d){
tempArray[i] = dd;
i++;
}
}
bubbleSort(tempArray);//Implement your own method. The one you have seems fine with a few adaptations for a single dimensional arrays
i = 0;
while(i < tempArray.length){
for(Integer horizontal = 0; horizontal < table.length; horizontal++){
for(Integer vertical = 0; vertical < table[0].length; vertical++){
table[horizontal][vertical] = tempArray[i];
i++;
}
}
}
I whipped this up quickly, so I haven't debugged it. Please don't whinge if it doesn't work.
Hope this helps
EDIT 2: Bubble sort is quite easy to explain. Basically, you just loop over the array comparing the elements next to each other and swap them if they're wrong. You keep doing this until you've made no changes to the array throughout a pass over it
Last edited by Fallen_Demon; 10-01-2009 at 01:29 AM.
I have been trying to solve the weekly problems on my own which I have done up to this point. I think my brain is trying to handle the logic in a more complex way than what is really needed. That's why I need to try and remember how to flow chart the sort.
I also have been trying to do the backwards coding stuff and it has seemed to help out. anyhow I am rambling now. Need to get back to think on this topic.
here is my latest code update:
public void sortTable()
{
double temp; //temp is to hold values for data being swaped
int r = 0, c = 0;
for (; r < table.length - 1; r++)
{
for (; c < table[r].length - 1; c++)
{
if(table[r][c] > table[r][c + 1])
{
temp = table[r][c];
table[r][c] = table[r +1][c];
table[r + 1][c ] = temp;
}
}
}
}
my output:
1.0, 2.0, 6.0
3.0, 5.0, 4.0
Sum => 21.0, Average = > 3.5
my data
double[][] myTable = {{3,2,6}, {1,5,4}};
processor.setTable(myTable);
processor.sortTable();
processor.tableStats();
processor.displayTable();
I do not think I am getting enough loops to complete the sort. I think I might need one more loop. If you notice the 1 is jumping to the first half of the array.
here is my latest code update:
public void sortTable()
{
double temp; //temp is to hold values for data being swaped
int r = 0, c = 0;
for (; r < table.length - 1; r++)
{
for (; c < table[r].length - 1; c++)
{
if(table[r][c] > table[r][c + 1])
{
temp = table[r][c];
table[r][c] = table[r +1][c];
table[r + 1][c ] = temp;
}
}
}
}
my output:
1.0, 2.0, 6.0
3.0, 5.0, 4.0
Sum => 21.0, Average = > 3.5
my data
double[][] myTable = {{3,2,6}, {1,5,4}};
processor.setTable(myTable);
processor.sortTable();
processor.tableStats();
processor.displayTable();
I do not think I am getting enough loops to complete the sort. I think I might need one more loop. If you notice the 1 is jumping to the first half of the array.
This is wrong. It's testing one array cell, but swapping another. These two --
Code:
table[r][c + 1]
table[r + 1][c ]
-- do not refer to the same array location. If you want to perform a bubble sort, try to pay attention to array indices and locations. Be consistent.
The best thing to do is first ask yourself what the program is supposed to do. Do you want to individually sort the rows within the array, or also sort the columns? If the latter, what will the sort criterion be?
I got the tableStats(performs the math to sum and average the numbers) done rather quickly along with the displayTable().
My final step is to bubble sort the numbers in ascending order 1-6.
Here is my brain storm (my sudo code if you will), let me know if I am getting warm.
First I want to set up the variables needed for the FOR loop that will handle the row position and the column positions. variables will be "row", "column" (chose those to help me follow through the loops)
next I want to establish the variable that will handle the temp place holder "temp"
next start the first for loop that will track the row position
for (row = 0; row < table.length; ++row)
next i need to loop through the columns of that row
for (column = 0; column < table[row].length - 1; ++column)
next I need to perform my decision logic for the columns
if ( table[row][column] > table[row][column + 1])
{
temp = table[row][column]
table[row][column] = table[row][column + 1]
table[row][column] = temp
}
this is where I lose my logic. I know I need a nested for loop to sort the numbers and a third for loop to change rows to sort the new row. And my final step is to put all the numbers in order.
public void sortTable()
{
double temp; //temp is to hold values for data being swaped
int row = 0, rows = 0, column = 0, columns = 0;
int rLength = table.length, cLength = table[row].length;
for (; row < rLength; ++row)
{
for (; column < cLength; ++column)
for (; columns < cLength - 1; columns++)
if (table[row][columns] > table[row][columns + 1])
{
temp = table[row][columns];
table[row][columns] = table[row][columns + 1];
table[row][columns + 1] = temp;
}
column = 0;
columns = 0;
}
this will sort both rows. Its kinda sloppy but it does the trick for this project. Now to
set up the swap to compare the last position in array one with the first position in array 2 and swap them if needed. Then resort the two rows.
Here is my code that does what I need for this project. It is a bit sloppy in structure(imo). Any suggestions on making this a little cleaner would be great.
Some times text books don't explain everything. Authors like to leave stuff out for a Teacher to explain.
here is my final output:
An alternative way would be to write it as if you were sorting a one dimensional array, but "translating" the loop index to the two indexes (of your array) when you're working with the two dimensional array.
For example, the fifth element in a one dimensional array corresponds to [1][1] in your array (Dimensions: 2 x 3)
Please correct these equations if they're wrong but you get the idea. The number of elements in the array - nRows*nCols - will be the upper bound of your loops.
That's starting to make sense. I will work with it after my assignment is turned in. That methodology was not explained in the text. It would really simplify the coding process.
An alternative way would be to write it as if you were sorting a one dimensional array, but "translating" the loop index to the two indexes (of your array) when you're working with the two dimensional array.
For example, the fifth element in a one dimensional array corresponds to [1][1] in your array (Dimensions: 2 x 3)
Please correct these equations if they're wrong but you get the idea. The number of elements in the array - nRows*nCols - will be the upper bound of your loops.
That's actually quite elegant I think my first idea was something similar, but I really didn't put a lot of thought into it
this is why I like this forum. Once I get this class done I will further investigate this style of coding. It appears that would me stream line to code with less overhead if you had a lot of sorting to do.
this is why I like this forum. Once I get this class done I will further investigate this style of coding. It appears that would me stream line to code with less overhead if you had a lot of sorting to do.
If you had a lot of sorting to do, you certainly wouldn't use a bubble sort:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.