Forums Register Login

method overloading in sort program

+Pie Number of slices to send: Send
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;
}
}
}
+Pie Number of slices to send: Send
When you overload a function you must have a diffrent argumentlist, or else can't the compiler deside witch function to use.
ex. xxx base(int[] xxx )
xxx base(long[] xxx)
thats what the compiler message
"int [] already in use" means..
excuse me for my bad english.
hope this helps !!

[This message has been edited by Rikard Qvarforth (edited July 16, 2001).]
+Pie Number of slices to send: Send

I would suggest having another look at the error message. It looks as if it is really compaining about the method selectionSort() being duplicated. (Both selectionSort() methods have the same int[] parameters.)
The overriding of the method insertionSort() is valid. So, you look as if you are on the right track, and just need to fix the selectionSort() as you have done insertionSort(). It looks as if you have simply been thrown off track by the error message.
Weeds: because mother nature refuses to be your personal bitch. But this tiny ad is willing:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 2219 times.
Similar Threads
help with sort code
im trying to sort my program with insertionSort and selectionSort
Does arraycopy create aliases?
cannot resolve a symbol with a method.
does this sort an array of integers?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:10:07.