# BuBBle Sorting

• 03-28-2011, 08:16 PM
Bgreen7887
BuBBle Sorting
Hey guy basic bubble sorting . I 've triple checked code and still cant find mistake. Output is 45.37,22,16,99
Code:

```public class Sort {         public static void main(String[] args) {         int i =0;                 int[] list = {45,37,22,16,99};         for(i = list.length-1; i < 0; i--){                 for(int k =0 ; k <list.length-1; k++ ){                 int tempa = list[k];                 int tempb = list[k+1];                         if (list[k] > list[k+1]){                         list[k] = tempb;                         list[k+1] = tempa;                                                         }                                 }                         }                                                 for(int x =0; x < list.length; x ++) {                         System.out.println(list[x] + "");                         }                 }         }```
• 03-28-2011, 08:35 PM
doWhile
Code:

`for(i = list.length-1; i < 0; i--){`
Look closely at the above loop that's defined in your code. Imagine an array of any length, what will happen when this executes (answer: nothing)
• 03-28-2011, 08:41 PM
FlipPoker@gmail.com
Your other for loop isn't correct either:
Code:

`for(int k =0 ; k <list.length-1; k++ ){`
This loop is not counting the way it should be for bubblesort.
• 03-28-2011, 08:42 PM
ozzyman
as doWhile is telling you, i > 0; not < 0.
• 03-28-2011, 08:54 PM
UJJAL DHAR
This is you may try...

Code:

```for(int i = 0; i < list.length-1; i++)   for(int j = i+1; j < list.length; j++)     {         if(list[i]>list[j])           {             temp=list[i];             list[i]=list[j];             list[j]=temp;           }       }```
• 03-28-2011, 09:41 PM
doWhile
...and a perfect example of spoonfeeding
• 03-28-2011, 10:03 PM
Bgreen7887
Hi Guyz first off THANKS.. I took "DOwhile" suggestion. I fixed the loop and it corrected the problem. works fine now.