bubble sort help.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • evilmonkey
    New Member
    • Jan 2007
    • 14

    #1

    bubble sort help.

    I am trying to sort an array by name ( that works) but I only need each sorted name to show up once, regardless of how many times it shows up in the sort. Where do I go from here?

    Code:
    public class Report {
        Rental[] rentals = new Rental[20];
        /** Creates a new instance of Report */
        public CustomerReport() {
        }
        public Rental[]sort(Rental[] rentals)
      {
            Rental[] newRentals = new Rental[rentals.length];
            for (int i = 0; i < rentals.length; i++){
                     newRentals[i] = rentals[i];
            }
            int n = rentals.length;
            for (int pass=1; pass < n; pass++){
                     for(int i=0; i< n-pass; i++){
                           int result = newRentals[i].getCustomer().getName().compareTo(newRentals[i+1].getCustomer().getName());
                                if(result > 0)
                                     {
                                       Rental temp = newRentals[i];
                                       newRentals[i] = newRentals[i+1];
                                       newRentals[i + 1] = temp;
                                       }
            } 
         } return newRentals;
       }
    public Rental[] getRentals(){
        return rentals;
      }
      }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Does the sorting method has to be a bubble sort? If not simply add all your
    array elements to a SortedSet. It will remove the duplicates for you and sort
    all the unique members. All you have to do is make your class implement
    the Comparable interface.

    kind regards,

    Jos

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by evilmonkey
      I am trying to sort an array by name ( that works) but I only need each sorted name to show up once, regardless of how many times it shows up in the sort. Where do I go from here?

      Code:
      public class Report {
      Rental[] rentals = new Rental[20];
      /** Creates a new instance of Report */
      public CustomerReport() {
      }
      public Rental[]sort(Rental[] rentals)
      {
      Rental[] newRentals = new Rental[rentals.length];
      for (int i = 0; i < rentals.length; i++){
      newRentals[i] = rentals[i];
      }
      int n = rentals.length;
      for (int pass=1; pass < n; pass++){
      for(int i=0; i< n-pass; i++){
      int result = newRentals[i].getCustomer().getName().compareTo(newRentals[i+1].getCustomer().getName());
      if(result > 0)
      {
      Rental temp = newRentals[i];
      newRentals[i] = newRentals[i+1];
      newRentals[i + 1] = temp;
      }
      } 
      } return newRentals;
      }
      public Rental[] getRentals(){
      return rentals;
      }
      }
      You can also read this tip of the week.

      Comment

      Working...