First we assume that all the array elements are distinct. We will be given a sequence of n distinct items from an ordered set. For definiteness let us assume we are sorting integers. We will assume that each of these is equally likely to be input. In bubble sort, each element, starting with element 0, is swapped with its neighbor if the two are not in the proper order.
Bubble sort is easy to program and so is a useful sorting algorithm for small arrays.
If any element is out of order, then there will be at least one swap done in the inner loop. The modification uses this fact by stopping the sorting process when no element is moved in one pass. Give an example of an array of n integers for which this worst case time is achieved.