Introduction to Algorithms----------what is an algorithm?

Source: Internet
Author: User

What is an algorithm?

Algorithm, one that makes us sound familiar with unfamiliar words. Many just graduated from college graduates, in the bat interview, the two essential, data structure + algorithm.

So what is the algorithm, today let us follow the "Introduction to the algorithm" this book, together to soar the world of algorithms.

Algorithm, as the name implies, is the method of calculation, this calculation is a very broad vocabulary, can refer to the calculation of our life to buy vegetables, can refer to our program in the calculation of programming solutions, in short, the algorithm is a ubiquitous thing. Whether it is in our life or work, we can not leave the algorithm.

The algorithm in life:

I believe that everyone has done such a thing as a child, is the mother gave us 1 dollars, this piece of money is our day's allowance (maybe we all say a piece of money can be a yarn, anyway, I was a child, 1 cents a popsicle, 1 cents a bag through heart cool drink). Then I'm going to think about what I can do with this piece of money, buy 10 popsicles, or buy 5 popsicles and 5 cool hearts.

At this point, my choice is the embodiment of an algorithm, that is, in two different ways, to achieve a body of money all the effect of spending light.

Algorithms in the program:

The main thing I've been doing for Android recently is an example of my Android code (which is actually Java's basic code) to get an understanding of the algorithm.

The first type of notation:

@Override public    void onscrollchanged (Basescrollview scrollView, int x, int y, int oldx, int oldY) {        log.e ("scroll bar "x+"  "+y+" "  +oldx+"  "+oldy" <pre name= "code" class= "java" >            int showorhide = oldY > 0? View.VISIBLE:View.GONE;
if (ScrollView = = Article_detail_scroll) {article_detail_btn_totop.setvisibility (showorhide);}}

The second type of notation

@Override public    void onscrollchanged (Basescrollview scrollView, int x, int y, int oldx, int oldY) {        log.e ("ScrollBar State" , x+ "  +y+" "  +oldx+" "  +oldy);        int showorhide = OldY > 50? View.VISIBLE:View.GONE;        if (ScrollView = = Article_detail_scroll && article_detail_btn_totop.getvisibility ()! = showorhide) {            Article_detail_btn_totop.setvisibility (showorhide);        }    }


At this point, it is very intuitive to observe how our two-segment code is different.

You can see that the second piece of code has a more judgmental condition than the first paragraph.

Both article_detail_btn_totop.getvisibility ()! = Showorhide

So what's the word for, he's used to judge if we're going to set the value equal to our original value?

If it's equal, set it up once. It's a waste of system performance.


Perhaps read here, some friends will ask, you two pieces of code and algorithms have what relationship?

So you still don't understand the meaning of the algorithm, the code of the worry about the slightly, but also embodies the design of good or bad (both: algorithm ) a way


What the? I've said so much that you still don't understand what the algorithm is.

Well, please do not give up treatment, I will continue to treat your illness (don't mind if you have a joke)

Let me tell you the simplest example, I now need two positive integers, so that two numbers add up to 3, both, to satisfy: a+b=3

What the value of a and B can be

Programme I: a =1 b=2;
Scenario Two: a =2 b=1;

The difference between these two schemes is also an embodiment of an algorithm, but is a NP-incomplete algorithm solution (NP will be mentioned later)

Believe that here, you should know what is the algorithm put.

            int showorhide = oldY > 0? View.VISIBLE:View.GONE;

Introduction to Algorithms----------what is an algorithm?

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.