// objectSort.java
// demonstrates sorting objects (uses insertion sort)
// to run this program: C%26gt;java ObjectSortApp
//////////////////////////////////////...
class Person
{
private String lastName;
private String firstName;
private int age;
//--------------------------------------...
public Person(String last, String first, int a)
{ // constructor
lastName = last;
firstName = first;
age = a;
}
//--------------------------------------...
public void displayPerson()
{
System.out.print(" Last name: " + lastName);
System.out.print(", First name: " + firstName);
System.out.println(", Age: " + age);
}
//--------------------------------------...
public String getLast() // get last name
{ return lastName; }
} // end class Person
//////////////////////////////////////...
10 points if you manage to change this Insertion sort to bubble sort?
Bubble sort really shouldn't be all that hard...have you done any research on this besides asking on Yahoo Answers?
Here's my suggestion. You're going to need to adapt it to your specific program. Disclaimer: I wrote this without any testing, much less debugging.
//====================
public void bubbleSort(Person [] personArray) {
boolean sortsPerformed = true;
while (sortsPerformed == true)
{
for (int i = 0; i %26lt; getArraySize(personArray) ; i++)
{
sortsPerformed = false;
if ( personArray[i].getLastName() %26gt; personArray[i+1].getLastName() )
{
swap (personArray, i, i+1)
sortsPerformed = true;
}
}
}
}
//===================
I leave it to you to write the appropriate functions or modify the code as necessary to make it fit into your program. Good luck!
Reply:Before sorting:
Last name: Evans, First name: Patty, Age: 24
Last name: Smith, First name: Doc, Age: 59
Last name: Smith, First name: Lorraine, Age: 37
Last name: Smith, First name: Paul, Age: 37
Last name: Yee, First name: Tom, Age: 43
Last name: Hashimoto, First name: Sato, Age: 21
Last name: Stimson, First name: Henry, Age: 29
Last name: Velasquez, First name: Jose, Age: 72
Last name: Vang, First name: Minh, Age: 22
Last name: Creswell, First name: Lucinda, Age: 18
After sorting:
Last name: Creswell, First name: Lucinda, Age: 18
Last name: Evans, First name: Patty, Age: 24
Last name: Hashimoto, First name: Sato, Age: 21
Last name: Smith, First name: Doc, Age: 59
Last name: Smith, First name: Lorraine, Age: 37
Last name: Smith, First name: Paul, Age: 37
Last name: Stimson, First name: Henry, Age: 29
Last name: Vang, First name: Minh, Age: 22
Last name: Velasquez, First name: Jose, Age: 72
Last name: Yee, First name: Tom, Age: 43Lexicographical Comparisons
Reply:you know, i didn't have the yahoo answers for a resource when i was taking programing classes. nor did i stoop to attempting to get other people to do my homework. i am sure that if you apply your professors lessons that you will find the solution.
Reply:What in the word Is this stuff I do not under-stand this it is very weird
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment