2

That is my Java code for a Generic bubbleSorter:

public class BubbleSorter<E extends Comparable<E>> {
    E[] a;
    void swap(int i, int j) {
        E temp;
        temp=a[i];
        a[i]=a[j];
        a[j]=temp;
    }
    void bubbleSort(E[] a) {
        this.a=a;
        for (int i=0 ;i<a.length;i++) {
            for (int j=0;j<a.length;j++) {
                if ( a[i].compareTo(a[j]) > 0) swap(i,j);
            }
        }
    }

}

public interface Comparable<E> {
    public int compareTo(E e);
}

and here is an example of its use:

public class Test { 
    public static void main (String arg[]) {
        Rational[] a = new Rational[3];
        a[0]=Rational.rationalFactory(9,2);
        a[1]=Rational.rationalFactory(1,3);
        a[2]=Rational.rationalFactory(10,11);
        Complex[] b = new Complex[3];
        b[0]=new Complex(7,5);
        b[1]=new Complex(3,4);
        b[2]=new Complex(8,9);
        BubbleSorter<Rational> br=new BubbleSorter<Rational>();
        BubbleSorter<Complex> bi=new BubbleSorter<Complex>();
        br.bubbleSort(a);
        bi.bubbleSort(b);
        for (int i=0 ; i < 3 ; i++ ) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
        for (int i=0 ; i < 3 ; i++ ) {
            System.out.print(b[i] + " ");
        }
    }
}

public class Rational implements Comparable<Rational> {
    int mone,mehane;
    private Rational(int n,int m) {
        mone=n;
        mehane=m;
    }
    static public Rational rationalFactory(int n,int m) {
        if (n==0) return null;
        return new Rational(n,m);
    } 
    public String toString() {
        return mone + "/" + mehane;
    }
    public int compareTo(Rational r) {
        return (r.mehane*mone - r.mone*mehane);
    }
}
public class Complex implements Comparable<Complex> {
        int real,img;
        public Complex(int r,int i) {
            real=r;
            img=i;
        }
        public String toString() {
            return real + "+" + img + "i";
        }
        public int compareTo(Complex r) {
            double x=(getLength() - r.getLength());
            if (x>0) return 1;
            if (x==0) return 0;
            return -1;
        }
        public double getLength() {
            return Math.sqrt(real*real+img*img);
        }
}

When I tried to convert my Java code to C#, I got stuck trying to force the generic type to extend Comparable since < E : Comparable > doesnt work. How can i overcome this?

that is what i tried :

abstract class Comparable<E> {

    static bool operator ==( Comparable<E> e1, Comparable<E> e2 );
    static bool operator !=( Comparable<E> e1, Comparable<E> e2 ) {
        return !( e1 == e2 );
    }
    static bool operator >( Comparable<E> e1, Comparable<E> e2 );
    static bool operator >=( Comparable<E> e1, Comparable<E> e2 ) {
        if ( e1 > e2 ) return true;
        if ( e1 == e2 ) return true;
        return false;
    }
    static bool operator <=( Comparable<E> e1, Comparable<E> e2 ) {
        return !( e1 > e2 );
    }
    static bool operator <( Comparable<E> e1, Comparable<E> e2 ) {
        return !( e1 >= e2 );
    }
}

public class BubbleSorter<E : Comparable<E>> {
        E[] a;
        void swap(int i, int j) {
            E temp;
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
        void bubbleSort(E[] a) {
            this.a=a;
            for (int i=0 ;i<a.length;i++) {
                for (int j=0;j<a.length;j++) {
                    if ( a[i]>a[j] ) swap(i,j);
                }
            }
        }

}
1
  • I am trying to learn. thats all. Commented Jul 2, 2012 at 18:14

4 Answers 4

7

You should use the built-in IComparable<T> interface, and then declare your class as

public class BubbleSorter<T> where T : IComparable<T> { ... }

The where keyword defines a "constraint" on the generic parameter T. The compiler will enforce this constraint by ensuring that for any instantiation of the generic class, the type argument implements the IComparable<T> interface.

Sign up to request clarification or add additional context in comments.

3 Comments

nice, Thanks. but what if i want to be able to inherit? how do i fill it in the syntax?
@OfekRon I'm not sure I follow. Do you want to inherit from BubbleSorter<T>? Or from IComparable<T>?
This works well if the implementation of E can be modified. Otherwise, a helper class implementing IComparer<E> will need to be supplied.
2

The keyword to use for generic constraints in C# is where.

Therefore, first declare the signature of your generic type:

public class BubbleSorter<E>

Then define the generic constraints:

    where E : IComparable<E>

A word on coding conventions: In C#, it is customary to call a single generic argument T (like type) rather than E (like element). That's the pattern used in all the framework collection classes, so you might want to adapt your type name:

public class BubbleSorter<T>
    where T : IComparable<T>
{
    // ...
}

Behind the colon (:), you can specify a comma-separated list of interfaces and possibly a class name. The compiler knows which is which, so you don't have to explicitly specify whether you want to implement (an interface) or inherit (from a class).

Comments

1

This is C# syntax:

public class BubbleSorter<E> where E : Comparable<E>

2 Comments

and what if i want to be able to inherit? how do i fill it in?
@OfekRon Static methods (and therefore, operator overloads as well) cannot be virtual, which means they cannot be overridden or abstract.
1

In C#, one usual idiomatic way to implement comparability for a type is to have it derive from IComparable<T>. If you can't change the type to implement IComparable, then you can implement a helper class that implements IComparer<E>.

public class BubbleSorter<E> 
{
    static void Swap(E[] a, int i, int j) 
    { 
        E temp; 
        temp=a[i]; 
        a[i]=a[j]; 
        a[j]=temp; 
    } 

    public void BubbleSort(E[] a, IComparer<E> comparer) 
    { 
        for (int i=0 ;i<a.length;i++) { 
            for (int j=0;j<a.length;j++) { 
                if ( comparer.Compare(a[i],a[j]) > 0 ) swap(a,i,j); 
            } 
        } 
    } 
 } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.