class BubbleSort<T> extends ArraySort<T>
{
public void iSort(T[] inArray) {
int n = inArray.length;
int k;
for (int m = n; m >= 0; m--) {
for (int i = 0; i < n - 1; i++) {
k = i + 1;
if (compare(inArray[i], inArray[k])) {
T temp;
temp = inArray[i];
inArray[i] = inArray[k];
inArray[k] = temp;
}
}
}
print(inArray);
}
public static <T extends Comparable<T>> boolean compare(T a, T b) {
if (a.compareTo(b) > 0) {
return true;
} return false;
}
I'm getting (T extends comparable < T >, T extends comparable < T >) in the type bubblesort< T > is not applicable for the arguments (T,T) error!
The quick fix is telling me to change method compare(T,T)< T > to compare(T,T), but that wouldn't resolve my problem. It works perfectly fine when I enter in actual value of elements, for example, compare(3, 5) or compare("hi", "hello") instead of compare(inArray[i], inArray[k]).
I would appreciate it a lot if someone could explain why it's doing that and give me a solution.
You've already defined your class with a generic type T, so there's no point in declaring a separate one for the method:
class BubbleSort<T extends Comparable<T>> extends ArraySort<T> {
public void iSort(T[] inArray) {
int n = inArray.length;
int k;
for (int m = n; m >= 0; m--) {
for (int i = 0; i < n - 1; i++) {
k = i + 1;
if (compare(inArray[i], inArray[k])) {
T temp;
temp = inArray[i];
inArray[i] = inArray[k];
inArray[k] = temp;
}
}
}
print(inArray);
}
public static boolean compare(T a, T b) {
if (a.compareTo(b) > 0) {
return true;
}
return false;
}
}
Anyway, you should denote T extends Comparable<T> in the class declaration rather than in the method.
Related
I am trying to use a generic method to sort an array. I am receiving an error on Lab6Sort(octArr); that says classname cannot be applied to Shape[].
public static void main(String[] args) {
Shape[] octArr = new Shape[10];
for(int i = 0; i < 10; i++){
octArr[i] = new L6MPerRegOct(Math.floor(Math.random() * 1000) / 10);
}
Lab6Sort(octArr);
}
.
.
public static <AnyType> void Lab6Sort (AnyType [] arr, Comparator<? super AnyType> cmp)
It seems that I need a second argument, but I am unsure what this should be.
Here is the complete code:
public class L6MPerRegOct extends Shape {
public static void main(String[] args) {
Shape[] octArr = new Shape[10];
for(int i = 0; i < 10; i++){
octArr[i] = new L6MPerRegOct(Math.floor(Math.random() * 1000) / 10);
}
Lab6Sort(octArr);
}
private double sideLength;
public L6MPerRegOct(double len){
sideLength = len;
}
public double area(){
return 2 * sideLength*sideLength * (1 + Math.sqrt(2));
}
public static <AnyType> void Lab6Sort (AnyType [] arr, Comparator<? super AnyType> cmp)
{
int j, minIndex, n = arr.length;
AnyType temp;
for ( int index = 0; index < n - 1; index++ ) {
minIndex = index;
for (j = index + 1; j < n; j++) {
if (cmp.compare(arr[index], arr[minIndex]) < 0)
minIndex = j;
}
if (minIndex != index) {
temp = arr[index];
arr[index] = arr[minIndex];
arr[minIndex] = temp;
}
}
public abstract class Shape implements Comparable<Shape>
{
public abstract double area( );
public abstract double perimeter( );
public int compareTo( Shape rhs )
{
double diff = area( ) - rhs.area( );
if( diff == 0 )
return 0;
else if( diff < 0 )
return -1;
else
return 1;
}
public double semiperimeter( )
{
return perimeter( ) / 2;
}
}
You need to pass it an instance of a Comparator, e.g.
Lab6Sort(octArr, new Comparator<Shape>() {
#Override
public int compare(Shape o1, Shape o2) {
return 0;
}
});
Or define the Comparator in a separate class, if you want to reuse it
public class ShapeComparator implements Comparator<Shape> {
#Override
public int compare(Shape o1, Shape o2) {
return 0;
}
}
class ShapeComparator implements Comparator<Shape> {
#Override
public int compare(Shape o1, Shape o2) {
return 0;
}
}
edit: not sure if this is a correct fix but for some reason adding extends Comparable gets rid of the errors
I keep getting this same error. Why?
The error I receive is:
The method maxHeapify(T[], int) in the type HeapSort is not applicable for the arguments (T[], int)
Here's my code:
public class HeapSort {
private static int parent(int i)
{
if(i % 2 == 0)
{
return((i-1)/2);
}
else
{
return(i/2);
}
}
private static int leftChild(int i)
{
return(2*i + 1);
}
private static int rightChild(int i)
{
return(2*i + 2);
}
private static <T> void buildMaxHeap(T[] array)
{
int heapSize = array.length;
for(int i = (heapSize/2 - 1); i >= 0; i--)
{
maxHeapify(array, i);
}
}
private static <T extends Comparable<T>> void maxHeapify(T[] array, int i)
{
int L = leftChild(i);
int R = rightChild(i);
int Largest = (int) array[0];
T temp;
if(L <= array.length && array[R].compareTo(array[i]) > 0)
{
Largest = L;
}
else
{
Largest = i;
}
if(R <= array.length && array[R].compareTo(array[Largest]) > 0)
{
Largest = R;
}
if(Largest != i)
{
Swap(array[i], array[Largest]);
maxHeapify(array, Largest);
}
}
public static <T> void heapSort(T[] array)
{
buildMaxHeap(array);
int heapSize = array.length;
for(int i = array.length - 1; i >= 0; i--)
{
Swap(array[0], array[i]);
heapSize = heapSize-1;
maxHeapify(array, 0);
}
}
}
The reason for the error is because this line defines T differently
private static <T extends Comparable<T>> void maxHeapify(T[] array, int i)
to this
private static <T> void buildMaxHeap(T[] array)
and this
public static <T> void heapSort(T[] array)
Its
<T>
vs
<T extends Comparable<T>>
because buildMaxHeap and heapSort both call maxHeapify so you get a generics type mismatch
If you convert all the types to
<T extends Comparable<T>>
Then this will fix that error
You will also have another error when you fix this
int Largest = (int) array[0];
because your array doesn't contain int's, it contains your generic type objects
My merge sort doesn't seem to be working correctly. When I display the sorted list, it is not sorted and elements are added, where there is supposed to be 9 there is 49.
Anyone see where Im going wrong?
public static <E extends Comparable<E>> void mergeSort(List<E> A) {
int n = A.size();
if (n > 1) {
int half = n / 2;
List<E> B = copyPartialArray(A, 0, half);
List<E> C = copyPartialArray(A, half, n);
mergeSort(B);
mergeSort(C);
merge(B, C, A);
}
}
public static <E extends Comparable<E>> void merge(List<E> B, List<E> C, List<E> A) {
int n1 = B.size();
int n2 = C.size();
int i = 0;
int j = 0;
int k = 0;
while (i < n1 && j < n2) {
if (B.get(i).compareTo(C.get(j)) < 0) {
A.add(k, B.get(i));
i++;
}
else {
A.add(k, C.get(j));
j++;
}
k++;
}
if (i == n1)
for (int p = j; p < n2; p++) {
A.add(k, C.get(p)); k++;
}
else if (j == n2)
for (int p = i; p < n1; p++) {
A.add(k, B.get(p)); k++;
}
}
private static <E extends Comparable<E>> List<E> copyPartialArray(List<E> A, int first, int last) {
int n = last - first;
List<E> copy = new ArrayList<E>(n);
for (int i = 0; i < n; i++)
copy.add(i, A.get(first + i));
return copy;
}
This answer will try to make you realise what's wrong.
It's clear that mergeSort won't do anything to a one element array, but what happens if there are two (for instance [2,1])? You mention there are more elements than before in the result list (list A). Why? What's merge doing to that list? Hint.
I am trying to find the the Longest Common Sub-sequence between two things of type comparable. I have the algorithm down, but I could like to add these items to a list via a recursion method call, but I do not know how I would add the last item to the list this way.
Here is my code:
public static <E extends Comparable<E>>List<E> getLongestCommonSubSeq(
List<E> x,
List<E> y )
{
int m = x.size() +1;
int n = y.size() +1;
String[][] b =new String [m][n];
int[][] c = new int[m][n];
for(int i=1;i<m;i++) {
c[i][0] = 0;
}
for(int j = 0;j<n;j++) {
c[0][j]= 0;
}
for(int i=1; i<m;i++) {
for(int j=1;j<n;j++) {
if(x.get(i).equals(y.get(j))) {
c[i][j] = c[i-1][j-1]+1;
b[i][j]="NW";
}
else if(c[i-1][j] >= c[i][j-1]) {
c[i][j]=c[i-1][j];
b[i][j]="N";
}
else {
c[i][j] = c[i][j-1];
b[i][j]= "W";
}
}
}
return getLCS( m,n, b, x );
}
public static <E extends Comparable<E>> List<E> getLCS(
int i,
int j,
String[][] b,
List<E> x )
{
if(i==0 || j ==0)
return null;
if(b[i][j].equals("NW")) {
// This can't be done because add returns a boolean type
return getLCS(i-1,j-1, b, x) .add(x.get(i));
}
if(b[i][j].equals("N")) {
return getLCS(i-1,j, b, x);
}
if(b[i][j].equals("W")) {
return getLCS(i, j-1, b, x);
}
return null;
}
public static <E extends Comparable<E>>List<E> getLCS(int i, int j, String[][] b, List<E> x){
List<E> ret = new ArrayList<E>();
if(i==0 || j ==0)
return ret;
if(b[i][j].equals("NW")) {
// This can't be done because add returns a boolean type
ret=getLCS(i-1,j-1, b, x);
ret.add(x.get(i));
}else if(b[i][j].equals("N")) {
ret = getLCS(i-1,j, b, x);
}else if(b[i][j].equals("W")) {
ret= getLCS(i, j-1, b, x);
}
return ret;
}
I was implementing it a little wrong
public class Structure <E extends Comparable<? super E>>{
private E[] d;
public Structure() { d = getArray(1); }
public void show() { show(0); }
private void show(int p){
if( p < d.length && d[p] != null) {
show(r(p));
show(l(p));
System.out.print(d[p] + " ");
}
}
public void add(E obj) {
int p = getPos(obj);
if(p >= d.length)
resize();
d[p] = obj;
}
public boolean present(E obj){
int p = getPos(obj);
return p < d.length && d[p] != null;
}
private int getPos(E obj){
int p = 0;
while(p < d.length && d[p] != null){
int dir = <*1>;
if(dir < 0)
p = l(p);
else if(dir >0)
p = r(p);
else
return p;
}
return p;
}
private E[] getArray(int size) {
return (E[]) new Comparable[size];
}
private void resize(){
E[] temp = getArray(d.length*2 + 1);
for( int i = 0; i < d.length; i++)
temp[i] = d[i];
d = temp;
}
private int l(int i) { return 2 * i + 1;}
private int r(int i) { return 2 * i + 2;}
}
Take that data structure. What is it? I think it's a binary search tree, but I'm pretty sure it's that or a max heap. I'm largely leaning BST, though.
public void fillCol (int n, Collection<Integer> col){
for(int i = 0; i < n; i++)
col.add (i);
}
What is the big O for that method if col is a linked list? I think it's O (N).
And is col a tree set? I think it's O (N log N).
public void sort (List<Double> data){
int lim = data.size();
for(int i = 0; i < lim; i++){
int m = i;
for(int j = i + 1; j < lim; j++)
if(data.get(j) < data.get(m) )
m = j;
data.set( i, data.set(m, data.get(i)));
}
}
and big o for each type of list. I think it's O (N²) for ArrayList and O (N³) for Linked list.
A class that represents a graph uses an adjacency matrix to represent the connections between verticies. What are the space requirements for a graph that contains N nodes with an average of M connections per node?
I think it's O (N²)
Please help! Confirm if I'm right, or correct me if I'm wrong.
It looks like a (not-necessarily-balanced) binary tree, implemented in a manner similar to how a binary heap is often done - in an array where the children of i are 2i and 2i+1.
Someone should've documented what they were doing better.
I agree with your assessment of fillCol.
That sort callable seems like an unrelated question, and yes it does look O(n^2) with a normal data structure.