Algorithm and Data Structure
Algorithms and data structures-inextricably linked
Looking at various algorithm books, most of them talk about algorithms and data structures as a whole.
The data structure is the structure that stores or represents object data, such as arrays and tree structures.
The algorithm and data structure are described as a whole because the data structure must be selected according to common operations in the algorithm. For example, if you store data in an appropriate tree structure in advance, searching in most cases becomes simple and can reduce complexity.
As we can see in Lesson 11th, the implementation of RDBMS indexes adopts the tree structure of B + tree. The B + tree is a tree structure suitable for external storage. Using the B + tree to save indexes not only reduces the operation steps required for searching, but also minimizes the number of disk reads. Therefore, RDBMS indexes generally use B + trees, and use algorithms suitable for this data structure for search, insertion, sorting, and other operations.
Therefore, algorithms and data structures are inextricably linked.
Algorithm and Data Structure
Data Structure
Array, tree structure, heap ......
Select based on common algorithm operations
Select a data structure based on common algorithm operations
Complexity and constant -- Evaluation is very important
The computation complexity method ignores all "constant items ". The so-called constant term is a type of processing that does not depend on the input size but has to be executed in algorithm implementation.
For example, function calls, function return, and other processing are constant items. The first allocation of variables, if statement branches, and so on are also constant items. In simple implementation, constant terms almost do not affect the complexity of the algorithm, but in complex implementation, constant terms cannot be ignored. Even if the implementation is not complex, whether the CPU cache is easy to take effect, whether the branch prediction occurs, and other computer structure features will also affect, so constant items may lead to gaps.
For example, if the theoretical lower limit of a sorting algorithm is O (n log n), the average complexity of several algorithms can reach O (n log n ). However, for O (n log n), quick sorting is generally the fastest. The fast sorting feature makes the CPU cache easy to take effect, which is much better than other algorithms. This is an example of a small constant.
That is to say, the complexity LOG method is suitable for comparing algorithms, but it should not only consider complexity in implementation. And the constant term often depends on the implementation method. Therefore, we should try our best to reduce the constant term during implementation.
Optimization issues to be aware of during implementation
On the other hand, it is expected that when we implement something (not limited to algorithms), constant terms are optimized from the very beginning, which is basically incorrect. It is better to reduce the constant term of an algorithm whose complexity is O (n2) than to use an O (n log n) algorithm.
To put it bluntly, it is still "the most important evaluation ". Through evaluation (benchmark) or analysis (profiling), it is most important to find out the problem of the current program correctly. Do you need to change the algorithm to improve the performance, reduce the constant term to improve the performance, or replace the hardware with insufficient physical resources? Be sure to try to improve the problem after finding out the problem.
This article is excerpted from the book large-scale Web service development technology.
Book details: http://blog.csdn.net/broadview2006/article/details/6695616