Redundant If Else

Share Button

Hello,

In this tutorial, I am going to highlight a tiny but important detail about using redundant IF ELSE control. Using unnecessary if else controls would probably affect performance of the program. We are going to write two versions of binary search. Even for a small data size, there is a considerable amount of difference between performances.

You can find many resources on the internet about binary search.

You can check the following video. Binary search just works with ordered lists.

Version1:

Version2(The difference from version1 is removing else part in while loop. Move mid = (low + high) / 2 operation to the end of while loop. Put one extra mid = (low + high) / 2 statement before while loop.)

main.c (Finding 768463 between 0..2000000)

Compile with GCC and run:

Source: Second Edition The C Programming Language (ANSI C version) by Brian W. Kernighan, Dennis M. Ritchie (Ex: Cp 3: P:1)