The function is clearly trying to make sure that all elements that are less that $x$ are on the left hand side, and all elements greater than $x$ are on the right hand side.
To do this, it scans from left-to-right with the index $l$, looking for the index $A[l]$ where $A[l] > x$.
Similarly, it scans right-to-left with the index $r$, looking for a $A[r]$ such that $A[r] < x$
Knowing this, we can design the worst case input. For example, for an array of length $n$, the worst case wold be where:
- $l$ would have to look till $n- 1$ from $0 \to n - 1$
- $r$ would have to look till $0$ from $n - 1 \to 0$
Hence, an input maybe like
$$S = [x - 1, x, x, x, \ldots, x + 1]$$
For this input $S$, $l$ will have to go to the $n-1$th index to find the $x + 1$ element which is greater than $x$. This is one search of length $n$
By a similar argument, $r$ will have a search of length $n$ from $n-1 \to 0$ to find the $x - 1$
If we consider our basic operator to be comparisons $<$ and $>$ that are done in the loop:
- there are $n$ $<$ comparisons for $l$
- there are $n$ $>$ comparisons for $r$.
Hence, the total search for a list of length $n$ in $2n$, which is in $\Theta(n)$
Hence, the algorithm is in $\Theta(n)$