bubble sort does not print all

i'm trying to use bubble sort to sort and print all data in my Array list, but it only prints  the first two records sorted. how do i get it to print all the data in my list sorted without repeating.

public class Album {
    public static void main(String[] args) {
     
      List<Recording> record = new ArrayList<>();
       Recording record1 = new Recording(137481,672745103);
       record.add(record1);
       Recording record2 = new Recording(937176,68652108);
       record.add(record2);
       Recording record3 = new Recording(198459,69738107);
       record.add(record3);
       Recording record4 = new Recording(238264,69722104);
       record.add(record4);
     
       System.out.println(record.toString());
       
        System.out.println("Now sorted");
       
        BubbleSort(record);
        System.out.println(record.toString());
       
    }
    public static void BubbleSort (List<Recording> record) {
        int temp = 0;                                            

        for (int i=0; i<record.size()-1; i++) {
           
           for (int j=0; j<record.size()-1; j++) {
               if (record.get(j).getCompletedTime()>record.get(j+1).getCompletedTime()){
                     record.add(j, record.get(j + 1));
                    record.remove(j + 1);
                }
           }
        }
    }
   
}
rosy2015Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gurpsbassiCommented:
Please post your Recording class too.
0
rosy2015Author Commented:
here's the recording class


public class Recording {
   private long  recordingTime;
   private long  completedTime;
         
    public Recording( long tRecordingTime, long tCompletedTime)
    {
        this.recordingTime =tRecordingTime;
        this.completedTime = tCompletedTime;
    }

    public long getRecordingTime()
   {
        return recordingTime;  
   }
 
   public void setRecordingTime(long value)
   {
       recordingTime = value;      
   }
   
    public long getCompletedTime()
   {
        return completedTime;    
   }
 
   public void setCompletedTime(long value)
   {
       completedTime = value;    
   }
   
    @Override
     public String toString()
            //print information
    {
      Date recordingDate = new Date(recordingTime);
      Date completedTimeDate = new Date(completedTime);
      DateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
      String dateFormatted = formatter.format(recordingDate);
      String completedFormatDate = formatter.format(completedTimeDate);
     
      return "("+ "\t" + dateFormatted +  "\t" + completedFormatDate +  "\t "
              +"\n";
     
        //return "("+ "\t" + recordingTime +  "\t" + completedTime +  "\t " 
               // +"\n";
    }
}
0
CEHJCommented:
Don't go adding and removing. Use


Collections.swap(record, i, j);

Open in new window


That should be called 'records' btw and method names begin lower case in Java
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

CEHJCommented:
String dateFormatted = formatter.format(recordingDate);

Open in new window


That doesn't really make sense. How can you format a 'recording date' with just a time format?
0
rosy2015Author Commented:
i'm not concerned with the time format or the variable names, that section works fine. The question i posted was pertaining to the bubble sort. Also, i do not want to use collection i want to understand how the bubble sort algorithm works without collection.
0
CEHJCommented:
Also, i do not want to use collection

Then do the swapping yourself

Here's the corrected sorting method. I'll leave it to you to provide your own swap method:

    public static void bubbleSort(List<Recording> records) {
        for (int i = 0; i < records.size(); i++) {
            for (int j = 0; j < i; j++) {
                if (records.get(j).getCompletedTime() > records.get(i)
                                                                 .getCompletedTime()) {
                    Collections.swap(records, i, j);
                }
            } // end inner for
        } // end outer for
    }

Open in new window

0
rosy2015Author Commented:
Hi Thanks for your answer, but you haven't answered my question, i'm new to java, and i'm teaching myself, i'm stuck hence the question, Again i do not want to use collection.  How do i do the swapping so it prints all the data in the arraylist at the moment it only prints the the first two (sorted) data.  using the code below can you specifically tell me why it is not showing the rest of the data.

public static void BubbleSort (List<Recording> record) {
        int temp = 0;                                            

        for (int i=0; i<record.size()-1; i++) {
           
           for (int j=0; j<record.size()-1; j++) {
               if (record.get(j).getCompletedTime()>record.get(j+1).getCompletedTime()){
                     record.add(j, record.get(j + 1));
                    record.remove(j + 1);
                }
           }
        }
    }
0
CEHJCommented:
using the code below ...
I've already told you, you should NOT be using the code below as it involves mutating the List. Not only that, you're mutating the List while iterating it, which is nonsensical and totally wrong.

Therefore, you must start from a correct point from which you can complete your code. I've supplied that point above and leave it to you to alter whilst leaving it working correctly.
0
rosy2015Author Commented:
Thanks CEHJ but that is not what i'm looking for.
0
CEHJCommented:
... as it involves mutating the List. Not only that, you're mutating the List while iterating it, which is nonsensical and totally wrong.

To make it absolutely clear, let us substitute the words 'changing the size of' for 'mutating'. The proper implementation involves mutation, but not mutation of the size.
0
rosy2015Author Commented:
don't worry CEHJ i managed to use a different sort algorithm (SELECTION: Shown below) and it works beautifully. i previously just wanted to understand how the bubble sort worked on my List, but got no help. Although, if you Do want to help, please paste a snippet of how i can apply the bubble sort algorithm to my List (without using a collection), else, please ignore this question. Thanks.

 public static void selectionSort(List<Recording> record ) {
            if (record == null)
                  return;
               
            if (record.isEmpty()|| record.size() == 1)
                  return;

            int smallestIndex;
            int smallest;

            // for each index in the array list
            for (int curIndex = 0; curIndex < record.size(); curIndex++) {

                  // initialize variables
                  smallest = (int)record.get(curIndex).getCompletedTime();
                  smallestIndex = curIndex;

                  for (int i = curIndex + 1; i < record.size(); i++) {
                        if (smallest > record.get(i).getCompletedTime()) {
                              // update smallest
                              smallest = (int) record.get(i).getCompletedTime();
                              smallestIndex = i;
                        }
                  }

                  if (smallestIndex == curIndex)
                        ;
                  else {
                           
                        Recording temp = record.get(curIndex);
                        record.set(curIndex, record.get(smallestIndex));
                        record.set(smallestIndex, temp);
                  }
            }
      }
0
CEHJCommented:
final List l = records;
l.set(i, l.set(j, l.get(i)));

Open in new window


Substitute the above for the Collections method
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rosy2015Author Commented:
Cheers that worked! now, i have to go read up about final!, dang CEHJ it did take us a while though! :-)
0
rosy2015Author Commented:
Thanks again:-)
0
CEHJCommented:
:) (Though you should have been able to swap them [perhaps not exactly like that] with your own code after i'd supplied most of it)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.