posted 23 years ago
I need to add an overloaded method to this Sort program. The overloaded method is to be used to sort objects. I'm not quite sure how to write the overloaded method. This is what I have so far. It does not compile. It say int[] is already defined.
-----------------------------------------------------------------
public class Sorts
{
//-------------------------------------------------------------
// Sorts the specified array of integers using the selection
//sort algorigthm.
//-------------------------------------------------------------
public static void selectionSort (int[] numbers)
{
int min, temp;
for (int index = 0; index < numbers.length-1; index++)
{
min = index;
for (int scan = index+1; scan < numbers.length; scan++)
if (numbers[scan] < numbers[min])
min = scan;
//Swap the values
temp = numbers[min];
numbers[min] = numbers[index];
numbers[index] = temp;
}
}
//-------------------------------------------------------------
// Sorts the specified array of objects using the selection
// sort algorithm.
//-------------------------------------------------------------
public static void selectionSort (int[] objects)
{
int min, temp;
for (int index = 0; index < objects.length-1; index++)
{
min = index;
for (int scan = index+1; scan < objects.length; scan++)
if (objects[scan] < objects[min])
min = scan;
//Swap the values
temp = objects[min];
objects[min] = objects[index];
objects[index] = temp;
}
}
//-------------------------------------------------------------
// Sorts the specified array of integers using the insertion
// sort algorithm
//------------------------------------------------------------- public static void insertionSort (int[] numbers)
{
for (int index = 1; index < numbers.length; index++)
{
int key = numbers[index];
int position = index;
//Shift larger values to the right
while (position > 0 && numbers[position-1] > key)
{
numbers[position] = numbers[position-1];
position--;
}
numbers[position] = key;
}
}
//-------------------------------------------------------------
//Sorts the specified array of objects using the insertion
//algorithm.
//-------------------------------------------------------------
public static void insertionSort (Comparable[] objects)
{
for (int index = 1; index < objects.length; index++)
{
Comparable key = objects[index];
int position = index;
while (position > 0 && objects[position-1].compareTo(key) > 0)
{
objects[position] = objects[position-1];
position--;
}
objects[position] = key;
}
}
}