Data structures and algorithms-why use algorithms

Source: Internet
Author: User

Today, why do we need to use algorithms?

What is the algorithm? Algorithm is: Refers to the problem solving scheme accurate and complete description, is a series of clear instructions to solve problems, the algorithm represents a systematic approach to describe the problem-solving strategy mechanism. In other words, the input of a certain specification can be obtained in a limited time with the required output. If an algorithm is defective, or is not suitable for a problem, executing the algorithm will not solve the problem. Different algorithms may use different time, space, or efficiency to accomplish the same task. The merits and demerits of an algorithm can be measured by the complexity of space and time. (Baidu Encyclopedia version)


Having said the concept of the algorithm, let's give an example of why the algorithm is needed.

public class Test {/** * uses the original loop to calculate arithmetic progression */private void Originalmethod (long n) {System.out.println ("* * using RAW loop algorithm * *"); long STA Rttime = System.currenttimemillis (); Long sum = 0;for (Long i = 0; I <= N; i++) {sum + = i;} Long endTime = System.currenttimemillis (); SYSTEM.OUT.PRINTLN ("Result:" + sum); System.out.println ("spents:" + (Endtime-starttime));} /** * uses the arithmetic progression algorithm to calculate * * @param n */private void Advancemethod (long n) {System.out.println ("* * using arithmetic progression algorithm * *"); Long startTime = S Ystem.currenttimemillis (); Long sum = 0;long A1 = 1;long an = N;sum = (A1 + an) * N/2;long endTime = System.currenttimemi Llis (); SYSTEM.OUT.PRINTLN ("Result:" + sum); System.out.println ("spents:" + (Endtime-starttime));} public static void Main (string[] args) throws interruptedexception {test test = new Test (); Long n = 1000; System.out.println ("-------when n=" + N + "------"); Test.originalmethod (n); Test.advancemethod (n); n = 1000000; System.out.println ("-------when n=" + N + "------"); Test.originalmethod (n); Test.advancemethod (n); n = 1000000000L; SyStem.out.println ("-------when n=" + N + "------"); Test.originalmethod (n); Test.advancemethod (n);}} 


Output Result:


-------when the n=1000------
* * Using the original loop algorithm * *
Results: 500500
Spents: 0
* * Using the arithmetic progression algorithm * *
Results: 500500
Spents: 0
-------when the n=1000000------
* * Using the original loop algorithm * *
Results: 500000500000
Spents: 3
* * Using the arithmetic progression algorithm * *
Results: 500000500000
Spents: 0
-------when the n=1000000000------
* * Using the original loop algorithm * *
Results: 500000000500000000
Spents: 2070
* * Using the arithmetic progression algorithm * *
Results: 500000000500000000
Spents: 0


From the above results can be seen, the use of the loop algorithm used to increase the time, and reached a certain order of magnitude (such as 10 of the 20), estimated that we can not wait for the result of death, and the inverse of the use of the arithmetic progression algorithm, the actual use is 0, of course, is actually not 0, but too fast, Did not show it, two calculation methods compared to each other, the performance of the algorithm can be seen in a moment.

And for today's big data, is still hundreds of millions of billions of of the data, the process of calculation is more complex than our above, the time required more, if not using the corresponding algorithm, the time to solve a problem is basically not estimated, so we need the algorithm


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structures and algorithms-why use algorithms

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.