PDA

View Full Version : sorting in java


pinkop
12-13-2006, 04:25 AM
hello i have been working on this for a little while now and i thought i had it but i somehow got everything mixed up and changed some stuff :confused:

ok what i needed it to do was sort an array of runner objects by the place they finished in a race.
my runner class is supposed to implement the Comparable interface, have equals and compareTo methods
i am trying to use insertion sorting to do this
diggy is my driver class
public class diggy
{
//-----------------------------------------------------------------
// Creates an array of Runner objects, sorts them, then prints
// them.
//-----------------------------------------------------------------
public static void main (String[] args)
{
Runner[] people = new Runner[10];

people[0] = new Runner ("Jack Sprat",10);
people[1] = new Runner ("Cecil Litwack",8 );
people[2] = new Runner ("Dina Might", 3);
people[3] = new Runner ("Seymour Butts",4 );
people[4] = new Runner ("Ima Reject", 1);
people[5] = new Runner ("Ivan Earache",9 );
people[6] = new Runner ("Ace Ventura",6 );
people[7] = new Runner ("Joan O'farc",7 );
people[8] = new Runner ("Jack O'Lantern", 5);
people[9] = new Runner ("Jim Nasium", 2);


Sorting.insertionSort(people[].getPlace());

for (Runner friend : people)
System.out.println (friend);
}
}
i really need help with the compareto and equals methods
public class Runner implements Comparable
{
private String Name;
int place;

//-----------------------------------------------------------------
// Constructor: Sets up this contact with the specified data.
//-----------------------------------------------------------------
public Runner (String first, int place)
{
Name = first;
this.place=place;
}

//-----------------------------------------------------------------
// Returns a description of this contact as a string.
//-----------------------------------------------------------------
public String toString ()
{
return Name + "\t" + place;
}

//-----------------------------------------------------------------
// Returns a description of this contact as a string.
//-----------------------------------------------------------------
public boolean equals (Object other)
{
return ();
}

//-----------------------------------------------------------------
// Uses place to determine ordering.
//-----------------------------------------------------------------
public int compareTo (Object other)
{
int result=0;

int placer = ((Runner)other).getPlace();


if (place>placer)
result =1;
if (place<placer)
result = -1;

return result;
}

//-----------------------------------------------------------------
// First name accessor.
//-----------------------------------------------------------------
public String getName ()
{
return Name;
}


//-----------------------------------------------------------------
// place accessor.
//-----------------------------------------------------------------
public int getPlace()
{
return place;
}
}
would i need to change the insertion sort for this to work right? if so what would that change be
public class Sorting
{
//-----------------------------------------------------------------
// Sorts the specified array of objects using the selection
// sort algorithm.
//-----------------------------------------------------------------
public static void selectionSort (Comparable[] list)
{
int min;
Comparable temp;

for (int index = 0; index < list.length-1; index++)
{
min = index;
for (int scan = index+1; scan < list.length; scan++)
if (list[scan].compareTo(list[min]) < 0)
min = scan;

// Swap the values
temp = list[min];
list[min] = list[index];
list[index] = temp;
}
}

//-----------------------------------------------------------------
// Sorts the specified array of objects using the insertion
// sort algorithm.
//-----------------------------------------------------------------
public static void insertionSort (Comparable[] list)
{
for (int index = 1; index < list.length; index++)
{
Comparable key = list[index];
int position = index;

// Shift larger values to the right
while (position > 0 && key.compareTo(list[position-1]) < 0)
{
list[position] = list[position-1];
position--;
}

list[position] = key;
}
}
}


thank you so much for any help
(this is homework and normally i wouldnt ask for help but I'm stuck ><)

Mink
12-13-2006, 06:23 AM
First of all...this code won't even compile

Sorting.insertionSort(people[].getPlace());

huh? What are you trying to do here? Even if this was correct syntax, getPlace() returns an int, and insertionSort takes in an array.

private String Name;

Aside from it being against the general naming conventions, you cannot do this. Name is going to have to be lowercased because Name is an interface is the javax.naming package. Normally, only classes are capitalized, and variable names are lowercased with each word after the first capital

Like this:

int numOfPeople; //int is a primitive data-type
String myName; //String is a class (this is a reference data-type)


Other than that your code looks good. insertionSort doesn;t need to be changed. And your compareTo function is good.

One thing I would change though is this:

public int compareTo (Object other)

to

public int compareTo (Runner other)


That way I don't have to unpack the object:

int placer = other.getPlace();


And

public boolean equals (Runner other)


Also, you have place set as a public variable in Runner. As a mark of good programming, that should also be a private variable (without it being private, the getPlace method is useless, since people could access it directly and change it)