Each correct algorithm must compare neighboring cells if they are not equal. Evidence. Assume otherwise. A [i] and A [i + 1] in the final array were not compared (A [i] <A [i + 1). What happens if their positions are replaced by the original array? All comparisons made by the algorithm give the same results as in the original run (*), therefore it performs the same permutation, therefore their end positions are swapped, which makes the algorithm incorrect.
(*) This follows from the fact that A [i] and A [j] are adjacent.
source
share