Page 1 of 2 12 LastLast
Results 1 to 20 of 24
  1. #1
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Bubble Sort objects

    Hi,

    I am trying to sort out a Stack of Dvd objects, I have a Dvd class which implements Comparator<Dvd>. The following code snippet below is the method that overrides the compare() method:

    Java Code:
    public int compare(Dvd dvd1, Dvd dvd2)
    	{
    		return dvd1.getTitle().compareTo(dvd2.getTitle());
    	}
    In another class I have a main method which tests this statement which I am also trying to implement it into a bubble sort method:

    Java Code:
    for(int x=1;x<10;x++)
    		{
    			for(int y=0;y<10-x;y++)
    			{
    				if(Dvd.compare(dvds(y), dvds(y+1))>0)
    				{
    				}
    			}
    		}
    Im not going to go any further with the sorting code until I have the if statement correct, which I have called the overrided compare method in Dvd to compare two objects which is running in two for loops to compare all Dvds in the Stack dvds. ?
    Thanks

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,333
    Rep Power
    25

    Default Re: Bubble Sort objects

    Does the dvds() method return a Dvd object from some collection?

    Add some println statements to the code to print out the values being compared and the value returned by the compare statement to see if the code is doing what you want.

    The compare() method (Dvd.compare) is coded like it was a static method: Classname.methodname()
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Bubble Sort objects

    Sorry I never mentioned that the if statement compiles. It doesn't. I dont have a dvds() method. The dvds is a Stack of dvd object, where I am trying to compare dvd elements in dvds by indexing them? This obviously a mistake.
    Thanks

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,333
    Rep Power
    25

    Default Re: Bubble Sort objects

    With a Stack you can only look at the top element. What is underneath is hidden.
    If you want to sort the objects, put them into another type of container. Why use a Stack?
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Bubble Sort objects

    Yes I understand that, but can you not put stack elements into a collection, and then compare the elements that way? And then use the comparing method on the bubble sort?
    I chose Stack because it the data structure I am dealing with for a project.
    Thanks

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,333
    Rep Power
    25

    Default Re: Bubble Sort objects

    Why do you want a Stack?
    Yes you could take the elements out of the Stack and put them into a collection.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Bubble Sort objects

    Well I never knew I'd come into problems like these I guess, but I'm far into trying to accomplish this, rather than to pick a different data structure.
    What would be the better option would you recommend; Taking the elements from the Stack into a collection, or into String array of elements? In terms of comparing elements?
    Thanks

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,333
    Rep Power
    25

    Default Re: Bubble Sort objects

    Why are you using a Stack?
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Bubble Sort objects

    Because I am doing an analysis on data structures, So i have chosen a Stack.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,333
    Rep Power
    25

    Default Re: Bubble Sort objects

    Many data structures are for a specific purpose. Have you chosen one suitable for your purpose?

    It sounds like you are trying to put a square peg into a round hole.
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Bubble Sort objects

    Yes, I am creating a logical DVD rack so I can push DVD's and pop them off the stack on top. And then I am trying to be able to sort the elements which have being pushed onto the stack. I am not actually accessing any data within the stack, only trying to sort the data.
    Thanks

  12. #12
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,333
    Rep Power
    25

    Default Re: Bubble Sort objects

    Then the order that the items were put in the stack is not important.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Bubble Sort objects

    Its not important in the real sense, but in the project for my analysis is important. All I am asking is what would be the recommended way of storing the stack elements into; An array or a Collection?

    As you did say that you cannot sort elements in a Stack by itself?
    Thanks

  14. #14
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,333
    Rep Power
    25

    Default Re: Bubble Sort objects

    Look at the API doc for the Stack class and see if it or the class it extends has any methods that you could use.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Bubble Sort objects

    Don't worry, I tried looking not only just the API, but over the net. But thanks for your time and effort.

  16. #16
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,333
    Rep Power
    25

    Default Re: Bubble Sort objects

    I don't think you have looked at what I suggested. What class does Stack extend?
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Bubble Sort objects

    Stack extends Vector which has 3 methods.

  18. #18
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,333
    Rep Power
    25

    Default Re: Bubble Sort objects

    Try counting again. There are lots more than 3 methods in the Vector class.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    dougie1809 is offline Senior Member
    Join Date
    Nov 2011
    Posts
    116
    Rep Power
    0

    Default Re: Bubble Sort objects

    My bad, I was looking at a complete different section of the page. Anyway, yes I see there are alot of methods, but is there a method that I can compare one object to the other?

  20. #20
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,333
    Rep Power
    25

    Default Re: Bubble Sort objects

    there a method that I can compare one object to the other?
    Did any of them look likely to do that?
    If you don't understand my response, don't ignore it, ask a question.

Page 1 of 2 12 LastLast

Similar Threads

  1. Question with bubble sort
    By Metastar in forum New To Java
    Replies: 22
    Last Post: 09-13-2010, 06:25 AM
  2. Bubble sort
    By pineapple in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 12:45 AM
  3. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 08:04 PM
  4. Bubble Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 07:42 PM
  5. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 05:27 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •