7 Replies - 265 Views - Last Post: 06 December 2012 - 07:59 PM Rate Topic: -----

#1 simman92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 06-December 12

Making BubbleSort Accept Generics

Posted 06 December 2012 - 01:10 PM

Hi. this is my first post so please forgive me for errors.

I have made a Bubble Sort for ints.
my next step is to make my Bubble Sort
more sophisticated by making it work
for a generic array. I have to write a
test program for my method using arrays
with these three data types: Double, Character, and String.

so far i have
 
public class Sort {


	public static void main(String[] args) {
		int array[] = { 23, 12, 466, 22,1,2,3,4,56,2,6,4 };
		int temp;
		for (int i = 0; i < array.length -1; i++) {
			for (int j = 0; j < (array.length-1) - i; j++) {
				if (array[j + 1] < array[j]) {
					temp = array[j + 1];
					array[j + 1] = array[j];
					array[j] = temp;
				}
			}
		}
		System.out.println("Sorted Array");
		for (int i = 0; i < array.length; i++) {
			System.out.println(array[i]);
		}
		
	}

}



thanks in advance for all help!

Is This A Good Question/Topic? 0
  • +

Replies To: Making BubbleSort Accept Generics

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1275
  • View blog
  • Posts: 2,837
  • Joined: 05-April 11

Re: Making BubbleSort Accept Generics

Posted 06 December 2012 - 01:21 PM

Why not just make a method which accepts an array of type Object? You do not need to know the exact type when doing a bubblesort
public void bubblesort(Object[] array) {



edit: yeah has to be Compareable instead of Object as they pointed out

This post has been edited by CasiOo: 06 December 2012 - 01:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10179
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Making BubbleSort Accept Generics

Posted 06 December 2012 - 01:23 PM

That's a bad idea, because you won't know that the elements are mutually Comparable. Use generics here instead:
public <T extends Comparable<? super T>> void bubblesort(T[] elements){}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Making BubbleSort Accept Generics

Posted 06 December 2012 - 01:24 PM

You can't have generic array of basic datatype in Java
If it is an array of Object like (Integer, Character, Doubkle, Float, ...) you can simply declare the tyupe of the array as Comparable in the method parameter

void sort(Comparable[] array) {
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10179
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Making BubbleSort Accept Generics

Posted 06 December 2012 - 01:58 PM

Quote

void sort(Comparable[] array) {

That still won't ensure that all the elements can be compared to each other. It is quite possible to run into something like a ClassCastException should that be the case.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Making BubbleSort Accept Generics

Posted 06 December 2012 - 02:07 PM

Mac,
How can you call, at compilation time, a method sort(Comparable[] array)
with an array of Object that do not implement Comparable ?

May be an element might be null, ok we are better to test it within the sort
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10179
  • View blog
  • Posts: 37,585
  • Joined: 27-December 08

Re: Making BubbleSort Accept Generics

Posted 06 December 2012 - 02:15 PM

As proof of concept. Notice how comparing Apples and Oranges crashes at runtime, but it is fine to store the two in the array. Sorting relies on comparisons, so the problem should be clear. Whereas if you use methods to enforce that the elements are mutually Comparable, you won't run into the problem of worrying about comparisons causing a crash (because of type) during runtime.
package test;


class Apple implements Comparable<Apple>{
    
    public Apple(){}
    
    public int compareTo(Apple other) {
        return 0;
    }

}

class Orange implements Comparable<Orange>{
    
    public Orange(){}
    
    public int compareTo(Orange other){
        return 0;
    }
}

class Test{

    private Comparable[] array;
    
    public Test(){
        array = new Comparable[2];
        array[0] = new Apple();
        array[1] = new Orange();
        
        array[0].compareTo(array[1]); //fails with ClassCastException
        compare(array[0], array[1]); //fails with ClassCastException
        compare(array[0], new Apple());
    }
    
    public <T extends Comparable<? super T>> int compare(T one, T two){
        return one.compareTo(two);
    }
    
    public static void main(String[] args){
        new Test();
    }
    
}


Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: Making BubbleSort Accept Generics

Posted 06 December 2012 - 07:59 PM

Ha... I see what you mean... :^:
You shouldn't send an array opf Comparable to the sort() method but an array of Orange or an Array of Apple

How can you compare Orange with Apple ? Good thing Java crashes if you try to do so :^:

Good show anyhow
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1