Recursion

Dec 11, 2021 at 5:02pm
l is lower-bound. r is upper bound for the array part to search. This is binary search by recursion for a sorted array.
Dec 11, 2021 at 7:30pm
The naming of the variables/function parameters is really bad. If the person who wrote this code had bothered with creating more descriptive names the code could have self-documented what was going on.

string rec(int lower_bounds, int upper_bounds, int arr[], int arr_size) for example.

Using single letter variable names in a for loop is acceptable, because the scope is more restrictive.

It also has historical reasons.

"It's always been done that way" shouldn't be an excuse for not self-documenting code with good explanatory variable names.
Dec 12, 2021 at 10:43am
He's used l for left and r for right - taking left as lower bound and right for upper bound. m is for middle.

Dec 12, 2021 at 2:14pm
> int m = (l+r)/2;
This should be written as
int m = l + (r-l)/2;
The former is basically the world's oldest overflow bug waiting to happen.
Having arrays of billions of items is easy to do now, especially on 64-bit architectures.
So it's all too easy to end up with an array that would overflow integer arithmetic.

> int a[n];
This isn't valid C++.
Use new[] or a std::vector.
Last edited on Dec 12, 2021 at 2:15pm
Dec 12, 2021 at 5:21pm
:) :) :)
Topic archived. No new replies allowed.