Bubble Sort Test

Bubble Sort Test

Page 1 of 1

3 Replies - 2732 Views - Last Post: 26 January 2010 - 10:14 PM Rate Topic: -----

#1 livtorres9  Icon User is offline

  • New D.I.C Head

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

Bubble Sort Test

Posted 26 January 2010 - 06:41 PM

here's the class

[code]
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] >(array[j]) && array[j]>0)
{
temp = array[j-1];
array[j-1] = array[j];
array [j] = temp;
}
}
}
}

}

[\code]

and the driver

[code]

import java.util.Random;

public class Bubble1Test
{
public static void main (String[] args)
{
Random rand = new Random();

int [] array = new int [20];

for(int x = 0; x< array.length; x++)
{
array[x] = rand.nextInt(100) + 1;
System.out.println(array[x]);

Bubble1.bubbleAsc(array);
System.out.println("Ascending Order: ");

System.out.println(array);
}


[\code]

I'm trying to sort an array with 20 random integers. It compiles fine but the output looks like this

--------------------Configuration: <Default>--------------------
37
Ascending Order:
[I@1cd2e5f
65
Ascending Order:
[I@1cd2e5f
94
Ascending Order:
[I@1cd2e5f
83
Ascending Order:
[I@1cd2e5f
59
Ascending Order:
[I@1cd2e5f
95
Ascending Order:
[I@1cd2e5f
13
Ascending Order:
[I@1cd2e5f
91
Ascending Order:
[I@1cd2e5f
98
Ascending Order:
[I@1cd2e5f
8
Ascending Order:
[I@1cd2e5f
78
Ascending Order:
[I@1cd2e5f
72
Ascending Order:
[I@1cd2e5f
23
Ascending Order:
[I@1cd2e5f
20
Ascending Order:
[I@1cd2e5f
58
Ascending Order:
[I@1cd2e5f
8
Ascending Order:
[I@1cd2e5f
13
Ascending Order:
[I@1cd2e5f
44
Ascending Order:
[I@1cd2e5f
77
Ascending Order:
[I@1cd2e5f
61
Ascending Order:
[I@1cd2e5f

Process completed.

thanks if you can help. i've tried some things but can't figure out how to get it working

Is This A Good Question/Topic? 0
  • +

Replies To: Bubble Sort Test

#2 evinrows  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 141
  • Joined: 03-August 09

Re: Bubble Sort Test

Posted 26 January 2010 - 07:21 PM

Close your [code] tags.
Was This Post Helpful? 0
  • +
  • -

#3 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

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

Re: Bubble Sort Test

Posted 26 January 2010 - 07:27 PM

System.out.println(array);


You can't print out the entire array at once (well you can, but it's not what you want to do). Either print out only a specific element of it with array[someIndex] or print each element of it with a for/foreach
for(int i: array)
   System.out.println(i);


Was This Post Helpful? 1

#4 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Bubble Sort Test

Posted 26 January 2010 - 10:14 PM

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] >(array[j]) && array[j]>0)
{
temp = array[j-1];
array[j-1] = array[j];
array [j] = temp;
}
}
}
}

}




This is a tad bit wrong.
Bubble sorting consists of changing the array's order by checking the current element, seeing if it's bigger than the next element and changing it if it is. Then, on the next run through, it doesn't hit the last one it just put in the right place (to make it more efficient) because it's already in the right order...

Yours blows off that last part and also checks the element before the current one, which would create a major problem if you're at, for example, the first element in the array.

I created something like this, hopefully you can understand everything I meant:
	public static int[] bubbleSort(int[] a){
		int help = 0;
		int count = 1; //Edit, this was at the wrong value for some reason - would have caused errors if ran.
			//This adds up "count" so that the nested for loop doesn't even bother checking the last element that it just ordered...
		for(int j = 0; j < a.length; j++, count++){
				//This just loops through the array as needed
			for(int i = 0; i < a.length - count; i++){
					//Simply check the current element and the next one
				if(a[i] > a[i+1]){
						//If the next one is bigger, move the current one to the next one's position
					help = a[i];
					a[i] = a[i+1];
					a[i+1] = help;
				}
			}
		}
			//Return the result
		return a;
	}



Yours,
Shane~

This post has been edited by ShaneK: 27 January 2010 - 07:16 AM

Was This Post Helpful? 1

Page 1 of 1