How to write algorithm?
CodePudding user response:
The most primitive way, with two global variables a, b,In each cycle and branch statements to increment of variables,
In order to find, for example, when a loop, to +,
Such as binary search (if your binary search is a recursive algorithm, then put a b++ line in function directly, if it is iterative algorithm, the cycle of each if the else inside put a b++),
===================================
Or you can pass a pointer or reference to replace a global variable, as a function of parameters to the corresponding search function,
The basic train of thought that is
===================================
In addition, this calculation times the sum of the meaning is not big, actually use complexity in terms of analysis, for orderly sequences, the efficiency of the binary search for o (logn), and sequential search is o (n), apparently binary search better, of course, the binary search, only commonly used in orderly sequence ,