About algorithm complexity and algorithm complexity

Source: Internet
Author: User

About algorithm complexity and algorithm complexity
I. algorithm complexity

Algorithm complexity: The resources required for running an algorithm after it is compiled into an executable program. resources include time resources and memory resources.

The same problem can be solved by different algorithms, and the quality of an algorithm will affect the efficiency of the algorithm and even the program. Algorithm analysis aims to select appropriate algorithms and improve algorithms. The evaluation of an algorithm mainly involves time complexity and space complexity.

 

OK! Here, I think you should understand the complexity of the algorithm. If you are a computer science or mathematics professional, and you have a very good score, you do not need to continue reading. Because the author did not study computer science at all, he was not familiar with these profound knowledge after reading a lot of information, and he could not explain the truth to everyone. But I don't want to copy or paste anything on Baidu. Therefore, I think that the vast majority of the readers here are understandable. I think it's boring to put the facts and make sense. If you can use examples from the real world to illustrate this, it would be even better. Of course, if the author has the opportunity, he will make a good effort to figure out what he is talking about here. I think this is also in line with the mentality of every prosperous year.

 

1. Time Complexity

By viewing Baidu encyclopedia, you can get a general idea of time complexity. There are also abstract letter expressions. I will not copy or paste those items here. It is better to use a more concise and easy-to-understand example to illustrate the general meaning here. In the previous article, it seems like a blog by a university teacher, I saw a very interesting explanation.

It is assumed that the CPU speed has increased by 10 in just a few years. Although there is Moore's Law, it is exaggerated. However, one of our algorithms can write an O (n) algorithm with a time complexity, but it is written into an O (n ^ 2) program, which is easy to think of and write. That is, in the O (n ^ 2) time complexity program, the speed is actually only increased by 10 (root 100 = 10) times. For O (n) time complexity algorithms, that is 100 times true. That is to say, an old-fashioned CPU computer runs an O (n) program and a new CPU runs an O (n ^ 2) program with a speed increase of 100 times, the ultimate winner of high efficiency is an old-fashioned CPU computer. The reason is that the advantages and disadvantages of algorithms directly determine the program running efficiency. It can be seen that the time complexity is a very important method to measure the algorithm quickly and estimate it in advance.

The time complexity calculation method is not described in detail. Let's talk about it here.Worst caseAndAverageTime complexity. When you get up in the morning and get ready to go out for dinner, you suddenly get up under the dormitory and forget to bring your cell phone. In these years, the key, wallet, and mobile phone are nothing less. Go back to the dormitory, open the door, and put your hands on the windowsill. Of course, this is better, and it takes little time. However, if you are not there, you will find it. I may have rummaged through my desk and closet. When I found it, it was almost 8 o'clock. I had to go to class in a hurry and had a good breakfast. When we find things with good luck, we can't find anything. But in reality, most of the things we encounter are neither the best nor the worst, therefore, the average condition is mostly.

This is also true for algorithm analysis. Search for a number in n random numbers,The best case is that the first number is that the time complexity isO (1), IfThe last number is what we are looking for, so the time complexity isO (n)This is the worst case. WhileThe average running time is from the perspective of probability. If a number may appear at each position, the average number of searches is n/2..

The average running time is the most meaningful in all cases because it is the expected running time. In reality, the average running time is hard to be obtained through analysis. It is generally estimated by running a certain amount of experiment data. The worst running time is a guarantee that the running time will not break down. This is the most important requirement in applications,Generally, unless otherwise specified, the running time we mentioned is the worst-case running time. That is, the time complexity is the worst case.

 

The time complexity of common algorithms ranges from small to large:

O (1) <O (log2n) <O (n log2 n) <O (n ^ 2) <O (n ^ 3) <O (2 ^ n)

 

 

2. spatial complexity

The concept of space complexity is much simpler. Simply put, it is the memory resources occupied when the algorithm is executed. Apart from professional terms and computational methods, I really don't know how to say more in the vernacular.

 

It is worth mentioning:

For an algorithm, its time complexity and space complexity often affect each other. When pursuing a better time complexity, the performance of the space complexity may deteriorate, that is, it may lead to a large amount of storage space. Otherwise, when pursuing a better space complexity, it may degrade the performance of time complexity, that is, it may lead to a long running time.

It seems that there are many solutions for solving a problem. Here there are many algorithms. When selecting an algorithm, we should analyze the cost and benefits of this algorithm. Because each algorithm has its own characteristics, if time is saved, it may waste space. And vice versa.

 

Afterwards:

After the baptism of several simple algorithms, I suddenly found that the previous work was too simple. It doesn't mean that the previous things have no value, but from the perspective of the Technical career, normal programming may soon encounter a bottleneck. But after studying several algorithms. It seems that we have seen a longer-term path. Compared with the code accumulation, this optimization effect achieved through careful design is more interesting and challenging. As a result, I feel that there are too many things to learn, not only the accumulation of experience, but also the breakthrough of intelligence and thinking.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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.