Otherwise, the values in A[a] and A[b] need to be swapped. Then reverse the values in between, A[a+1], … A[b−1]. Here is an illustration for a = 10 and b = 16. The first step is to reverse the first and last values, and the second step is the entire recursive call with a = 11 and b = 15.
|
→ |
|
→ |
|
Here is a definition of reverse using those ideas.
void reverse(int A[], const int a, const int b) { if(a < b - 1) { int t = A[a]; A[a] = A[b]; A[b] = t; reverse(A, a+1, b-1); } }