MichelleJS
MichelleJS

Reputation: 759

How to define a generic sort class

First off let me say that this is related to homework for a class I'm taking on the side. I actually already have the answer for the homework but I decided I wanted to go a step further to help myself understand object oriented programming since I'm currently working as a programmer but I have some gaps in my education since I only actually did a few high level programming classes in college, most of what I did was assembly and c which is obviously much different.

Anyway I had to write a bubble sort for a homework assignment. No problem. But I was also reading about generics and didn't quite understand it so I wanted to make a generic bubblesort for my own understanding.

From what I understand the reason for using generics would be so you can use the same generic class on different types of data.

From my main method I call the doBubbleSort method and send it an arraylist of ints. I want to be able to send it an arraylist of any type.

Here is my bubbles sort updated for generics:

public static<T> ArrayList doBubbleSort(ArrayList<T> arrayList)
{
    boolean wasSwapped;//
    do{
        wasSwapped = false;
        for (int i=1; i<arrayList.size();i++)
        {
            int b = arrayList.get(i-1).compareTo(arrayList.get(i));
            if (b<0)
            {
                int temp = arrayList[i];
                arrayList[i] = arrayList[i-1];
                arrayList[i-1] = temp;
                wasSwapped = true;
            }


        }
    }while (wasSwapped);
    return arrayList;
}

I cannot for the life of me figure out how to use the compare statement properly. I am currently getting cannot find symbol - method compareTo(T) . I was hoping someone here could give me a nudge in the right direction.

Also I know that at somepoint I have to have the type specified. Is it okay to do it in the method call or does it have to be in the method itself. I assume that I call it like this ArrayList al = Sorting.doBubbleSort(al); and that if I want to sort strings instead it would be ArrayList al = Sorting.doBubbleSort(al);

Please help me out with my comparison and let me know where I have gone off the rails with my understanding of Generics. Thanks

Thanks for all your help so far. I have changed my code to reflect both @LuiggiMendoza and @newaccts suggestions. After much wrangling I got it working and I think I'm starting to understand it properly. The only problem is that I can't figure out how to declare a generic variable for temp. For now I have just added another generic List and am using element 0 for the swap but that seems pretty inefficient so I was wondering if you could make any suggestions.

    public static<T extends Comparable<? super T>> List<T> doBubbleSort(List<T> list)
    {
        boolean wasSwapped;
        List<T> temp = new ArrayList<>();
         temp.add(list.get(0));

        do{
            wasSwapped = false;
            for (int i=1; i<list.size();i++)
            {
                int b = list.get(i-1).compareTo(list.get(i));
                temp.set(0,list.get(i));
                if (b>0)
                {    
                    list.set(i,list.get(i-1));//list[i] = list[i-1];
                    list.set(i-1,temp.get(0));//list[i-1] = temp;
                    wasSwapped = true;
                }


            }
        }while (wasSwapped);
        return list;
    }

}

Upvotes: 0

Views: 1669

Answers (1)

Keppil
Keppil

Reputation: 46239

You need some way to compare your elements. The common way to solve this is to demand that they implement Comparable:

public static<T extends Comparable<T>> ArrayList<T> doBubbleSort(ArrayList<T> arrayList)

or even better (coding against interface):

public static<T extends Comparable<T>> List<T> doBubbleSort(List<T> list)

Another way is like @LuiggiMendoza suggests, to provide a Comparator to do the comparison:

public static <T> doBubbleSort(List<T> list, Comparator<T> comparator)

Upvotes: 4

Related Questions