Thanks to everyone who helped!
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Thanks to everyone who helped!
Last edited by Dave Bowman; June 19th, 2014 at 02:09 PM.
How have you tried debugging the code?
Print out the contents of the arraylist before the sort and after the sort using a println() with the arraylist name:
System.out.println("before booklist="+booklist);
to see what is there before and after the sort. Change "before" to "after" and to "inside".
And also print out the array inside of the if where the swap is done.
If you don't understand my answer, don't ignore it, ask a question.
System.out.println(bookList);
Gives me.
[Book@272d7a10, Book@1aa8c488, Book@3dfeca64, Book@22998b08, Book@e76cbf7]
Those Strings are the values returned by the class's default toString() method.
Add a toString() method to the Book class that returns a String with some of the values held in the Book class so you can identify what book it is.
If you don't understand my answer, don't ignore it, ask a question.
Though I still have one question? Is this considered a "bubble sort" or just a way to sort? I understand the difference, I just want to make sure I am fulfilling the requirements of the assignment.
EDIT: My major is networking, Java is definitely not my strongsuit.
Last edited by Dave Bowman; June 19th, 2014 at 02:09 PM.
It is a bubble sort
To improve efficiency change line to
for (int in = 0; in < bookList.size() - 1 -out; in++)
The logic behind this is that each time you run through the outer loop you guarantee that the next to last item is in the correct place
eg if you have 5 elements
after the first run through the fifth element is correct
after the second run through the fourth element is also correct
For small amounts of data this is not a noticeable efficiency; for large amounts it is significant
Abhilash (June 19th, 2014)
That's right. Efficiency matters.
It is important to know how bubble sort works, the same as quick, and merge. However java does it for us and I honestly do not know which one it uses. I would assume merge because it is the most efficient in my opinion.