| | |
How do i change this JAVA Insertion sort object to Bubble Sort object
![]() |
•
•
Join Date: Jan 2007
Posts: 5
Reputation:
Solved Threads: 0
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
////////////////////////////////////////////////////////////////
// objectSort.java
// demonstrates sorting objects (uses insertion sort)
// to run this program: C>java libmainsys
////////////////////////////////////////////////////////////////
Java Syntax (Toggle Plain Text)
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 8:19 am. Reason: add code tags
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.
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
•
•
i have try to look for sorting object using Bubble sort on the internet but couldn't , i only found insertion object
http://www.cs.princeton.edu/~ah/alg_...ubbleSort.html
*Voted best profile in the world*
http://www.cs.princeton.edu/~ah/alg_...ubbleSort.html[/QUOTE]
Nice and simple explained :cheesy:
(applets didn't run my firefox
)
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
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
•
•
Join Date: Mar 2004
Posts: 763
Reputation:
Solved Threads: 38
•
•
•
•
i have try to look for sorting object using Bubble sort on the internet but couldn't , i only found insertion object
But isn't bubble sort like the first thing they teach in school?
![]() |
Similar Threads
- Insertion Sort Problem (C++)
- Simple insertion sort program (Java)
- Quicksort/Insertion sort Hyrbid? (C)
Other Threads in the Java Forum
- Previous Thread: deploying application on JBoss
- Next Thread: LSA algorithm in java
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






