The best case is Theta (n), for example, for a sorted array. In the worst case, Theta (n ^ 2 log n).
Upper bound
Secondary subtasks have a sorted array that is preceded or followed by an arbitrary element. This is O (n log n). If we do O (n) before, solve the secondary subtask in the first half, and then in the second half, and then do O (n) more work - O (n log n). If this succeeds, perform operation O (n), sort the already sorted first half (O (n)), enable the second subtask in the second half, execute O (n), enable the secondary subtask in the first half, sort the already sorted second half ( O (n)), do O (n) work - O (n log n).
Now, in the general case, we solve two main subtasks in two halves, and then slowly exchange elements along the rotation axis using secondary calls. O (n) exchanges are required, therefore, a direct application of Mat. The theorem gives an estimate of O (n ^ 2 log n).
Bottom line
For k> = 3, we construct an array A (k) of size 2 ^ k, recursive, using the above analysis as a guide. Bad cases are the arrays [2 ^ k + 1] + A (k).
Let A (3) = [1, ..., 8]. This sorted base register does not allow Reverse
be called.
For k> 3, let A (k) = [2 ^ (k-1) + A (k-1) [1], ..., 2 ^ (k-1) + A (k-1) [2 ^ (k-1)]] + A (k-1). Note that the primary subproblems [2 ^ k + 1] + A (k) are equivalent to [2 ^ (k-1) + 1] + A (k-1).
After the initial recursive calls, the array is [2 ^ (k-1) + 1, ..., 2 ^ k, 1, ..., 2 ^ (k-1), 2 ^ k + 1]. There are Omega (2 ^ k) elements that must move Omega (2 ^ k) positions, and each of the secondary invocations that moves the element so far has O (1) sorted subtasks and therefore Omega (n log n) .
Obviously, more coffee is needed - the primary subtasks are irrelevant. This is not so bad for analyzing the average case , which is Theta (n ^ 2 log n) .
With constant probability, the first half of the array contains at least half the smallest quartile and at least half the largest quartile. In this case, regardless of whether Reverse
exists, there are Omega (n) elements that must move Omega (n) positions through secondary calls.