aspose file tools*
The moose likes Java in General and the fly likes help me to change this java Insertion sort object to Bubble sort object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "help me to change this java Insertion sort object to Bubble sort object" Watch "help me to change this java Insertion sort object to Bubble sort object" New topic
Author

help me to change this java Insertion sort object to Bubble sort object

dexter michael
Greenhorn

Joined: Jan 29, 2007
Posts: 1
i try to look for sorting object using Bubble sort but couldn't find it



// 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
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2495
    
    8

This is a question that popped up at Yahoo Answers.
You could gain 10 points with it.
10 points if you manage to change this Insertion sort to Bubble sort?

The answer there was:


That's not a question - that's a request to completely rewrite a chunk of code from scratch. You're unlikely to get any response here... especially as this is so obviously a piece of homework...

p.s. Someone always gets 10 points, even if you don't offer them up as some kind of "special reward". This could be considered "point-gaming".



Regards, Jan


OCUP UML fundamental and ITIL foundation
youtube channel
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: help me to change this java Insertion sort object to Bubble sort object