(C #) data structure and algorithm analysis-runtime computing

Source: Internet
Author: User
When comparing the efficiency of an algorithm, it is often a comparison between the algorithm running time and the number of data items. For example, to get a result, which algorithm runs the fastest and most efficient under the same data volume, which algorithm is the fastest time to change the data volume ......

Large O notation
When describing the algorithm running time, we can often see O (N), O (logn), etc. The o in Large O means "order of" (approximately ), it is a concept, such as a large car, a small car and a medium-sized car, ignoring the specific size and size to describe the car.

First, let me analyze a piece of code:
Example 1:

1 int sum (int n)
2 {
3 int partialsum;
4 partialsum = 0;
5 For (INT I = 1; I <= N; I ++)
6 {
7 partialsum + = I * I;
8}
9 return partialsum;
10}

This program is used to calculate the integer Σ ni = 1i3, that is, from 1 to n, and calculate their cube and

The Declaration does not take time, and the assignment, calculation, and comparison take up one time unit each time. Then, starting from the fourth row, the statement first takes one time unit, then enters the loop, and assigns a value to I, once again, it only runs once, then compare, run N + once, I ++ is two computations, one plus one, one assignment, and it needs to run n times, the time unit is 2n, and the cycle subject is four time units (two multiplication, one addition, and one value assignment). The total execution is n times, so it takes 4 N Units, the unit of time is returned.

In the end, the total cost of this program is 1 (Value assignment) + 1 (Value assignment) + (1 + 2n) (comparison) + 4n (calculation) + 1 (return) = 6N + 4 time units. However, if every program is computed like this, it would not crash. The purpose of the big O notation is to remove all constants and classify them into CPUs, the time caused by factors such as the platform. If the total number of constants is K, the time consumed by the above program is n + K. According to this calculation method, the running time of each program is converted to xn + k, so we can remove K.

Finally, in big O notation, the program runs at O (n ).

What if it is a nested loop, such:
Example 2:

For (INT I = 0; I <n; I ++)
{
For (Int J = 0; j <I; j ++)
{
Code;
}
}

The running time of this Code is O (n2) (you can refer to the above method ).

If instance 2 is added to the Return Statement in Example 1, the running time is n + N2, and the O (n2) is the general o representation ).

As you can see, the big O notation does not have a plus sign, and all these are ignored. There is no constant and only the number of iterations.

Many students with poor math, like me, will be confused when they encounter log numbers such as O (logn). In fact, it is not difficult to take binary search as an example, its running time is O (logn ).

What is binary search? Let me give you an example. Brother LI Yong asked you to guess the price of an electric appliance. if you guess it, it will be yours. If you are smart, you won't guess it, first, you will randomly guess the price that you think is suitable for (for example, a TV, you guess 5000). If he calls low, he will continue to add, but he calls high, you will choose half of the price (2500). If he shouted high, he will continue to fold half (1250). This time, when he shouted low, you would guess 3750. According to this principle, you finally guessed it. It's 3750. You only used it three times.

In this method, in the worst case (that is, the maximum number of guesses), the number of times N is used x <= log2n, because 2x <= N, such as 10, A maximum of three guesses, namely 23 <= 10, 3 <= log210.

Now we have a general understanding of the running time in log format.

Here are some running time calculation rules, which are extracted from data structure and algorithm analysis C ++ description (Third edition)

Rule 1: the running time of a for loop is at most the running time of the statements (including tests) in the for loop multiplied by the number of iterations.

Rule 2: nested loops analyze these loops from the inside out. The total run time of a statement in a group of nested loops is the time that the statement runs multiplied by the product of the size of all cycles in the group.

Rule 3: The sequential statement can sum the running time of each statement (this means that the maximum value is the running time ).

Rule 4: If/else statements: the running time of an IF/else statement never exceeds the Judgment time, and the total running time of each subject with a longer running time.

Finally, to sum up, the running time O is the relative growth rate. The running time we compare is to compare their relative growth rate, the results are obtained in the worst case, because the data volume is full of uncertainty and is not conservatively estimated, it will lead to errors and understand the running time algorithm, there is a benchmark for determining the quality of an algorithm. It will be used in the future (especially for sorting algorithms ).

What I understand is likely to be wrong. I hope that the experts can correct the question and refuse to comment. After all, I do not know high numbers. This design has reached the limit.

PS: At that time, the sorting algorithm only taught bubble, and even the quick sorting did not know what it was. Now, the sorting is full of bubble.
Fortunately, I have learned a lot about sorting algorithms, and each of them has its own application scenarios. I will discuss sorting in my future notes. I really hope my classmates can take a look at these notes.

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.