# Thread: Problem with bubble sort code

1. Member
Join Date
Oct 2010
Posts
12
Rep Power
0

## Problem with bubble sort code

The method below for bubble sort should work, it displays the unsorted array fine, but for the sorted array it only displays one of the numbers. It seems to have just skipped over the other elements in the array.

Here is a sample output:
How many random even integers would you like to generate? 6
Unsorted:
196
166
68
14
68
176
Sorted:
196
5

Java Code:
```public static void bubbleSort (int [] array, int length) {

int i, j,t=0;
System.out.println("Unsorted: ");
for (i=0; i<array.length; i++)
{
System.out.println(array[i]);

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

System.out.println("Sorted: ");
for (i=0; i<length-1; i++);
{
System.out.println(array[i]);
System.out.println(i);
}
}
}```

2. Member
Join Date
Oct 2010
Posts
12
Rep Power
0
Originally Posted by Fubarable
What is the value of "length"?
Oh, length just stands for the array.length

3. Originally Posted by flyingcurry
The method below for bubble sort should work, it displays the unsorted array fine, but for the sorted array it only displays one of the numbers. It seems to have just skipped over the other elements in the array.

Here is a sample output:
How many random even integers would you like to generate? 6
Unsorted:
196
166
68
14
68
176
Sorted:
196
5

Java Code:
```public static void bubbleSort (int [] array, int length) {

int i, j,t=0;
System.out.println("Unsorted: ");
for (i=0; i<array.length; i++)
{
System.out.println(array[i]);

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

System.out.println("Sorted: ");
for (i=0; i<length-1; i++);
{
System.out.println(array[i]);
System.out.println(i);
}
}
}```
Do you want to print out the 'sorted' array while you are sorting it? i.e. print it out after each sorting step? Note that the last loop also changes the variable 'i' (as the outer loop does). Use another variable for the last loop. btw that variable 'length' is redundant, i.e. your array itself 'knows' its length: array.length. Also what is the semicolon doing here?

Java Code:
`for (i=0; i<length-1; i++);`
... remove it.

kind regards,

Jos

4. Member
Join Date
Oct 2010
Posts
12
Rep Power
0
Originally Posted by JosAH
Do you want to print out the 'sorted' array while you are sorting it? i.e. print it out after each sorting step? Note that the last loop also changes the variable 'i' (as the outer loop does). Use another variable for the last loop. btw that variable 'length' is redundant, i.e. your array itself 'knows' its length: array.length. Also what is the semicolon doing here?
Thanks, i realized after that making a variable for array.length was unnecessary.
The semi-colon was a bad typing mistake.

I changed the use of 'i' so that i is declared separatedly within each loop like "for int i" instead of one i used by every loop, and the bubble sort seems to work fine right now.

Thanks Jos.