# Problem with bubble sort code

• 11-10-2010, 05:00 AM
flyingcurry
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

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);                         }                 }         }```
• 11-10-2010, 05:09 AM
Fubarable
What is the value of "length"?
• 11-10-2010, 05:23 AM
flyingcurry
Quote:

Originally Posted by Fubarable
What is the value of "length"?

Oh, length just stands for the array.length
• 11-10-2010, 09:58 AM
JosAH
Quote:

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

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?

Code:

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

kind regards,

Jos
• 11-11-2010, 04:14 AM
flyingcurry
Quote:

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.