Does a comparison sort have to compare the A[i] largest and A[i+1] largest values? I think any comparison sort must, but I’m not sure. I’ve checked out mergesort, insertion sort, and quicksort and in each of them the A[i] largest and A[i+1] largest values have to be compared.
Does a comparison sort have to compare the A[i] largest and A[i+1] largest values?
Share
Every correct algorithm has to compare adjacent cells, unless they are equal. Proof: Assume otherwise. A[i] and A[i+1] in the final array have not been compared (A[i] < A[i+1). What happens if their positions are swapped in the original array? All the comparisons made by the algorithm give the same results as in the original run(*), therefore it executes the same permutation, therefore their final positions are swapped, so it makes the algorithm incorrect.
(*) This follows from the fact that A[i] and A[j] are adjacent.