two functions—easyExchange, where the new smallest
element x < L[i], 0 i < n replaces the largest element
L[m] and returns L[m]; hardExchange is identical
to easy exchange, but x can be any number. This paper
defines an extra function normalize that transform the
rotated list to a sorted array.
As described in (Frederickson 1983), easy exchange
can be done in O(1) operations once L[m] is found, as the
operation only needs to replace L[m] with the new smallest
element x. Array L still satisfies as a rotated list, but
the position m0 of the new largest element L[m0] is leftcircular-
shifted by one (m0 = m - 1, or m0 = n - 1 if
m = 0). Hard exchange is O(n) since it needs to shift
all the elements larger than x in the worst case. Figure 1
shows easy exchange and hard exchange examples on a
rotated list.
Normalization can be done in O(n) time, an obvious
way to achieve this is by having a temporary duplicate but
the exact bound can also be achieved in-place recursively
by using Algorithm 1, which has exactly optimal 2n words
read and 2n words write for the array L. The same algorithm
can also be done iteratively.