4 ms·
Not quite, your algorithm has j going from i+1 to n, and swapping when a[i] > a[j]. OP's algorithm has both indexes going from 1 to n, and swapping when a[i] <
by BoppreH 5y ago
Not quite, your algorithm has j going from i+1 to n, and swapping when a[i] > a[j].
OP's algorithm has both indexes going from 1 to n, and swapping when a[i] < a[j].
Yours is more efficient and easy to see why it's correct, but OP's is slightly simpler and more surprising.
- svat 5y agoOh yes, good point: I got carried away; there is a difference! Simplicity is subjective (I think mine is simpler of course :P), but the algorithm in the post is indeed more surprising.
- pricecomstock 5y agoYour algorithm does appear in Section 3.1 of the linked paper by the name "Exchange Sort"