Read data structure (C) (2)

Source: Internet
Author: User

This section describes algorithm analysis and Big O estimation (big-O Notation ). The measurement of algorithm efficiency generally uses the method of analyzing and estimating beforehand. The common practice is to "select a problem (or algorithm type) from the algorithm) it is the original operation of the basic operation, and the number of times that the basic operation repeats is used as the time measure of the algorithm ". When talking about this, the author introduces the large O estimation method.

In this book, the author's introduction to the big O estimation method seems a little hasty. At the beginning, a sub-Statement t (n) = O (N3) emerged. Then, at the bottom of this page, a small word is used to introduce the so-called "O" format definition ": If F (N) is a function of positive integer N, then xn = O (f (N) indicates that there is a positive constant M, when N is greater than or equal to N0, | xn | ≤m | f (n) | is met. Maybe I have a poor mathematical foundation. In short, I am confused when I see this definition. I don't know why I didn't spend a little time introducing the origins and definitions of the big O estimation method. I Googled and found the following introduction:

Definition:A theoretical measure of the execution ofAlgorithm, Usually the time or memory needed, given the problem size N, which is usually the number of items. informally, saying some equation f (n) = O (G (N) means it is less than some constant multiple of g (n ). the notation is read, "F of N is big oh of G of N ".

Formal Definition:F (n) = O (G (N) means there are positive constants c and K, such that 0 ≤ F (n) ≤ CG (n) for all n ≥ K. the values of C and K must be fixed for the function f and must not depend on N.

 

Note: As an example, n² + 3N + 4 is O (n²), since n² + 3N + 4 <2n for all N> 10. strictly speaking, 3N + 4 is O (n²), too, but big-O notation is often misused to mean equal to rather than less. the notion of "equal to" is expressed by minute (n ).

The importance of this measure can be seen in trying to decide whether an algorithm is adequate, but may just need a better implementation, or the algorithm will always be too slow on a big enough input. for instance, quicksort, Which is O (n log n) on average, running on a small desktop computer can beat bubble sort, which is O (n² ), running on a supercomputer if there are a lot of numbers to sort. To sort 1,000,000 numbers, the quicksort takes 20,000,000 steps on average, while the bubble sort takes 1,000,000,000,000 steps!

Any measure of execution must implicitly or explicitly refer to some computation model. usually this is some notion of the limiting factor. for one problem or machine, the number of floating point multiplications may be the limiting factor, while for another, it may be the number of messages passed transmission ss a network. other measures which may be important are compares, item moves, disk accesses, memory used, or elapsed ("Wall Clock") time.

(The above is from Paul E. Black, "big-O notation", from Dictionary of algorithms and data structures, Paul E. Black, ed., NIST .)

In addition, this post also discussed the algorithm's time complexity estimation, which is very easy to understand.

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.