Ookay,
1 for p := 1 to ⌊k/2⌋
means, we're going up to the half of the array.
2 t := ap
3 ap := ak−p+1
4 ak−p+1 := t
This pattern can be recognized as a "swap with temporary t
". And what is swapped?
Well, ap
and ak-p+1
, one being the p
-th element from the array start, the other one the p
-th one from the end.
So, to sum up:
You swap the n
-th first with the n
-th last array value up to the half of the array. And afterwards? The array is reversed.
Note that your pseudocode-format looks really weird - and, most importantly - ambiguous.
Is ak-p+1
equivalent to a[k-p+1]
or to a[k]-p+1
or a[k-p]+1
? If not, how did you express the other ones.
So at first, I'll convert this code to an actual source like Python's, which is much easier to read.
Edit.
I) Well, as you posted, the array ranges from a1
to ak
.
II) Think how you could swap the values of two variables (a
and b
):
1 temp := a
2 a := b
3 b := temp
Of course, since you overwrote a
with b
in line 2, you had to store the old a
value in a temporary, which is temp
.