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.

Results 1 to 8 of 8

Thread: Java Bubble Sort Issue

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Bubble Sort Issue

    Thanks to everyone who helped!
    Last edited by Dave Bowman; June 19th, 2014 at 02:09 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,136
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Java Bubble Sort Issue

    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.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Bubble Sort Issue

    System.out.println(bookList);

    Gives me.

    [Book@272d7a10, Book@1aa8c488, Book@3dfeca64, Book@22998b08, Book@e76cbf7]

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,136
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Java Bubble Sort Issue

    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.

  5. #5
    Junior Member
    Join Date
    Jun 2014
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Bubble Sort Issue

    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.

  6. #6
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Java Bubble Sort Issue

    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

  7. The Following User Says Thank You to shahcat For This Useful Post:

    Abhilash (June 19th, 2014)

  8. #7
    Member Abhilash's Avatar
    Join Date
    Jun 2014
    Location
    Kolkata, West Bengal, INDIA
    Posts
    108
    My Mood
    Busy
    Thanks
    5
    Thanked 10 Times in 10 Posts

    Default Re: Java Bubble Sort Issue

    That's right. Efficiency matters.

  9. #8
    Member
    Join Date
    Apr 2014
    Posts
    135
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Java Bubble Sort Issue

    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.

Similar Threads

  1. How do you sort an array? [using bubble sort]
    By namenamename in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 27th, 2013, 02:45 PM
  2. Java program for bubble sort
    By shvn2693 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 3rd, 2013, 10:04 PM
  3. Bubble Sort Help
    By asundar in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 24th, 2012, 04:14 PM
  4. Bubble Sort help
    By baueml01 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: June 5th, 2011, 08:47 PM
  5. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM

Tags for this Thread