Pointer a and b never changes - it is pointer c that does the work.
The values to be sorted are stored in an array/buffer.
pointer a is set to the start of the buffer and b to the last value in the buffer.
for(int*c=b,t;c>a;)
The loop ends when pointer c reaches pointer a - the only way for that to happen is when the
all the values have been sorted.
1 2
|
if(t=*c--,*c>t)
c[1]=*c,*c=t,c=b;
|
This takes the current value pointed at by c (stores it in t), moves c back by one, and compares the
current value with the previous one. If the current value is greater , the values are swapped, and pointer c is reset (c =b). If there is no need to swap, then pointer c jis not reset
In other words- say the buffer have 5 values 1 2 5 4 3.
The function is called with pointer a pointing to the 1 (start of buffer) and pointer b pointing to the 3 (end of the buffer).
pointer c is made to point to the same as b so c points to 3.
The value 4 is compared to the value 3. 4 is greater so they are swapped.
The buffer now looks like this
1 2 5 3 4.
Because a swap was made pointer c is reset to the end of the buffer and the loop runs again.
3 and 4 are compared - NO SWAP - pointer c is not NOT reset
5 and 3 are compared - SWAP - Buffer now looks like this 1 2 3 5 4. pointer c is reset.
5 and 4 are compared - SWAP - Buffer now looks like 1 2 3 4 5 - pointer c is reset.
Now you will see that this time round the loop, they will NO SWAP, pointer c will not be reset, and pointer c will continue to be decremented, and will reach pointer a and the loop will terminate (because
c > a will be false) - sort complete