wafflehammer
Fully Optimized
- Messages
- 2,502
- Location
- Logan, WV
Uhm k. I've been tasked with this(minus everything that has to do with merge sorting..they told us to leave that out):
But I pretty much have no clue on how to do any of it really :|
Well okay..so I do
I know
Is the code to get select and insertion sorts to work
and I know
Is the code for a template binary search. But it's also for an array of "list' or something like that..which I don't understand
But beyond that I have no clue how any of it goes together, where i declare stuff, how do I make the searches template searches, etc :| I can do this stuff separately. But when it comes to making a template class and doing it all together I just go blank :\
Any help/pointers/toss in the right direction is appreciated.
EDIT: and I know that ArrayUtil.selectSort(arraynameinhere); and ArrayUtil.insertionSort(arraynameinhere); are what I'd use in the test class to do the sorting. But still everything else is bleh...dunno when/where I'm declaring the array/variables...blah blah blah totally lost
Code:
Templated Array Utilities
ï‚· Write a class called ArrayUtil with the following signature
public class ArrayUtil<E extends Comparable>. Note that this
makes ArrayUtil a template class.
 ArrayUtil should have one member variable – a templated Array called a. The
declaration of a is the same as in the selectionSort example shown in class and
in your lab implementation of mergeSort.
ï‚· ArrayUtil should have the following methods that are written to work with objects
from any class
1. mergeSort
2. insertionSort
3. findMax – this method finds and returns the largest element in the array as
determined by the compartTo method
4. findMin – this method finds and returns the smallest element in the array as
determined by the compareTo method.
5. search – the signature for this method should be
int search(E toFind).
The method should return the index of the element toFind in the array if it
exists and â€1 if toFind is not in the array.
6. contains – the signature for this method should be
boolean contains(E toFind)
It should return true if the element toFind is in the list and false otherwise.
Note that an array becomes permanently attached to an object of the class ArrayUtil. We can
perform any of the 6 operations above on this array. However, if we wish to sort/search
another array, we must declare a new instance of ArrayUtil.
ï‚· Test your class with a test program that creates two arrays. One should be an array of
Double, the second should be an array of String as shown below:
o {58.7, 67.3, 11.4, â€9.0, 21.7, 99.4, 200.0}
o {“Donaldâ€, “Daisyâ€, “Hueyâ€, “Deweyâ€, “Louieâ€, “Mickeyâ€, “Minnieâ€, “Mortimerâ€}
ï‚· Print each array in its original order (across the page!)
ï‚· For each array create an instance of ArrayUtil and
o Sort the first array by insetionSort
o Sort the second array by mergeSort
o Print the sorted arrays (across the page)
o Find and print the maximum and minimum value in each array
But I pretty much have no clue on how to do any of it really :|
Well okay..so I do
I know
Code:
public static void selectSort(int[] a)
{
for(int i=0; i< a.length -1; i++)
{
int minValue=a[i];
int minIndex = i;
for(int k=+1; k<a.length; k++)
if(a[k]<a[minIndex])
minIndex=k;
int tmp=a[i];
a[i]=a[minIndex];
a[minIndex]=tmp;
}
}
public static void insertionSort(int[] a)
{
for(int j=1; j<a.length; j++)
{
int i=j-1;
int temp = a[j];
while(i>=0 && temp < a[i])
{
a[i+1] = a[i];
i--;
}
a[i+1]=temp;
}
}
Is the code to get select and insertion sorts to work
and I know
Code:
public int mybinarySearch(E toFind)
{
int low = 0;
int high = list.length-1;
while(low <= high)
{
int mid = (low+high)/2;
if(list[mid].compareTo(toFind) ==0)
return mid;
if(list[mid].compareTo(toFind) <0)
low = mid+1;
else
high = mid-1;
}
return -1;
}
Is the code for a template binary search. But it's also for an array of "list' or something like that..which I don't understand
But beyond that I have no clue how any of it goes together, where i declare stuff, how do I make the searches template searches, etc :| I can do this stuff separately. But when it comes to making a template class and doing it all together I just go blank :\
Any help/pointers/toss in the right direction is appreciated.
EDIT: and I know that ArrayUtil.selectSort(arraynameinhere); and ArrayUtil.insertionSort(arraynameinhere); are what I'd use in the test class to do the sorting. But still everything else is bleh...dunno when/where I'm declaring the array/variables...blah blah blah totally lost