Skip to main content
Code Review

Return to Answer

added 17 characters in body
Source Link
Konrad Rudolph
  • 6.7k
  • 23
  • 35

Since nobody has mentioned it:

Your code contains a famous bug. The following code may overflow the int range for large arrays:

int i = (high + low) / 2;

To avoid this, use the following:

int i = low + (high - low) / 2;

Note that simply using a different data type isn’t enough — it just postpones the bug to larger arrays.

Another, unrelated issue relates to code readability: in the parameter list to binarySearch, int arr[] looks like an array declaration but (because of C++ rules), this is actually a pointer, i.e. it’s equivalent to writing int* arr. Using the int arr[] syntax is potentially misleading, and therefore best avoided.

To further improve readability, use a different name for i (what does i stand for?). mid is frequently used here.

Since nobody has mentioned it:

Your code contains a famous bug. The following code may overflow the int range:

int i = (high + low) / 2;

To avoid this, use the following:

int i = low + (high - low) / 2;

Note that simply using a different data type isn’t enough — it just postpones the bug to larger arrays.

Another, unrelated issue relates to code readability: in the parameter list to binarySearch, int arr[] looks like an array declaration but (because of C++ rules), this is actually a pointer, i.e. it’s equivalent to writing int* arr. Using the int arr[] syntax is potentially misleading, and therefore best avoided.

To further improve readability, use a different name for i (what does i stand for?). mid is frequently used here.

Since nobody has mentioned it:

Your code contains a famous bug. The following code may overflow the int range for large arrays:

int i = (high + low) / 2;

To avoid this, use the following:

int i = low + (high - low) / 2;

Note that simply using a different data type isn’t enough — it just postpones the bug to larger arrays.

Another, unrelated issue relates to code readability: in the parameter list to binarySearch, int arr[] looks like an array declaration but (because of C++ rules), this is actually a pointer, i.e. it’s equivalent to writing int* arr. Using the int arr[] syntax is potentially misleading, and therefore best avoided.

To further improve readability, use a different name for i (what does i stand for?). mid is frequently used here.

Source Link
Konrad Rudolph
  • 6.7k
  • 23
  • 35

Since nobody has mentioned it:

Your code contains a famous bug. The following code may overflow the int range:

int i = (high + low) / 2;

To avoid this, use the following:

int i = low + (high - low) / 2;

Note that simply using a different data type isn’t enough — it just postpones the bug to larger arrays.

Another, unrelated issue relates to code readability: in the parameter list to binarySearch, int arr[] looks like an array declaration but (because of C++ rules), this is actually a pointer, i.e. it’s equivalent to writing int* arr. Using the int arr[] syntax is potentially misleading, and therefore best avoided.

To further improve readability, use a different name for i (what does i stand for?). mid is frequently used here.

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /