Disadvantage: After the first pass it may not be necessary to examine the
entire range of the array -- only from where the lowest exchange occurred
to where the highest exchange occurred. The parts above and below must
already be sorted. See below.

Bubble Sort -- Sort only necessary range

Here's a version of bubble sort that, on each pass, looks only at the
region of array where more exchanges might be necessary.

//========================================================= bubbleSortRange
// After a pass in bubble sort, it's only necessary to sort from just
// below the first exchange (small values may move lower)
// to just before the last exchange (largest values won't move higher).
// Everything that wasn't exchanged must be in the correct order.
// After each pass, the upper and lower bounds for the next pass are
// set from the positions of the first and last exchanges on prev pass.
void bubbleSortRange(int x[], int n) {
int lowerBound = 0; // First position to compare.
int upperBound = n; // First position NOT to compare.
//--- Continue making passes while there is a potential exchange.
while (lowerBound <= upperBound) {
int firstExchange = n; // assume impossibly high index for low end.
int lastExchange = -1; // assume impossibly low index for high end.
//--- Make a pass over the appropriate range.
for (int i=lowerBound; i<upperBound; i++) {
if (x[i] > x[i+1]) {
//--- exchange elements
int temp = x[i]; x[i] = x[i+1]; x[i+1] = temp;
//--- Remember first and last exchange indexes.
if (i<firstExchange) { // true only for first exchange.
firstExchange = i;
}
lastExchange = i;
}
}
//--- Prepare limits for next pass.
lowerBound = firstExchange-1;
if (lowerBound < 0) {
lowerBound = 0;
}
upperBound = lastExchange;
}
}//end bubbleSortRange

The text from the above example can be selected, copied, and pasted into an editor.

Disadvantage: Notice that the largest unsorted element will always be moved all the
way to its correct position in the array, but small elements are shifted down
by only one place on each pass.

Other bubble sorts

Note that in all these sorts the part that is sorted grows at only one end
of the array. The ability to quit early is not symmetrical. The extreme
values move all the way to the end in one direction, but only one place in
the other direction. The algorithm can be improved by alternately "bubbling" in
opposite directions.