Step by step take you to experience the charm of Algorithms

What is an algorithm?

Implementation of the algorithm is a series of steps to resolve a particular problem for
other words: a geometry question, demand certificate. There may be a lot of positive solution method, but as long as the problem can be solved confirmation, we can put this method is called positive solution algorithm. Of course, the algorithm is much more than that.

Step by step take you to experience the charm of Algorithms
This algorithm can be called two codes, respectively, can solve the problem because the two numbers together, and n is from 1 added. Algorithm does not have to be very complex, small line of code, and more to tens of thousands of lines of code, as long as solve a specific problem that algorithm.

How to assess the merits of the algorithm

Using different algorithms to solve the same problem, efficiency may be very much different
to verify geometry such as said before, we can do it though, but not necessarily the best method.

Such as:
conventional two requirements Fibonacei (fibonacci number) algorithm

(Fibonacci number: 112358 ......)
where

public static int fib1(int n) {
    if (n <= 1) return n;
    return fib1(n - 1) + fib1(n - 2);
}
public static int fib2(int n) {
    if (n <= 1) return n;

    int first = 0;
    int second = 1;
    for (int i = 0; i < n - 1; i++) {
        int sum = first + second;
        first = second;
        second = sum;
    }
    return second;
}

Which of these two algorithms better it?

If a single assessment of the implementation of efficiency might expect such a scheme

Execution processing time to compare different algorithms for the same set of input

This program is also known as: ex post statistics

Our approach is:

public static void main(String[] args) {
    int n = 45;//求第45个斐波那契数

    TimeTool.check("fib1", new Task() {
        public void execute() {
            System.out.println(fib1(n));
        }
    });//5.815秒

    TimeTool.check("fib2", new Task() {
        public void execute() {
            System.out.println(fib2(n));
        }
    });//0.0秒
}

The above-described embodiment has obvious disadvantages

The execution time is heavily dependent on the hardware and run a variety of environmental factors of uncertainty

You must write the code corresponding estimates

Selecting test data is more difficult to ensure the fairness of (for 100 n = a first algorithm may be shorter, the second algorithm may be shorter when n = 200)

Generally used to evaluate the algorithm from the following dimensions

Correctness, readability, robustness (against unreasonable input response capability and processing power)

Time complexity (time complexity): the estimated number of execution program instructions (execution time)

Space complexity (space complexity): estimate the required storage space occupied

We evaluate this program the math 1 + 2 + ... + n algorithm

Step by step take you to experience the charm of Algorithms
The second algorithm is obviously better. Is it because the second method code is shorter it? Examples of Fibonacci number has not told us the code as short as possible. In this case the second algorithm only requires a three-step operation can solve the problem, but first need to loop n times. First of all are met correctness, readability, robustness criteria, and then from time complexity is concerned, assuming certain, we examine the operation of step execution time it takes roughly how many times the instruction is executed, you can compare the two algorithms the length of time; and from space complexity considerations, the need of the less variable, the smaller open storage space, better algorithms.

Big O notation

Big O notation is generally used to describe the complexity, it represents the size complexity data corresponding to n

Procedure:

(1) estimate the time complexity / space complexity (mostly time complexity)

(2.1) ignoring constants, coefficients, lower-order

         $9$>> O(1) 

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

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

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

(2.2) of the order in base commonly abbreviated

         $log_2n=log_29+log_9n$ (任意底数的对数可通过乘以一个常数相互转化)

         所以 $log_2n$、$log_9n$ 统称为 $logn$

Note: Big O notation is merely a rough analysis model is an estimate that can help us a short time to understand the efficiency of the algorithm

Calculates the following paragraphs the time complexity of the code

java
public static void test1(int n) {
//1(进行一次判断操作)
if (n > 10) {
System.out.println("n > 10");
} else if (n > 5) { // 2
System.out.println("n > 5");
} else {
System.out.println("n <= 5");
}
// 1(定义一次i) + 4(i累加四次) + 4(判断i<4四次) + 4(循环体一条语句执行四次)=9
for (int i = 0; i < 4; i++) {
System.out.println("test");
}
// 大O表示法时间复杂度O(1)
}

java
public static void test2(int n) {
// 1(定义一次i)+ 3n(i累加n次+判断i<n n次+循环体一条语句执行n次)=1+3n
for (int i = 0; i < n; i++) {
System.out.println("test");
}
// 大O表示法时间复杂度O(n)
}

java
public static void test3(int n) {
// 1(定义一次i) + 2n(i累加n次+判断i<n n次) + n(外层循环体语句执行n次) * (1(定义一次j) + 3n(j累加n次+判断j<n n次+内层循环体一条语句执行n次))=3n^2 + 3n + 1
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.println("test");
}
}
// 大O表示法时间复杂度O(n^2)
}

Java
public static void test4(int n) {
// 8 = 2^3
// 16 = 2^4

// 3 = log2(8)
// 4 = log2(16)

// 执行次数 = log2(n)
while ((n = n / 2) > 0) {
    System.out.println("test");
}
// 大O表示法时间复杂度O(logn)

}

java
public static void test5(int n) {
// log5(n)
while ((n = n / 5) > 0) {
System.out.println("test");
}
// 大O表示法时间复杂度O(logn)
}

java
public static void test7(int n) {
// 1(定义一次i) + 2log2(n)(i2运算次数) + log2(n)(外层循环执行次数) (1 + 3n)(内层循环执行次数)
for (int i = 1; i < n; i = i
2) {
// 1 + 3n
for (int j = 0; j < n; j++) {
System.out.println("test");
}
}
// 1 + 3log2(n) + 2 nlog2(n)
// 大O表示法时间复杂度O(nlogn)
}
Step by step take you to experience the charm of Algorithms
$O(1)<O(logn)<O(n)<O(nlogn)<O(n^2)<O(n^3)<O(2n)<O(n!)<O(n^n)$

可以借助函数生成工具对比复杂度的大小

https://zh.numberempire.com/graphingcalculator.php

篇幅有限,在此不再过多讲解。总而言之:大数据时代因为算法而使人们的生活更加便利,就比如早上很多会打开APP查看今天的天气如何,湿度温度,提醒穿什么衣服,出门要不要带伞。又比如打开地图,查看今天路上有没有堵车,是否限行等等,这都是算法带来的便利。算法的发展空间还很大,各位coder加油。

If you want to improve yourself, learn more algorithms, high-level programming language skills, there are free study materials, please add micro letter: 19950277730 for more technical upgrade cheats. It is not only small like-minded partners, more numerous free programming skills, learning videos and information, plus micro-channel technology to learn to discuss it! !

Guess you like

Origin blog.51cto.com/14598441/2449127