Java Bubble Sort Programs

Normal Bubble Sort Program


Revised Bubble Sort Program


In case of sorting the array in ‘Ascending Order’ : The idea of bubble sort is to compare every element (i) with the ‘next’ element (i+1) in the array and bubble out the largest element to the ‘end’ of that array. The normal bubble sort helps us to do that, but there are some obvious improvements which can be done in it. Hence there is a need of revised bubble sort. The revised bubble sort helps to save us time by adding two main conditions.

The first improvement in bubble sort was to add a switched variable. To check whether the array is ‘already’ sorted or not. If the array is already sorted then we need not add anymore passes.

The second improvement was implemented by this part of the code n-pass-1. This filters out the ‘already’ bubbled out elements from the next passes. Example: If a element is bubbled out to the end of the array in n th pass.

This element wont be considered in the n+1 th pass.
Thus saving us a huge amount of time, especially if the array is quite big.