LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-01-2009, 12:47 AM   #1
murankar
Member
 
Registered: Jan 2008
Location: Cleveland Ohio
Distribution: Current CentOS 5.6
Posts: 118

Rep: Reputation: 20
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>
 
Old 10-01-2009, 12:59 AM   #2
tom4everitt
Member
 
Registered: Jan 2009
Location: Stockholm
Distribution: Fedora, Ubuntu, Mac OS X
Posts: 78

Rep: Reputation: 23
As I understand it you want to sort a "matrix" of doubles, on the form:

x x x
x x x

The way I would solve it (there are of course many ways), is this:


1. Add all elements in the matrix to one array, so we end up with something like this:

x x x x x x

2. Sort this array with bubble for example (which I assume you know how to).

3. Put the three first elements back into the top row, and the three last back into the second row.

4. Done!
 
Old 10-01-2009, 01:04 AM   #3
murankar
Member
 
Registered: Jan 2008
Location: Cleveland Ohio
Distribution: Current CentOS 5.6
Posts: 118

Original Poster
Rep: Reputation: 20
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.
 
Old 10-01-2009, 01:25 AM   #4
Fallen_Demon
LQ Newbie
 
Registered: Sep 2009
Location: Australia
Distribution: Ubuntu
Posts: 13

Rep: Reputation: 2
EDIT: Damn, ninja'd

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.
 
Old 10-01-2009, 01:38 AM   #5
murankar
Member
 
Registered: Jan 2008
Location: Cleveland Ohio
Distribution: Current CentOS 5.6
Posts: 118

Original Poster
Rep: Reputation: 20
I will also consider your solution.

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.
 
Old 10-01-2009, 03:54 AM   #6
murankar
Member
 
Registered: Jan 2008
Location: Cleveland Ohio
Distribution: Current CentOS 5.6
Posts: 118

Original Poster
Rep: Reputation: 20
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.
 
Old 10-01-2009, 04:26 AM   #7
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by murankar View Post
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.
Code:
              if(table[r][c] > table[r][c + 1])
              {
                temp = table[r][c];
                table[r][c] = table[r +1][c];
                table[r + 1][c ] = temp;
              }
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?
 
Old 10-01-2009, 03:15 PM   #8
murankar
Member
 
Registered: Jan 2008
Location: Cleveland Ohio
Distribution: Current CentOS 5.6
Posts: 118

Original Poster
Rep: Reputation: 20
my final output should read as follows:

1.0, 2.0, 3.0
4.0, 5.0, 6.0
Sum => 21.0, Avarage = > 3.5


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.

again let me know if i am getting close on this.
 
Old 10-01-2009, 06:24 PM   #9
murankar
Member
 
Registered: Jan 2008
Location: Cleveland Ohio
Distribution: Current CentOS 5.6
Posts: 118

Original Poster
Rep: Reputation: 20
this sorts the first row:
Code:
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.
 
Old 10-01-2009, 07:27 PM   #10
murankar
Member
 
Registered: Jan 2008
Location: Cleveland Ohio
Distribution: Current CentOS 5.6
Posts: 118

Original Poster
Rep: Reputation: 20
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:

Quote:
1.0, 2.0, 3.0
4.0, 5.0, 6.0
Sum => 21.0, Avarage = > 3.5
Code:
public void sortTable()
    {
        double temp, tempB; //temp is to hold values for data being swaped
        int row = 0, rows = 0, column, columns, i;
        int rLength = table.length, cLength = table[row].length;
        for (i = 0; i < rLength; ++i)
        {
        for (; row < rLength; ++row)
        {
            for (column = 0; column < cLength - 1; ++column)
              for (columns = 0; 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;

                        }
            if (table[0][cLength - 1] > table[1][0])
            {
                tempB = table[0][cLength - 1];
                table[0][cLength - 1] = table[1][0];
                table[1][0] = tempB;
            }
            column = 0;
            columns = 0;
        }
        row = 0;
        }
     }
 
Old 10-01-2009, 11:09 PM   #11
gregorian
Member
 
Registered: Apr 2006
Posts: 509

Rep: Reputation: 34
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)

rowIndex = loopVar/nCols
colIndex = loopVar - nCols*rowIndex

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.
 
Old 10-01-2009, 11:28 PM   #12
murankar
Member
 
Registered: Jan 2008
Location: Cleveland Ohio
Distribution: Current CentOS 5.6
Posts: 118

Original Poster
Rep: Reputation: 20
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.

This project was the hardest so far for me.
 
Old 10-02-2009, 11:15 PM   #13
Fallen_Demon
LQ Newbie
 
Registered: Sep 2009
Location: Australia
Distribution: Ubuntu
Posts: 13

Rep: Reputation: 2
Quote:
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)

rowIndex = loopVar/nCols
colIndex = loopVar - nCols*rowIndex

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
 
Old 10-06-2009, 12:44 PM   #14
murankar
Member
 
Registered: Jan 2008
Location: Cleveland Ohio
Distribution: Current CentOS 5.6
Posts: 118

Original Poster
Rep: Reputation: 20
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.
 
Old 10-06-2009, 12:58 PM   #15
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by murankar View Post
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:

Google: quicksort
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
two dimensional array sorting in C/C++ George2 Programming 10 11-19-2006 08:29 PM
Java sorting char array nulls at end xemous Programming 1 08-10-2006 09:45 AM
I need help sorting an array in PHP! socceroos Programming 14 05-09-2006 02:37 AM
Sorting Arrays with JBuilder dflan98783 Programming 2 10-26-2005 11:42 PM
java student with serialize type question Brain Drop Programming 11 03-29-2004 08:36 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:52 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration