Progressive data structure (algorithm)

Source: Internet
Author: User

1. Algorithms

is a description of the solution steps for a particular problem that behaves as a finite sequence of instructions in the computer, and each instruction represents one or more operations.

2. Comparison of algorithms

Here is a chestnut: summation algorithm comparison.

// General Summation int 0  - ;  for 1; I <= N; i++) {    + = i;} MessageBox.Show (sum. ToString ()); // Gaussian summation int 0  -  = (12; MessageBox.Show (sum. ToString ());

It can be seen that the first method executes n times internally for the loop, and the second method executes only 1 times.

Maybe n is 100, the performance difference between the two is very small, if this is 10000000000000000000000000000000? The results are conceivable.

3. Features of the algorithm
    1. Input and output: 0 or more inputs, one or more outputs;
    2. Poor: will automatically end without an infinite loop;
    3. Certainty: Every step has the definite meaning, does not appear the non-duality;
    4. Feasibility: Each step must be feasible, the implementation of a limited number of times to complete.
4. Requirements for algorithmic design
    1. Correctness
      • Algorithmic program with no syntax errors
      • The algorithm program can produce the output result satisfying the requirement for the valid input data.
      • The algorithm program is able to produce results that satisfy the specification for illegal input data.
      • The algorithm program for carefully selected, even difficult test data have to meet the requirements of the output results.
    2. Readability: Another purpose of the algorithm design is to facilitate reading, understanding and communication
    3. Robustness: When the input data is not valid, the algorithm can also do the relevant processing, rather than produce abnormal or inexplicable results
    4. High time efficiency and low storage capacity
      • Efficiency: For the same problem, if there are multiple algorithms can be solved, the execution time of the algorithm is high efficiency, long execution time is inefficient.
      • Storage: The reserve requirement refers to the maximum amount of storage space required during the execution of the algorithm, mainly refers to the memory or external hard disk storage space occupied by the algorithm when the program is running.
5. Method of measuring algorithm efficiency
    1. Afterwards statistics method: through the design good test procedure and the data, uses the computer counter to compare to the program running time which the different algorithm compiles, thus determines the algorithm efficiency high and low. (Many defects, not considered)
    2. Pre-analysis and estimation method: Before the computer program is compiled, the algorithm is estimated according to statistical method.

The time it takes for a program written in a high-level programming language to run on a computer depends on the following factors:

    1. The strategy, method (fundamental) used by the algorithm
    2. Code quality generated by compilation (software)
    3. The input size of the problem (that is, the amount of input)
    4. Speed of machine execution instructions (hardware performance)

Analyze the run time of an algorithm to correlate the number of basic operations with the input scale.

For example, the first method is a linear scale of N, the number of operations N, the second method regardless of the size of the value of N, the number of operations is 1, if you add a double loop on this basis, the number of operations N squared, their relationship as shown:

6. Progressive growth of functions

Given f (n), g (n), if there is an integer n, so that all N>n,f (n) is always greater than g (n), then the asymptotic growth of F (n) is faster than g (n);

Give me a chestnut:

F (N) G (N) N
2n+3 3n+1 2
4n+8 2n2+1
3

When n approaches infinity, we can recognize that some values don't affect our final judgment, and we can omit

    1. Can ignore addition Changshu
    2. Constants that are multiplied by the highest term can be ignored

Our main concern is the order of the highest order, the higher the exponent, the faster the growth.

7. Time complexity of the algorithm

T (n) =o (f (n)) indicates that the growth rate of the algorithm execution time and the growth rate of f (n) are the same as the increase of the problem size n, which is called the progressive time complexity of the algorithm, short of the time complexity. where f (n) is a function of the problem size n. (Large o notation).

In general, the algorithm with the slowest growth of T (n) is the optimal algorithm with the increase of N.

O (1) is called the constant Order, O (n) called the Linear Order, O (N2) called the square Order.

Derivation of the large O-order method (how to analyze the time complexity of the algorithm)

  1. Replace all the addition constants in the run time with constant 1.

2. In the modified run Count function, only the highest order is preserved.

3. If the highest order exists and is not 1, the constant multiplied by the item is removed.

  Give me a chestnut:

3n3+2n2+n+10 = O (n3+1)

log2n = O (Logn)

The time complexity that is commonly used is taken from small to large, in turn:

O (1) <o (LOGN) <o (n) <o (NLOGN) <o (n^2) <o (n^3) <o (2^n) <o (n!) <o (N^n)

8. Algorithmic spatial Complexity

S (n) =o (f (n)) n is the scale of the problem, and F (n) is the function of the statement about the storage space occupied by n

 

Progressive data structure (algorithm)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.