Hi, thank you for your reply! I’m a bit confused as to what you mean? BubbleSort “bubbles” the largest value of an array to the end and then you decrement the boundary index, or the “right” index. In worst case, you would do the following comparisons (pseudo-optimized):
[0,1] ; [1,2] ; [2, 3] ; [3,4] ; then [0,1] ; [1,2] ; [2,3] ; then [0,1] ; [1,2] ; then [0,1]
however, if within any of the groupings between my then statements we never swap any elements, then we can terminate the algorithm since the array is now already sorted. That’s how I implemented it and when I print the step-by-step comparisons, I get the following as a sample:
In this example, we terminate after the [26, 49] comparison since we haven’t swapped up to that point and we’ve reached the “right” boundary i.e. everything to the right of 49 is already sorted.
Ohhhh I think I understand what you mean! I’ve implemented an optimized version of this but the example project doesn’t want us to terminate prematurely and the lab simply wants to go to the end of the array in every pass.
