Results 1 to 20 of 24
Thread: Bubble Sort objects
- 03-25-2012, 06:56 PM #1
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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()); }
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) { } } }
Thanks
- 03-25-2012, 07:48 PM #2
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.
- 03-25-2012, 11:47 PM #3
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
- 03-25-2012, 11:52 PM #4
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.
- 03-25-2012, 11:55 PM #5
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
- 03-26-2012, 12:00 AM #6
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.
- 03-26-2012, 12:06 AM #7
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
- 03-26-2012, 12:15 AM #8
Re: Bubble Sort objects
Why are you using a Stack?
If you don't understand my response, don't ignore it, ask a question.
- 03-26-2012, 12:17 AM #9
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Bubble Sort objects
Because I am doing an analysis on data structures, So i have chosen a Stack.
- 03-26-2012, 12:20 AM #10
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.
- 03-26-2012, 12:24 AM #11
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
- 03-26-2012, 12:27 AM #12
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.
- 03-26-2012, 12:31 AM #13
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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
- 03-26-2012, 12:34 AM #14
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.
- 03-26-2012, 12:37 AM #15
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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.
- 03-26-2012, 12:44 AM #16
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.
- 03-26-2012, 12:46 AM #17
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
Re: Bubble Sort objects
Stack extends Vector which has 3 methods.
- 03-26-2012, 12:53 AM #18
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.
- 03-26-2012, 12:58 AM #19
Senior Member
- Join Date
- Nov 2011
- Posts
- 116
- Rep Power
- 0
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?
- 03-26-2012, 12:59 AM #20
Similar Threads
-
Question with bubble sort
By Metastar in forum New To JavaReplies: 22Last Post: 09-13-2010, 07:25 AM -
Bubble sort
By pineapple in forum New To JavaReplies: 3Last Post: 04-25-2009, 01:45 AM -
How to sort a list using Bubble sort algorithm
By Java Tip in forum AlgorithmsReplies: 3Last Post: 04-29-2008, 09:04 PM -
Bubble Sort in Java
By Java Tip in forum AlgorithmsReplies: 0Last Post: 04-15-2008, 08:42 PM -
need help with bubble sort
By lowpro in forum New To JavaReplies: 3Last Post: 12-17-2007, 06:27 PM
Bookmarks