Java Bubble sort

Bubble sort algorithms to use with " User defined data types"

Page 1 of 1

9 Replies - 5486 Views - Last Post: 18 March 2010 - 03:59 AM Rate Topic: -----

#1 lil' bali   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-March 10

Java Bubble sort

Posted 17 March 2010 - 03:00 PM

hi every 1..

all rite lets c who can help mke out with this...


ok..my uni lecturer left out a question about tis bubble sort algo of javga...

he says how can we convert the bubble sort algorithm in such a way as it can accept " user defined" data types as input and not only integers....


hit me bak guys..
Is This A Good Question/Topic? 0
  • +

Replies To: Java Bubble sort

#2 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Java Bubble sort

Posted 17 March 2010 - 03:03 PM

You could use the magic that is Generics to to accept any class. But you would probably want to only use something that extended implemented Comparable.

http://java.sun.com/...rics/index.html

edit: thanks Dogstopper :)

This post has been edited by erik.price: 17 March 2010 - 03:19 PM

Was This Post Helpful? 0
  • +
  • -

#3 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Java Bubble sort

Posted 17 March 2010 - 03:17 PM

You will want to IMPLEMENT Comparable...This allows two user-defined types to be compared like integers.

Here's a good tutorial:
http://java.sun.com/...aces/order.html
Was This Post Helpful? 1
  • +
  • -

#4 pbl   User is offline

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

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Bubble sort

Posted 17 March 2010 - 09:04 PM

View Postlil said:

he says how can we convert the bubble sort algorithm in such a way as it can accept " user defined" data types as input and not only integers....

You mean user defined Class

class Dog implements Comparable<Dog> {
   double price;
...
   public int compareTo(Dog otherDog) {
     return (int) (price - otherDog.price);
   }
}


Was This Post Helpful? 1
  • +
  • -

#5 JonBernal   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 199
  • Joined: 14-March 09

Re: Java Bubble sort

Posted 17 March 2010 - 09:09 PM

I'm not much of a programmer yet, but in order to sort
something you need to have a define way of doing so, for
example you need to have it from min to max, or something
like that.

So if you want to sort user defined data, then you must also
specify how to sort it.
Was This Post Helpful? 0
  • +
  • -

#6 pbl   User is offline

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

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Bubble sort

Posted 17 March 2010 - 09:15 PM

View PostJonBernal, on 17 March 2010 - 10:09 PM, said:

So if you want to sort user defined data, then you must also
specify how to sort it.

This is what the Comparable interface is for
Was This Post Helpful? 0
  • +
  • -

#7 JonBernal   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 199
  • Joined: 14-March 09

Re: Java Bubble sort

Posted 17 March 2010 - 09:24 PM

ahh, Well then awesome, i guess i'm not too
lost :D
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12390
  • View blog
  • Posts: 45,519
  • Joined: 27-December 08

Re: Java Bubble sort

Posted 17 March 2010 - 09:34 PM

I agree with erik.price here; generics are a good way to go. I would start off my method header using Generic paramterization to accept only types that are Comparable. Like so:
/*
  T: Reference data type 
  Comparable<? super T>: Elements that are instances of T or instances of T subclass
  <T extends Comparable<? super T>>: T implements Comparable interface
*/
public <T extends Comparable<? super T>> void sort(T[] array){
   //bubblesort implementation here
   //you can use compareTo() on all elements of T
   //since they are all Comparable to T
}


Was This Post Helpful? 3
  • +
  • -

#9 pbl   User is offline

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

Reputation: 8379
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Bubble sort

Posted 17 March 2010 - 10:02 PM

View Postmacosxnerd101, on 17 March 2010 - 10:34 PM, said:

I agree with erik.price here; generics are a good way to go. I would start off my method header using Generic paramterization to accept only types that are Comparable. Like so:
/*
  T: Reference data type 
  Comparable<? super T>: Elements that are instances of T or instances of T subclass
  <T extends Comparable<? super T>>: T implements Comparable interface
*/
public <T extends Comparable<? super T>> void sort(T[] array){
   //bubblesort implementation here
   //you can use compareTo() on all elements of T
   //since they are all Comparable to T
}


I'll give you a +1 for that one but this contains too much <> that I will ever been abble to understand

can you guys keep it kiss ? Simple ?
Was This Post Helpful? 0
  • +
  • -

#10 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7292
  • View blog
  • Posts: 15,179
  • Joined: 16-October 07

Re: Java Bubble sort

Posted 18 March 2010 - 03:59 AM

View Postpbl, on 17 March 2010 - 11:02 PM, said:

can you guys keep it kiss ? Simple ?


In terms of simple, I'm afraid for generics, it is. :P

Since you don't really care about type safety. And the only thing you really need is for the object to implement one little interface. You should be able to just do this:
void sort(Comparable [] array) {



While this is logical and does work, Java since generics will complain with a warning.
Note: Sort.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.




If you write this you get the same warning: public <T extends Comparable> void sort(T [] array) {. Since is basically says the same thing.

You have to now choose: tolerate annoying warnings or annoying syntax. At least the syntax does have meaning. Type safety is your friend, even if, in this case, it's somewhat misplaced.

edit:
It should probably also be noted that rather than put the compare in the object, you can specify it at sort time, which is a lot more flexible. e.g.
Arrays.sort(dogs, new Comparator<Dog>() {
	public int compare(Dog dog1, Dog dog2) {
		return (int) (dog1.price - dog2.price);
	}
});


Oops. Not simple enough?

This post has been edited by baavgai: 18 March 2010 - 04:08 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1