

quicksort(A,p,r) {
If p < r then {
q := partition(A,p,r)
quicksort(A,p,q-1)
quicksort(A,q+1,r)
}
}
partition(A,p,r) {
x := A[r]
i := p-1
For j := p to r-1 do {
If A[j] ≤ x then {
i := i+1
exchange A[i] with A[j]
}
}
exchange A[i+1] with A[r]
return i+1
}
During the iteration of the For loop in partition, the property that
is maintained (the invariance) is represented by the following picture:
p i j r
-------------------------------------
| | | | | | | | | | | | | | | | | | |x|
-------------------------------------
|...........|.........|.............|
≤ x > x not processed
yet
---------------
A |7|6|5|4|8|2|3|6|
---------------
[However, for your information, squaring matrices is not more efficient in general than multiplying two matrices A and B. If nxn matrices can be squared in time O(nc), then any two nxn matrices can be multiplied in time O(nc) as well.]