• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Bubble Sort Array, read from a text file.

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
I am working on an assignment at the moment was wondering if I could receive some guidance.

What I am trying to do is read 3 columns from a text file and to put them into 3 arrays.
After putting them into arrays a Bubble sort method is used to sort the data using 1 of the arrays.

My program will read the file, put them into arrays, and print them out just fine, however the bubble sort method is giving me some trouble.

=Here is my code=



I followed a template from my instructor for the bubble sort method and i think its ok?
the output for the bubble sort shows the value of the LAST "diameters" followed my 9 zeros on the same line.
The project is not yet compete and i would like to take this step-by-step if you were wondering why this didnt make sense.
Any help would be much appreciated.

Regards
Jihwan



 
Bartender
Posts: 1464
32
Netbeans IDE C++ Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jihwan,

I think your problem is at Line 67. Look at Line 65 and Line 67, think about how they work their way through the arrays. Is Line 67 really doing what you want? Try simulating what bubbleSortArray() does with bits of paper that have diameters written on them. If you follow your code exactly as you wrote it, you'll see the problem.
 
Ranch Hand
Posts: 384
Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kim,

You are messing things up in the bubblesortarray()
Re read the algorithm for bubble sort and do as Stevens said ... take a notebook and trace it ...

that's the best way as a beginner to solve what's wrong

cheers
 
jihwan kim
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for responding guys. It's finals week for me so I am doing a lot of things at once, so if i dont reply fast enough it's not because I forgot or that I am ignoring the posts!

Going to take the suggestions and read aloud what my program is actually doing.



Will report when I have time, I apologize for your time.


regards
Jihwan
 
jihwan kim
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have reworked the code and figured out what the problem was.
I also changed the variables i and j in the bubbleSortArray method to n and m because I was initially worried about i and j corresponding with the variables in the main. Now I know that what i thought was completely mistaken because of different/new initializations.

Anyway I have:
1) changed the inequality in the second for-loop for the bubbleSortArray method.
2) added println statements to make sure that what i was doing was right.
3) figured out why i was getting repeated zeros when i wanted to print.
*I forgot to add the ++ to my array variables which made it so that only the last value (pluto) was stored in*



~~~~NEW QUESTION!~~~~
Now that I have bubble sorted my "diameters" array what would be the best way to sort my "planets" and "lengths" array to keep it straight when I sort the"diameters" array?

I am thinking that I can use my bubbleSortArray to make the changing n/m variables equal to a new String and a new Double variable which coincide with the other arrays.
Do you think that would be too clustered or is my thought process not doable?


Thank you-
Regards

Jihwan
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

jihwan kim wrote:
Now that I have bubble sorted my "diameters" array what would be the best way to sort my "planets" and "lengths" array to keep it straight when I sort the"diameters" array?



You should not have "parallel" arrays as you're currently doing.

Rather than an array of planet names, an array of diameters, and an array of day lengths, you should instead:
  • Define a class called, say, Planet, that has fields for name, diameter, and day length.
  • Create a single array of Planet objects.
  • Sort that array.


  • Defining the Planet class is a more object-oriented approach than what you're currently doing, and since Java is an OO language, you might as well take advantage of that fact and save yourself some headaches. (Although, the idea of defining a Planet type is not strictly an OO thing. Even if you were doing this in C or Pascal you'd still want to do the same thing.)
     
    jihwan kim
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Wow thanks for reminding me that I am stupid. I was suppose to create parallel arrays, but i thought that meant something else...
    Guess I have to rework my code!


    EDIT: Just wondering is there a way to create parallel arrays in just my Main instead of creating a separate class?
    And why are some people (internets) saying that creating parallel arrays isnt a good idea? Are they just lazy or is it a bad coding technique/etiquette?


    Much appreciated Jeff


    Regards
    Jihwan
     
    Marshal
    Posts: 80781
    489
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    jihwan kim wrote:Wow thanks for reminding me that I am stupid.

    I am sure that is not what anybody meant.

    I was suppose to create parallel arrays, . . .

    As you have been told, parallel arrays are poor design; you should query that instruction with whoever told you about parallel arrays.

    EDIT: Just wondering is there a way to create parallel arrays in just my Main instead of creating a separate class?

    That would make things worse; the main method is intended for starting the application, and nothing else. You are liable to create a large, un‑maintainable, class, which is another form of bad design.


    And why are some people (internets) saying that creating parallel arrays isnt a good idea? Are they just lazy or is it a bad coding technique/etiquette? . . .

    Not lazy; parallel arrays require much more work than a proper object‑oriented solution. You have to follow all your parallel arrays; if you sort array1, you have to duplicate all changes exactly in array2, array3, etc. That is error‑prone, and there is a risk of your missing out a change somewhere. Sorting a Planet[] array means you sort one array, and all the data for each Planet object stay together in that object.

    Create a nice object‑oriented solution and you will be surprised how straightforward and simple and neat the entire code looks. Even in a language like Java which seems to occupy a lot of space on the page.
     
    jihwan kim
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thank you for the reply Campbell.
    It is definitely easier for me to create a class where it reads in the txt file to create 1 array instead of creating 3 separate arrays.
    I ask because usually when my prof assigns homework he outlines whether or not we need a Class and/or Main.

    He's a nice guy and all, but he makes us follow an unspoken rubric (i.e what the instructions says) so that we show him that we know how to do exactly what he asks us to do. I am not complaining by any means, I just have to follow instructions

    Unless I am interpreting this the wrong way the instruction says:
    Read the data into three parallel arrays
    *planets(String) * = bullet points
    *diameters(int)
    *lengths(double)

    Anyway I'll probably make 2 projects, one with and one without parallels.

    Not lazy; parallel arrays require much more work than a proper object‑oriented solution. You have to follow all your parallel arrays; if you sort array1, you have to duplicate all changes exactly in array2, array3, etc. That is error‑prone, and there is a risk of your missing out a change somewhere. Sorting a Planet[] array means you sort one array, and all the data for each Planet object stay together in that object.


    I hear ya. I am trying to see if i can keep up with this sorting by doing everything in my bubbleSortArray method but I dont think that is going to work.
    Can you not typecast values stored in arrays? I thought sorting and tracking would be easy seeing that i could make the index of my int variable equal the index of the double variable (a[j] = b[j]).
    Then I thought about declaring 4 more variables in my bubbleSort (i,j,k,l,m,n) to do the same thing except make them = to each other to keep track , however that lead me to the problem of int[] != double[] and String[] !=int[].

    Sorry for rambling but I am in thinking mode for finalz.

    Much appreciated
    Jihwan



     
    Campbell Ritchie
    Marshal
    Posts: 80781
    489
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You’re welcome

    Are you telling us you are being taught poor program design?

    jihwan kim wrote: . . . Can you not typecast values stored in arrays?

    Yes, but what difference would that make?

    I thought sorting and tracking would be easy seeing that i could make the index of my int variable equal the index of the double variable (a[j] = b[j]).

    You mean use the same i throughout? You see what confusion is arising?


    Then I thought about declaring 4 more variables in my bubbleSort (i,j,k,l,m,n) . . .

    Or tying yourself in knots because you are stuck with a poor design?
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    jihwan kim wrote: all, but he makes us follow an unspoken rubric (i.e what the instructions says) so that we show him that we know how to do exactly what he asks us to do. I am not complaining by any means, I just have to follow instructions

    Unless I am interpreting this the wrong way the instruction says:
    Read the data into three parallel arrays
    *planets(String) * = bullet points
    *diameters(int)
    *lengths(double)



    Oh, so you're actually being required to do it the wrong way. Ouch!

    It may be that he's doing it this way on purpose--showing you the icky approach first so that you'll appreciate the more OO approach when he has you do it again that way. If so, then I would call that acceptable. If he's actually teaching this as the right way to store and manipulate data though, then I'd take anything he teaches with a grain of salt.

    It can be done using parallel arrays. When you're sorting, you have to make sure that every change you make to one array gets made to all of them. So if you swap planets[2] with planets[3], then you also have to swap diameters[2] with diameters[3] and lengths[2] with lengths[3].

    Also, his naming sucks. Since these are all properties of a given planet, the planets array would better be named "names" and the lengths array would better be named "dayLengths."
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    jihwan kim wrote:Wow thanks for reminding me that I am stupid.

    I am sure that is not what anybody meant.



    Definitely not. Jihwan, you seemed to be falling into a common trap that snags a lot of beginners, and I was trying to steer you away from it. I didn't realize at the time that you were required to use parallel arrays.

    Campbell Ritchie wrote:

    jihwan kim wrote:
    And why are some people (internets) saying that creating parallel arrays isnt a good idea? Are they just lazy or is it a bad coding technique/etiquette? . . .

    Not lazy;



    Well, yes lazy, but in the way that Larry Wall describes laziness as a positive attribute of a good programmer. The whole reason for using computers in the first place is to make them do the hard work, not us.

    And the "some people" that recommend against parallel arrays include pretty much every programmer with any real-world experience.
     
    jihwan kim
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    Or tying yourself in knots because you are stuck with a poor design?


    -yes Campbell, this coding is breaking my balls.

    It may be that he's doing it this way on purpose--showing you the icky approach first so that you'll appreciate the more OO approach when he has you do it again that way. If so, then I would call that acceptable. If he's actually teaching this as the right way to store and manipulate data though, then I'd take anything he teaches with a grain of salt.


    Thanks for responding Jeff. Iam pretty sure he is teaching us the long way to do it, just like in calculus I where you use the fundamental theorem to find the derivative when it shouldnt be that complicated.


    It can be done using parallel arrays. When you're sorting, you have to make sure that every change you make to one array gets made to all of them. So if you swap planets[2] with planets[3], then you also have to swap diameters[2] with diameters[3] and lengths[2] with lengths[3].



    I get that I have to swap the variables when I am making a change from the index, however I am completely stuck at how to code it right.

    Whats messing me up is that because my bubbleSortArray has int[] a as the variable for the diameter index I dont know if I should add more parameters (String[]b or String[]a or double[] a) and make it equal (int[] a = double[] b) to each other when I know that this way is not allowed.*

    Wouldnt having the same index parameter 'a' be the right way to go about it because the I am basing everything on the diameters index to keep track of the other 2 variables?

    I apologize for using the word I too much, and the fact that I am not explaining my thoughts clearly.

    Edit: just wondering would it be easier to create another method so that when I printArray the switched diameter values, this new method would take the Index of the switched diameter and makes it equal to the corresponding planets and lengths index? #derp
    regards

    Jihwan
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    jihwan kim wrote:I get that I have to swap the variables when I am making a change from the index, however I am completely stuck at how to code it right.



    What part are you stuck on? Do you know how to swap two entries in a single array? If so, then you know how to swap two entries in any number of arrays.

    Whats messing me up is that because my bubbleSortArray has int[] a as the variable for the diameter index I dont know if I should add more parameters (String[]b or String[]a or double[] a) and make it equal (int[] a = double[] b) to each other when I know that this way is not allowed.*

    Wouldnt having the same index parameter 'a' be the right way to go about it because the I am basing everything on the diameters index to keep track of the other 2 variables?



    No idea what you're saying here.

    You'll have a sort() method, that will either accept all 3 arrays as args, or else will take no args, if the arrays are member variables of your class.

    That method will follow bubblesort's algorithm for which pairs of elements to compare and swap, and for that part it will look only at one array--whatever you're sorting by, name, diameter, or dayLength.

    When that algorithm determines that, for example, name[2] is larger than name[7], then it will swap item 2 with item 7 for all 3 arrays.

    Edit: just wondering would it be easier to create another method so that when I printArray the switched diameter values, this new method would take the Index of the switched diameter and makes it equal to the corresponding planets and lengths index? #derp
    regards



    Again, not sure what you're saying here, but in general, yes, if you think it might be easier to do some task in its own method, it almost always will be. A method should be small and should do one thing.
     
    jihwan kim
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Dope, thanks for the response.

    Will be back when I finish up some coding.



    regards

    Jihwan
     
    jihwan kim
    Greenhorn
    Posts: 14
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    No idea what you're saying here.


    Again, not sure what you're saying here
    -Jeff


    hahahahah

    Ok so after going through the code, with the help of cigarettes, I figured out what I was doing wrong.

    The troubles I was having with the parallel arrays in the bubble sort was that:
    1) I knew I had to compare and swap the elements, but I didn't know how.
    2) I thought that I could sort the 3 arrays by using just the 2 original parameters for my bubbleSortArray method.
    3) I was trying to compare the elements in an all sorts of crazy ways.
    4) I just had too much shit written down making things a lot more complicated.

    I played around with making more 'temp' variables in conjunction with expanding the parameter for my sort method. and figured out that this was the way to go.
    Because I originally had 'int temp;' for my diameters variable, I thought that I could just add more initializations to the int.
    Instead I added the string and double parameters, and created String and double temp variables using the SAME algorithm for the diameters variable.

    My life was finally complete.
    Also got rid of all those extraneous println and printarray statements showing the process of where/what index was switching.

    ==Here is my Bau5 mode code==


    Having gone through the seven circles of hell, I honestly dont think that parallel arrays are that bad. I mean yeah it took my stupid brain a long time to figure out 6 lines of code, but in all seriousness that wasnt too bad.
    If i had created a class as You and Campbell have suggested, I think that would've been much more work and unnecessary for this specific code.

    I am so grateful so all the help and suggestions. I usually finish my homework promptly but this one was confusing and tricky.
    Thank You - jeff, Campbell, Lalit, and Stevens.

    regards
    Jihwan
     
    Jeff Verdegan
    Bartender
    Posts: 6109
    6
    Android IntelliJ IDE Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    jihwan kim wrote:Having gone through the seven circles of hell, I honestly dont think that parallel arrays are that bad. I mean yeah it took my stupid brain a long time to figure out 6 lines of code, but in all seriousness that wasnt too bad.
    If i had created a class as You and Campbell have suggested, I think that would've been much more work and unnecessary for this specific code.



    It can be a little hard to get your head around at first, especially if you're already looking at it in a different way. In terms of amount of code, in some cases it will be comparable, and in some cases one or the other will have more code, although I doubt parallel arrays will win the "less code" contest by a significant amount in many cases.

    The main reason to use OO techniques is that the code more closely models the way we think about the problem naturally. I don't think of "a bunch of names, diameters and day lengths, with the 3 values at a given position corresponding to those value for one planet. I think about "a bunch of planets, and each planet has a name, a diameter, and a day length."

    Going back to amount of code though, here's one example:


    Now, imagine we also decide to keep track of mass, distance from earth, orbital period, surface gravity, and number of moons. With parallel arrays, that's 5 more 3-line swaps we have to make, and if we change it again, we have to go and update our swap code again. With a Planet class, the above swap code does not change at all, no matter how many additional parameters we add.

    It is true that we could write the method in such a way that it could be used in the general case of parallel arrays, without having to modify it if we add parameters, but then it gets even harder to see how the code relates to what we're trying to model, and it would still be more code than the Planet class approach.

    Thank You - jeff, Campbell, Lalit, and Stevens.



    You're quite welcome. You seem to have good attitude toward learning and toward putting in the work necessary to do so. That's always gratifying to see for those of us on this end of the discussion.
     
    Campbell Ritchie
    Marshal
    Posts: 80781
    489
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I would suggest you get as much code out of that main method as you can. In my opinion the ideal length of a main method is one statement (I find few people will recommend it be any shorter ‍) which starts the application. I suggest your sort methods and swapTwoElementsInArray methods belong in a class by themselves, which can beneficially be a utility class (search for that and you find that utility class methods are usually static).
    Agree that a Planet class might have more code to start with, but the amount of work it would take to enhance the parallel arrays to add gravitational pull or similar would be far greater. And more error-prone.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic