# Bubble Sort Test

Page 1 of 1

## 3 Replies - 5267 Views - Last Post: 26 January 2010 - 10:14 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=152173&amp;s=a00ba08c4375a8e53dfe0a8259690100&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 livtorres9

• New D.I.C Head

Reputation: 0
• 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:
[[email protected]
65
Ascending Order:
[[email protected]
94
Ascending Order:
[[email protected]
83
Ascending Order:
[[email protected]
59
Ascending Order:
[[email protected]
95
Ascending Order:
[[email protected]
13
Ascending Order:
[[email protected]
91
Ascending Order:
[[email protected]
98
Ascending Order:
[[email protected]
8
Ascending Order:
[[email protected]
78
Ascending Order:
[[email protected]
72
Ascending Order:
[[email protected]
23
Ascending Order:
[[email protected]
20
Ascending Order:
[[email protected]
58
Ascending Order:
[[email protected]
8
Ascending Order:
[[email protected]
13
Ascending Order:
[[email protected]
44
Ascending Order:
[[email protected]
77
Ascending Order:
[[email protected]
61
Ascending Order:
[[email protected]

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

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

## Re: Bubble Sort Test

Posted 26 January 2010 - 07:21 PM

Close your [code] tags.

### #3 erik.price

• D.I.C Lover

Reputation: 486
• 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);

```

### #4 ShaneK

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

Reputation: 240
• 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