Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Take you step by step to experience the charm of algorithms.

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

What is an algorithm?

An algorithm is a series of execution steps used to solve a particular problem

In other words: a geometry problem, demand certificate. There may be many positive solution methods, but as long as we can solve the verification problem, we can call this positive solution method an algorithm. Of course, the algorithm is much more than that.

These two pieces of code can be called algorithms because they can solve the problem of adding two numbers and adding from 1 to n, respectively. The algorithm does not have to be very complex, as small as one line of code, up to tens of thousands of lines of code, as long as it can solve a specific problem, it is the algorithm.

How to evaluate the quality of the algorithm

Using different algorithms to solve the same problem, the efficiency may be very different.

For example, the geometric verification mentioned earlier, although all can be done, the method may not be the best.

Such as:

Two existing algorithms for calculating Fibonacci number (fibonacci number)

Fibonacci series: 1 1 2 3 5 8. )

Here

Public static int fib1 (int n) {if (no (1))

​ $2n+6 $> > O (n)

​ $n ^ 2 + 2n+6 $> > O ($n ^ 2 $)

​ $4N ^ 3 + 3n ^ 2 + 22n+100 $> > O ($n ^ 3 $)

(2.2) the base of logarithmic order is generally omitted

​ $log_2n=log_29+log_9n$ (logarithms of any base can be converted to each other by multiplying a constant)

​ so $log_2n$ and $log_9n$ are collectively referred to as $logn$

Note: the large O representation is only a rough analytical model and an estimate, which can help us to understand the execution efficiency of an algorithm in a short time.

Calculate the time complexity of the following code

Java

Public static void test1 (int n) {

/ / 1 (perform a judgment operation)

If (n > 10) {

System.out.println ("n > 10")

} else if (n > 5) {/ / 2

System.out.println ("n > 5")

} else {

System.out.println ("n

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report