Bubble Sort

Bubble Sort Ascending order

Page 1 of 1

6 Replies - 5907 Views - Last Post: 26 January 2010 - 12:17 PM Rate Topic: -----

#1 livtorres9   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 25-January 10

Bubble Sort

Posted 25 January 2010 - 09:33 PM

please help if you can!!!!!!!!

this is the error i'm getting using JCreator:
--------------------Configuration: <Default>--------------------
C:\All Java Programs\Bubble1.java:11: int cannot be dereferenced
if(array[j-1].equals(array[j])>0)
^
1 error

Process completed.


1. Create a class that will contain two bubble sort methods (ascending and descending) that utilizes an integer array as a parameter.


public class Bubble1
{
	public static void bubbleAsc(int [] array)
	{
		int temp;

		for(int i =(array.length-1);i>=0;i--)
		{
			for (int j = 1; j<=i;j++)
			{
				if(array[j-1].equals(array[j])>0)
				{
					temp = array[j-1];
					array[j-1] = array[j];
					array [j] = temp;
				}
			}
		}
	}


}



** Edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Bubble Sort

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12318
  • View blog
  • Posts: 45,417
  • Joined: 27-December 08

Re: Bubble Sort

Posted 25 January 2010 - 09:36 PM

The equals() method is used to compare Objects. In addition, you can't invoke a method from a primitive variable (or element in the array). You'd be better off using the ==, <, >, <=, >= and != operators to compare your elements, like so: if(array[j-1]. > array[j]). Also, lastly, the compareTo() method returns an int to compare using a relational operator, but equals() just returns a boolean value stating whether or not the two objects are equal.

This post has been edited by macosxnerd101: 25 January 2010 - 09:37 PM

Was This Post Helpful? 0
  • +
  • -

#3 livtorres9   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 25-January 10

Re: Bubble Sort

Posted 25 January 2010 - 09:47 PM

sorry, i don't really understand. I need to compare the two arrays. How would I go about doing that
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12318
  • View blog
  • Posts: 45,417
  • Joined: 27-December 08

Re: Bubble Sort

Posted 25 January 2010 - 10:01 PM

There is only 1 array, and it stores int variables. You want to compare those int variables; and to do so, you have to use the relational operators I showed you above instead of the equals() or compareTo() method, which can only be invoked from a non-primitive variable.
Was This Post Helpful? 0
  • +
  • -

#5 333OnlyHalfEvil   User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 674
  • Joined: 20-March 09

Re: Bubble Sort

Posted 25 January 2010 - 10:53 PM

Your if statement has two inequalities in it. It is of the form array[j-1] = array[j] > 0 which Java can't do. You need to split them up and use Java's and (&&) and or (||) operators so it looks more like this:
if ((array[j-1] == array[j]) && {array[j] > 0) {
	 //code
}



Hope that helps a little.

This post has been edited by 333OnlyHalfEvil: 25 January 2010 - 10:54 PM

Was This Post Helpful? 1
  • +
  • -

#6 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12318
  • View blog
  • Posts: 45,417
  • Joined: 27-December 08

Re: Bubble Sort

Posted 26 January 2010 - 05:14 AM

That's a good thought; however, I think the OP is thinking of the compareTo() method, which returns a positive integer, negative integer, or zero based on whether the invoking object is greater than, less than, or equal to the parameter. By checking to see if an element is > 0, we would be leaving the list unsorted. Remember, most (if not all) sorting algorithms allow for both positive and negative numbers.

Also, watch those rogue braces. {array[j] > 0) :)
Was This Post Helpful? 1
  • +
  • -

#7 livtorres9   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 25-January 10

Re: Bubble Sort

Posted 26 January 2010 - 12:17 PM

Thanks. both of you really helped a lot!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1