3 ms·
> whether swap() is used or not depends on the algorithm I didn't mean the std::swap() function itself, but the swapping of two elements a[i] and a[j]. Is ther
by XCSme 4y ago
> whether swap() is used or not depends on the algorithm
I didn't mean the std::swap() function itself, but the swapping of two elements a[i] and a[j]. Is there any sorting algorithm that doesn't rely on swapping two elements (or two parts of the array)? I guess, only if the sort is not being done in-place and the result is stored in a different variable/memory.
> does not necessary look like the elements were swapped
The only way elements don't look like they are swapped is elements are changed, added or removed, which doesn't happen during sort.
- Koshkin 4y agoThere are in-place algorithms that use shifting rather than swapping (the so called in-place merge sort, for instance). If you look at the result and at the original, generally you will not find many pairs in which the elements exchanged their positions. It takes special initial arrangements for this to be true for all elements.