Sorting Algorithms

# Java Bubble Sort Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

• Anonymous says:

thanks for the example

• Danish says:

I just want to know, if i want to print all the steps of sorting then what should i have to add in the above code? hope you understand the question? i need to show all the steps of sorting one by one… thanks Please reply as soon as possible.

• Gaurab Pradhan says:

i think in line 60 it must be like this for(int j=1; j < n-i-1; j++){

• Iulian says:

at line 60 it must be for(int j = 1; j < n; j++) because if you let the example as it is now you`ll get “5 35 45 90 150 3”

• johnny arcala says:

tnx for the Example ^_^

• johnny arcala says:

@google-c61175b004f9f920279c201183142df4:disqus i think its the same output hahahah

• Hamzeen. H. says:

Thanks for the example. I think line 60 should be as follows..
for(int j = 1; j < n; j++) {

if not the last element of the array will not get sorted properly specially if the last element is smaller than the previous ones on the array. I think lulian also makes the same point. Thanks.

• for loop of j must need to start with 0..
for(int j=0;j

• yousuf fahim says:

(n-i) is fine in line number 60 because even if the last number is the smallest number it will come to the first index of the array when i=0.

• SealsRGnarly says:

Thanks for the gnarly code.