1. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0

## BuBBle Sorting

Hey guy basic bubble sorting . I 've triple checked code and still cant find mistake. Output is 45.37,22,16,99
Java 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] + "");
}
}
}```

2. Moderator
Join Date
Jul 2010
Location
California
Posts
1,642
Rep Power
6
Java 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)

3. Member
Join Date
Mar 2011
Posts
94
Rep Power
0
Your other for loop isn't correct either:
Java Code:
`for(int k =0 ; k <list.length-1; k++ ){`
This loop is not counting the way it should be for bubblesort.

4. as doWhile is telling you, i > 0; not < 0.

5. Senior Member
Join Date
Apr 2010
Location
Posts
180
Rep Power
0
This is you may try...

Java 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;
}
}```

6. Moderator
Join Date
Jul 2010
Location
California
Posts
1,642
Rep Power
6
...and a perfect example of spoonfeeding

7. Senior Member
Join Date
Oct 2010
Location
Newark,nj
Posts
111
Rep Power
0
Hi Guyz first off THANKS.. I took "DOwhile" suggestion. I fixed the loop and it corrected the problem. works fine now.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•