void indexsort( vector<int> & data) { int i, n = data.size(); for (i = 0; i < n; i++) { while(data[i]-1 != i) swap(data, i, data[i]-1); } }
Here are the steps for potential function analysis:
For this problem, the operations are the execution of the swap call or the increment of the index i if no swap is needed.
Here, is the indicator function: one if e is true, and
zero otherwise.
So, measures the distance of i from n plus the
number of items still unsorted.
Note that the potential function need not be computable quickly. It's mainly a mathematical definition.
Here, At and it means the array and index after the tth operation.
In this case, there are two operations: Swapping and incrementing i. We swap if A[i] is out of place. Then, ct = 1, and
If we increment i, then ct = 1, and