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 5 of 5

Thread: What Algorithm Would Work Best for me?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    24
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question What Algorithm Would Work Best for me?

    Hello, I need to modify this movies program so that way the DVDs will be sorted by title at all times. I've gone over several algorithms like bubble sort and binary sort, but I am wondering which algorithm would be best since for this program the performance of the code will be scrutinized, so I need to produce the best modification(s) to achieve the requirements. If you have any suggestions and if you could give me some insight on how I would go about inducing/using the algorithm I would appreciate it.
    Thank you for your help. The 3 classes of this program are pasted below. (Movies -> DVDCollection -> DVD)


    public class Movies
    {
       //  Creates a DVDCollection object and adds some DVDs to it. Prints
       //  reports on the status of the collection.
       public static void main (String[] args)
       {
          DVDCollection movies = new DVDCollection();
     
          movies.addDVD ("The Godfather", "Francis Ford Coppala", 1972, 24.95, true);
          movies.addDVD ("District 9", "Neill Blomkamp", 2009, 19.95, false);
          movies.addDVD ("Iron Man", "Jon Favreau", 2008, 15.95, false);
          movies.addDVD ("All About Eve", "Joseph Mankiewicz", 1950, 17.50, false);
          movies.addDVD ("The Matrix", "Andy & Lana Wachowski", 1999, 19.95, true);
     
          System.out.println (movies);
     
          movies.addDVD ("Iron Man 2", "Jon Favreau", 2010, 22.99, false);
          movies.addDVD ("Casablanca", "Michael Curtiz", 1942, 19.95, false);
     
          System.out.println (movies);
       }
    }



    import java.text.NumberFormat;
    import java.util.Collections;
    public class DVDCollection
    {
       private DVD[] collection;
       private int count;
       private double totalCost;
     
       //-----------------------------------------------------------------
       //  Constructor: Creates an initially empty collection.
       //-----------------------------------------------------------------
       public DVDCollection ()
       {
          collection = new DVD[100];
          count = 0;
          totalCost = 0.0;
       }
     
       //  Adds a DVD to the collection, increasing the size of the
       //  collection array if necessary.
     
       public void addDVD (String title, String director, int year, double cost, boolean bluRay)
       {
          if (count == collection.length)
             {
              increaseSize();
              }
     
          collection[count] = new DVD (title, director, year, cost, bluRay);
          totalCost += cost;
          count++;
       }
     
       //  Returns a report describing the DVD collection.
       public String toString()
       {
          NumberFormat fmt = NumberFormat.getCurrencyInstance();
     
          String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
          report += "My DVD Collection\n\n";
     
          report += "Number of DVDs: " + count + "\n";
          report += "Total cost: " + fmt.format(totalCost) + "\n";
          report += "Average cost: " + fmt.format(totalCost/count);
     
          report += "\n\nDVD List:\n\n";
     
          for (int dvd = 0; dvd < count; dvd++)
          {
              report += collection[dvd].toString() + "\n";
          }
     
          return report;
       }
       //  Increases the capacity of the collection by creating a
       //  larger array and copying the existing collection into it.
     
       private void increaseSize ()
       {
          DVD[] temp = new DVD[collection.length * 2];
     
          for (int dvd = 0; dvd < collection.length; dvd++)
           {
             temp[dvd] = collection[dvd];
            }
     
          collection = temp;
       }
    }



    import java.text.NumberFormat;
     
    public class DVD
    {
       private String title, director;
       private int year;
       private double cost;
       private boolean bluRay;
     
       //  Creates a new DVD with the specified information.
     
       public DVD (String title, String director, int year, double cost, boolean bluRay)
       {
          this.title = title;
          this.director = director;
          this.year = year;
          this.cost = cost;
          this.bluRay = bluRay;
       }
     
     
       public String getTitle()
       {
           return title;
       }
       //  Returns a string description of this DVD.
       public String toString()
       {
          NumberFormat fmt = NumberFormat.getCurrencyInstance();
     
          String description;
     
          description = fmt.format(cost) + "\t" + year + "\t";
          description += title + "\t" + director;
     
          if (bluRay)
          {
             description += "\t" + "Blu-Ray";
          }
     
          return description;
       }
     
    }
    Last edited by helloworld922; February 20th, 2012 at 09:41 AM.


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: What Algorithm Would Work Best for me?

    Why don't you use collection and get benefit of already provided sorted algorithms?
    And if you want to sort by the title, it's upon you to either sort ascending or descending. Read this and find out which one will suit your requirements. Sorting algorithm - Wikipedia, the free encyclopedia

  3. The Following User Says Thank You to Mr.777 For This Useful Post:

    userct (February 21st, 2012)

  4. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: What Algorithm Would Work Best for me?

    The "best" algorithm/data structure to use depends on the usage patterns. Is the list mostly sorted already? Would it be resorted in a different order? Are you mostly adding, removing, or searching for items?

  5. #4
    Junior Member
    Join Date
    Feb 2012
    Posts
    24
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What Algorithm Would Work Best for me?

    Thank you for your responses.

    This is what it prints right now without any alterations. It is unsorted and basically all over the place.


    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    My DVD Collection

    Number of DVDs: 5
    Total cost: $98.30
    Average cost: $19.66

    DVD List:

    $24.95 1972 The Godfather Francis Ford Coppala Blu-Ray
    $19.95 2009 District 9 Neill Blomkamp
    $15.95 2008 Iron Man Jon Favreau
    $17.50 1950 All About Eve Joseph Mankiewicz
    $19.95 1999 The Matrix Andy & Lana Wachowski Blu-Ray

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    My DVD Collection

    Number of DVDs: 7
    Total cost: $141.24
    Average cost: $20.18

    DVD List:

    $24.95 1972 The Godfather Francis Ford Coppala Blu-Ray
    $19.95 2009 District 9 Neill Blomkamp
    $15.95 2008 Iron Man Jon Favreau
    $17.50 1950 All About Eve Joseph Mankiewicz
    $19.95 1999 The Matrix Andy & Lana Wachowski Blu-Ray
    $22.99 2010 Iron Man 2 Jon Favreau
    $19.95 1942 Casablanca Michael Curtiz
    Last edited by userct; February 20th, 2012 at 04:43 PM.

  6. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    24
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What Algorithm Would Work Best for me?

    I know I'm probably calling the array wrong but would this method help me to sort:
    I never told it to sort the String "title" so I must have to call that.


    public static void sort(DVD [] collection, int counter)
    {

    int a,b;
    String temp;
    int sortStrings = counter - 1;
    for (a = 0; a < sortStrings; ++a)
    for (b = 0; b < sortStrings; ++b)
    if(collection[b].compareTo(collection[b + 1]) >0)
    {
    temp = collection[b];
    collection[b] = collection[b + 1];
    collection[b + 1] = temp;
    }

    }
    Last edited by userct; February 20th, 2012 at 06:14 PM.

Similar Threads

  1. MWM Algorithm
    By andreas90 in forum Algorithms & Recursion
    Replies: 5
    Last Post: January 11th, 2012, 03:43 AM
  2. CRC algorithm
    By aldykid in forum Algorithms & Recursion
    Replies: 1
    Last Post: August 7th, 2011, 10:50 AM
  3. all i need is algorithm
    By coder.freak in forum Paid Java Projects
    Replies: 3
    Last Post: April 6th, 2011, 11:11 AM
  4. [SOLVED] Algorithm Help
    By aussiemcgr in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2010, 04:12 PM
  5. algorithm
    By AmmrO in forum Algorithms & Recursion
    Replies: 13
    Last Post: September 24th, 2009, 09:18 PM