What I try to do is to check a given array is ordered in an ascending manner by divide-and-conquer approach.
I wonder what the logic behind the additional return case (a⌊n/2⌋−1 ≤ a⌊n/2⌋) is to reach to the final result. I attempted to solve the problem without looking at its solution but I couldn't figure out how the author discovers/devises a⌊n/2⌋−1 ≤ a⌊n/2⌋ case. It is really hard for me to unearth the case.
Actually, Why not a⌊n/2⌋ ≤ a⌊n/2⌋+1 instead? And the base case case, why I'm stackoverflowed when I remove equality from the base case, that is h<l?
with trial and error approach, I tried to write the following.
def co(a, l, h):
if h <= l:
return True
mid = l + ((h-l)//2)
cl = co(a, l, mid-1)
rl = co(a, mid+1, h)
return rl and cl and a[mid] < a[mid+1]
# how is a[mid] < a[mid+1] devised ??
# why not a[mid-1] < a[mid] ??
#c = [3, 5, 7, 9, 11,12]
c = [3, 5]
print(co(c, 0, len(c) - 1))

Assume
c=[3, 5]. If you replaceh<=lwithh<l, then when you computeco(a, 1, 1), thenmid = 1+0... thenrl = co (a, 1+1, 1)anda[2]gives you stackoverflow.You need to compare the most-right element of subproblem1 with the most-left element of subproblem2. The order of these two elements are not taken into account in subproblem1 and subproblem2.
Be careful with Python indexing. 1) When you split the list into
a[l:mid-1]anda[mid+1,h], you leave outa[mid-1]anda[mid]. 2) When you writeco(c, 0, len(c) - 1)you leave out the last element ofc(see Comment4).There are some mistakes in your code, see my comments.
Below, I fixed the list indexing in your code. Note that the test becomes
h <= l+1because in Python the lista[mid:mid+1]contains one element.