i = first
Although last+1 may be out of bounds for array a, there is no error because a[last+1] is never accessed, because loc is decremented first before referencing a[loc].
In the partition procedure, we wish to have the elements less than or equal to the pivot toward the left and those greater than or equal toward the right.