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

Reply

Join Date: Jan 2007
Posts: 5
Reputation: dowen is an unknown quantity at this point 
Solved Threads: 0
dowen dowen is offline Offline
Newbie Poster

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

 
0
  #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
////////////////////////////////////////////////////////////////
  1. class libary
  2. {
  3. private String booktitle;
  4. private String bookauthor;
  5. private String publisher;
  6.  
  7. private int nofcop;
  8. //-----------------------------------------------------------
  9. public libary(String title, String author, String pub, int nfcp)
  10. { // constructor
  11. booktitle = title;
  12. bookauthor = author;
  13. publisher = pub;
  14.  
  15. nofcop = nfcp;
  16. }
  17. //-----------------------------------------------------------
  18. public void displaylibary()
  19. {
  20. System.out.print(" Book Title: " + booktitle);
  21. System.out.print(", Book Author: " + bookauthor);
  22. System.out.print(", Book Publisher: " + publisher);
  23.  
  24. System.out.println(",No Of Copies : " + nofcop);
  25. }
  26. //-----------------------------------------------------------
  27. public String getLast() // get title
  28. { return booktitle; }
  29. } // end class libary
  30. ////////////////////////////////////////////////////////////////
  31. class ArrayInOb
  32. {
  33. private libary[] nfcp; // ref to array ypub
  34. private int nElems; // number of data items
  35. //--------------------------------------------------------------
  36. public ArrayInOb(int max) // constructor
  37. {
  38. nfcp = new libary[max]; // create the array
  39. nElems = 0; // no items yet
  40. }
  41. //--------------------------------------------------------------
  42. // put libary into array
  43. public void insert(String title, String author, String pub, int nofcop)
  44. {
  45. nfcp[nElems] = new libary(title, author, pub, nofcop);
  46. nElems++; // increment size
  47. }
  48. //--------------------------------------------------------------
  49. public void display() // displays array contents
  50. {
  51. for(int j=0; j<nElems; j++) // for each element,
  52. nfcp[j].displaylibary(); // display it
  53. System.out.println("");
  54. }
  55. //--------------------------------------------------------------
  56. public void insertionSort()
  57. {
  58. int in, out;
  59. for(out=1; out<nElems; out++) // out is dividing line
  60. {
  61. libary temp = nfcp[out]; // remove marked person
  62. in = out; // start shifting at out
  63. while(in>0 && // until smaller one found,
  64. nfcp[in-1].getLast().compareTo(temp.getLast())>0)
  65. {
  66. nfcp[in] = nfcp[in-1]; // shift item to the right
  67. --in; // go left one position
  68. }
  69. nfcp[in] = temp; // insert marked item
  70. } // end for
  71. } // end insertionSort()
  72. //--------------------------------------------------------------
  73. } // end class ArrayInOb
  74. ////////////////////////////////////////////////////////////////
  75. class libmainsys
  76. {
  77. public static void main(String[] args)
  78. {
  79. int maxSize = 1000; // array size
  80. ArrayInOb arr; // reference to array
  81. arr = new ArrayInOb(maxSize); // create the array
  82. arr.insert("Java_how__to_program", "Patty_John", "Deitel", 201);
  83. arr.insert("System_Design", "Dexter_Smith", "Thomson", 202);
  84. arr.insert("Program_Design", "Lorraine_Paul", "About", 199);
  85. arr.insert("Computer_Architecture", "Paul_Andrew","Dzone", 206);
  86. arr.insert("Visual_Basic_How_To_ Program", "Tom_Jones", "Jeffereson_publication", 207);
  87. arr.insert("Information_ Management", "William_Peter", "Mcgraw_Hill", 195);
  88. arr.insert("Sofware_ Application", "Henry_Sam", "Pearson", 296);
  89. arr.insert("English", "Samantha_Julia", "James_Hill", 394);
  90. arr.insert("Web_Publishing", "Audrey_Cynthia", "Surg", 193);
  91. arr.insert("Human_Computer_Interaction", "Tony_Edward", "Telde", 202);
  92. System.out.println("Before sorting:");
  93. arr.display(); // display items
  94. arr.insertionSort(); // insertion-sort them
  95. System.out.println("After sorting:");
  96. arr.display(); // display them again
  97. } // end main()
  98. } // end class libmainsys
Last edited by Ancient Dragon; Jan 28th, 2007 at 8:19 am. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

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

 
0
  #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.\
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,185
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 482
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

 
0
  #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)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 5
Reputation: dowen is an unknown quantity at this point 
Solved Threads: 0
dowen dowen is offline Offline
Newbie Poster

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

 
0
  #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 Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

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

 
0
  #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
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,185
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 482
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

 
0
  #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)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 763
Reputation: Phaelax is on a distinguished road 
Solved Threads: 38
Phaelax Phaelax is offline Offline
Master Poster

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

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC