simple sorting then setting the second cell to the temporary value actuallyusing separate swap(method may not be good idea in practice because the function call adds small amount of overhead if you're writing your own sorting routineyou may prefer to put the swap instructions in line to gain slight increase in speed invariants in many algorithms there are conditions that remain unchanged as the algorithm proceeds these conditions are called invariants recognizing invariants can be useful in understanding the algorithm in certain situations they may also be helpful in debuggingyou can repeatedly check that the invariant is trueand signal an error if it isn' in the bubblesort java programthe invariant is that the data items to the right of out are sorted this remains true throughout the running of the algorithm (on the first passnothing has been sorted yetand there are no items to the right of out because it starts on the rightmost element efficiency of the bubble sort as you can see by watching the bubblesort workshop applet with barsthe inner and inner+ arrows make nine comparisons on the first passeight on the secondand so ondown to one comparison on the last pass for itemsthis is in generalwhere is the number of items in the arraythere are - comparisons on the first passn- on the secondand so on the formula for the sum of such series is ( - ( - ( - *( - )/ *( - )/ is ( * / when is thusthe algorithm makes about / comparisons (ignoring the - which doesn' make much differenceespecially if is large there are fewer swaps than there are comparisons because two bars are swapped only if they need to be if the data is randoma swap is necessary about half the timeso there will be about / swaps (although in the worst casewith the initial data inversely sorteda swap is necessary with every comparison both swaps and comparisons are proportional to because constants don' count in big notationwe can ignore the and the and say that the bubble sort runs in ( time this is slowas you can verify by running the bubblesort workshop applet with bars