User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 402,376 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,062 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 2165 | Replies: 6
Reply
Join Date: Jan 2007
Posts: 5
Reputation: dowen is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dowen dowen is offline Offline
Newbie Poster

Help How do i change this JAVA Insertion sort object to Bubble Sort object

  #1  
Jan 28th, 2007
and when i add a new int data type my program has an error and yet when i add a new String data type it has no error


// objectSort.java
// demonstrates sorting objects (uses insertion sort)
// to run this program: C>java libmainsys
////////////////////////////////////////////////////////////////
class libary
  {
  private String booktitle;
  private String bookauthor;
  private String publisher;
  
  private int nofcop;
  //-----------------------------------------------------------
  public libary(String title, String author, String pub, int nfcp)
   {                // constructor
   booktitle = title;
   bookauthor = author;
   publisher = pub; 
  
   nofcop = nfcp;
   }
  //-----------------------------------------------------------
  public void displaylibary()
   {
   System.out.print("  Book Title: " + booktitle);
   System.out.print(", Book Author: " + bookauthor);
   System.out.print(", Book Publisher: " + publisher);
   
   System.out.println(",No Of Copies : " + nofcop);
   }
  //-----------------------------------------------------------
  public String getLast()      // get title
   { return booktitle; }
  } // end class libary
////////////////////////////////////////////////////////////////
class ArrayInOb
  {
  private libary[] nfcp;        // ref to array ypub
  private int nElems;        // number of data items
//--------------------------------------------------------------
  public ArrayInOb(int max)     // constructor
   {
   nfcp = new libary[max];        // create the array
   nElems = 0;            // no items yet
   }
//--------------------------------------------------------------
                   // put libary into array
  public void insert(String title, String author, String pub, int nofcop)
   {
   nfcp[nElems] = new libary(title, author, pub,  nofcop);
   nElems++;             // increment size
   }
//--------------------------------------------------------------
  public void display()       // displays array contents
   {
   for(int j=0; j<nElems; j++)    // for each element,
     nfcp[j].displaylibary();     // display it
   System.out.println("");
   }
//--------------------------------------------------------------
  public void insertionSort()
   {
   int in, out;
   for(out=1; out<nElems; out++) // out is dividing line
     {
     libary temp = nfcp[out];    // remove marked person
     in = out;          // start shifting at out
     while(in>0 &&        // until smaller one found,
        nfcp[in-1].getLast().compareTo(temp.getLast())>0)
      {
      nfcp[in] = nfcp[in-1];     // shift item to the right
      --in;          // go left one position
      }
     nfcp[in] = temp;        // insert marked item
     } // end for
   } // end insertionSort()
//--------------------------------------------------------------
  } // end class ArrayInOb
////////////////////////////////////////////////////////////////
class libmainsys
  {
  public static void main(String[] args)
   {
   int maxSize = 1000;       // array size
   ArrayInOb arr;         // reference to array
   arr = new ArrayInOb(maxSize); // create the array
   arr.insert("Java_how__to_program", "Patty_John", "Deitel", 201);
   arr.insert("System_Design", "Dexter_Smith", "Thomson", 202);
   arr.insert("Program_Design", "Lorraine_Paul", "About", 199);
   arr.insert("Computer_Architecture", "Paul_Andrew","Dzone", 206);
   arr.insert("Visual_Basic_How_To_ Program", "Tom_Jones", "Jeffereson_publication",  207);
   arr.insert("Information_ Management", "William_Peter", "Mcgraw_Hill", 195);
   arr.insert("Sofware_ Application", "Henry_Sam", "Pearson", 296);
   arr.insert("English", "Samantha_Julia", "James_Hill", 394);
   arr.insert("Web_Publishing", "Audrey_Cynthia", "Surg", 193);
   arr.insert("Human_Computer_Interaction", "Tony_Edward",  "Telde", 202);
   System.out.println("Before sorting:");
   arr.display();         // display items
   arr.insertionSort();      // insertion-sort them
   System.out.println("After sorting:");
   arr.display();         // display them again
   } // end main()
  } // end class libmainsys
Last edited by Ancient Dragon : Jan 28th, 2007 at 7:19 am. Reason: add code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2005
Posts: 4,711
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 309
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: How do i change this JAVA Insertion sort object to Bubble Sort object

  #2  
Jan 28th, 2007
I've not compiled or tested it but you need to define two insertion sorts.

One for sorting strings the other for integers.\
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,226
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 10
Solved Threads: 270
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: How do i change this JAVA Insertion sort object to Bubble Sort object

  #3  
Jan 28th, 2007
There is plenty of examples for sorting on internet. Did you looked at them? Please do so, just quick google search brought this
Why I'm telling this to you? Because right now we can only recomend that you follow theory in order to create some code. Then, if you have any problem with this fresh developed code we will advice.
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Join Date: Jan 2007
Posts: 5
Reputation: dowen is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
dowen dowen is offline Offline
Newbie Poster

Re: How do i change this JAVA Insertion sort object to Bubble Sort object

  #4  
Jan 28th, 2007
i have try to look for sorting object using Bubble sort on the internet but couldn't , i only found insertion object
Reply With Quote  
Join Date: Aug 2005
Posts: 4,711
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 309
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: How do i change this JAVA Insertion sort object to Bubble Sort object

  #5  
Jan 28th, 2007
Originally Posted by dowen View Post
i have try to look for sorting object using Bubble sort on the internet but couldn't , i only found insertion object


Bubble sort is even easier.

http://www.cs.princeton.edu/~ah/alg_...ubbleSort.html
Member of: F-ugly code club

Join today don't delay!
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,226
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 10
Solved Threads: 270
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: How do i change this JAVA Insertion sort object to Bubble Sort object

  #6  
Jan 28th, 2007
http://www.cs.princeton.edu/~ah/alg_...ubbleSort.html[/quote]

Nice and simple explained :cheesy:

(applets didn't run my firefox )
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Join Date: Mar 2004
Posts: 717
Reputation: Phaelax is on a distinguished road 
Rep Power: 6
Solved Threads: 29
Phaelax Phaelax is offline Offline
Master Poster

Re: How do i change this JAVA Insertion sort object to Bubble Sort object

  #7  
Jan 30th, 2007
Originally Posted by dowen View Post
i have try to look for sorting object using Bubble sort on the internet but couldn't , i only found insertion object


You didn't look too hard then, I searched "bubble sort" on google and oddly enough, the whole page has results for bubble sort, not insertion. First result is even a wikipedia page and display code.
But isn't bubble sort like the first thing they teach in school?
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 12:50 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC