Made Simple - Charm Algorithms

What is an algorithm?
A description of the algorithm is actually a question or solve a class of problems in the process. They are not familiar Gaussian, right? In the first item plus the last item entries multiplied result by 2 to calculate the "1 + 2 + 3 + 4 + 5 + ••• + (n-1) + n" is. We call it the Gauss algorithm, because complex problems can be solved by a formula, greatly reducing the time problem solving. Of course, the charm of the algorithm is also more than that, we then look down:

Made Simple - Charm 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

Two existing seeking Fibonacci (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 assess compute 1 + 2 + ... + n algorithm in this program
Made Simple - Charm Algorithms
is clearly better second algorithm. 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

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

public static void test7(int n) {
    // 1(定义一次i) + 2*log2(n)(i*2运算次数) + 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 + 3*log2(n) + 2 * nlog2(n)
    // 大O表示法时间复杂度O(nlogn)
}
![](https://s1.51cto.com/images/blog/201911/09/4dab54d2db62501734bf51c4f89a038f.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
 $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 

总而言之,现今大数据时代,算法的使用和研发越来越受人瞩目。算法也逐渐进入人们的生活。篇幅有限,在此不再过多讲解算法的知识。我们下期再会。

如果您想提升自己,学习更多算法、高级编程语言技巧,这里有免费的相关学习资料,欢迎加微信:19950277730获取更多技术提升秘籍。这里不仅有志同道合的小伙伴,更有无数免费编程技巧、学习视频和资料,加上微信来一起探讨学习技术吧!!

Guess you like

Origin blog.51cto.com/14598441/2449133