Sei sulla pagina 1di 14

Algorithm

What is the algorithm?

Algorithms are a very important foundation in computer science. To put


it simply, the algorithm is to use mathematics to calculate mathematics
(the calculations of ancient people, the calculation of modern people's
computers), which can be said to be a number of subjects.

To solve various problems in real life, computer scientists map real-world


problems to math problems, then design formulas, write formulas into
programs, and let computers execute programs to calculate answers—
these formulas are called algorithms.

Although the word "formula" is used here to describe the algorithm, it is


not a mathematical formula in your impressions. Because computers can
perform complex calculations, formulas can be designed in dozens of
lines, hundreds of lines, and even many mathematical theories.

Therefore, even those who have studied algorithms do not know how to
design algorithms; because mathematics and programs are too
complicated. It is more complicated to want to map real problems to
math problems.

The computer will only count numbers

Looking back, what is the computer? The computer is a very trendy


Chinese translation, but in fact the original meaning of the computer is
"computer." The English of the computer is called computer, and the
English of the calculation is called compute.
A computer is a computer that only calculates, judges, and stores
numbers. Fast and accurate.

A program is a series of steps for calculating, judging, and storing


numbers.

The computer will only process numbers (binary digits). Every word,
every color, every kind of sound in the computer has a corresponding
number.

For example, we stipulate that 1 means "one", 2 means "B", and 3


means "person", .... A number corresponds to a Chinese character. All
the Chinese characters in the computer are changed according to the
rules of the people.

For another example, the word "person" is a "person" on the computer


screen. The screen of the computer screen is composed of many small
light spots; the "person" on the computer screen is also composed of
many small light spots. We use the lower left corner of "person" as the
coordinate origin, the horizontal direction is the X axis, and the straight
direction is the Y axis. Then the "person" is actually (0,1), (1,2), (2,3), ..
These coordinates are formed after the black spots are drawn. The
shape of the word "person" has been transformed into a series of
numbers in the computer.

By the same token, text, color, pictures, images, and sounds that appear
on the screen of a computer can all be converted into
numbers. Everything is a number in the computer.

The computer is not as magical as it is supposed to be. But the most


powerful part of the computer is not the computer itself, but the
computer can be connected to a variety of equipment. By connecting the
camera to the screen, you can turn the color into a number and turn the
number into a color. By connecting the microphone to the earphone, you
can turn the sound into a number and turn the number into a sound.
Once the computer is connected to the device, it is extra useful. Connect
the phone and the base station, you can exchange each other; connect
the digital camera and the printer, you can make memories; connect the
weight meter and the sieve, the computer will pick the potatoes; pick up
the car, connect the warning lights, and then mix and match When you
pick up a bunch of things, it becomes a mass transit system.

To solve real problems with a computer, there are usually two aspects to
consider: First, what devices should the computer be connected to? How
to control these devices with a computer? Second, how do real problems
correspond to mathematical problems? How to design an algorithm?

Program to compare numbers, change numbers, store numbers

For example, we want to turn "people" on the screen into italics. The
process is roughly like this - first, take the figures of "people" (0,1),
(1,2), (2,3), ... out; then, the higher the coordinates, Just move a little
more to the right, so that it becomes italic. To move the coordinates to
the right, let the computer do the digital addition calculation and then
store the addition results.

For another example, use the mouse to select a folder and the color of
the folder will be reversed. The process is roughly like this - first, after
the computer detects the coordinates of the mouse click, convert the
coordinates into numbers; then, take the information on the screen and
see the coordinates of each thing on the screen. One matches the
coordinates of the mouse; 噢, it turns out to be a picture of a folder, and
the color of the folder is reversed.

As another example, the computer is said to pick potatoes. The process


is roughly like this - take each potato out and use a special instrument to
convert the shape, weight, color, and smell into numbers and store them
in the computer. Then, compare these numbers with a computer to find
out the good ones. Potatoes, so there are loose potatoes!

Writing programs and calculating numbers is what programmers do.


Maths and programs are so complicated, why use computers to
solve real problems?

The calculation speed of the computer can be said to be very fast, and it
can be performed tens of millions of times in one second. Even if there
are so many words, how big the picture is, the computer handles it, it is
easy and freehand, and it is very smooth.

Open any file on your computer, scroll the file with your mouse, and
your eyes are still empty. The correct picture will appear on the screen
immediately. In fact, when the volume is animated, the computer has
been calculated tens of millions of times, and only a very short time is
used, and all the data that should be presented on the screen is
calculated.

Humans will want to solve problems with computers. It depends on the


speed and correctness of the computer and the characteristics that the
computer will automatically calculate according to the program. As long
as the programmer writes a good program, the next job can be done by
the computer. Computers do things faster and better than humans.
Computers do things that humans can't do; despite the complexity of
math and programming, there are still many people who choose to use
computers to solve problems.

Which real problems are solved with algorithms?

Modern people's lives are inseparable from algorithms. For example,


there are hundreds of algorithms in your phone. You can Google for
news. The Multimedia and Interdisciplinarity fields at the bottom of the
homepage of this site also collected a lot of videos.

A l go r i thm

What is the algorithm?

The algorithm consists of three parts: input, calculation steps, and


output. When introducing this matter, someone linked to the concept of
a function , and some people linked to the concept of a black box white
box .
-----------------
Input --->| computational |
| sequence |---> output
-----------------

Inputs and outputs are a bunch of numbers. The practical thing is to put
these numbers in the data structure , such as array, list. The input
source, usually the file stored in the hard disk, or the number captured
by the hardware device, such as a digital camera, a microphone, and the
like. The output is usually located in a file stored on the hard disk, or is
converted by a hardware device and then displayed in other types, such
as a digital television, a digital audio, and the like.

The calculation step is a series of instructions that process


numbers. There are two types of instructions, one is arithmetic, such as
mathematical operations plus, minus, multiply and divide, logical
operations and or not, comparison operations are greater than or equal
to less than, bit operations are left and right or XOR. The other type is
reading and writing. For example, reading a number somewhere and
storing a number somewhere is similar to the meaning of the MR and
M+ buttons of a computer .

The ancients defined the algorithm, which stipulated that the number of
calculation steps must be a finite step, not an infinite step. In the
terminology of programming languages: algorithms cannot have infinite
loops.

The ancients originally specified a limited number of steps in order to


facilitate the total number of steps. But in practice, many computer
programs remain in execution until they are turned on, until they are
rebooted, such as algorithms for network transmission. So practice can
be an infinite step.

How to record an algorithm?


Someone uses a virtual code to record an algorithm. To design
a computer program , virtual code is more appropriate.
GREATEST_COMMON_DIVISOR(a, b)
1 while a ≠ b do
2 if a > b then
3 a ← a - b
4 else
5 b ← b - a
6 return a

Some people use flowcharts to document an algorithm. If you want to


design an electronic circuit , the flow chart is more appropriate.

Most of the time, we can't fully understand algorithms from virtual code
and flow charts, just as we can't fully understand mathematical concepts
from mathematical formulas. If you want to understand the algorithm,
you usually have to use the auxiliary explanation of the text and the
picture.

How to implement an algorithm?


The meaning of the implementation is: actually go to the operation,
actually run.

For the students of the Department of Labor, it is natural to write the


algorithm into a computer program , such as a C or C++ program, and
then execute the program on the personal computer.

1. Int gcd ( int a , int b ) {


2. While ( a != b )
3. If ( a > b )
4. a -= b ;
5. Else
6. b -= a ;
7. Return a ;
8. }

For motor students, it is natural to design algorithms into electronic


circuits that are executed on breadboards , printed circuit boards ,
and PLDs .

Electronic circuits also have adders, subtractors, AND logic gates, OR


logic gates, etc., so electronic circuits can also be used to implement
algorithms. For example, electronic watches, walkmans, leisure cards,
etc., all directly kill the algorithm on the wafer. Before personal
computers and smart phones were still popular, they used to implement
algorithms in electronic circuits.

The execution speed of electronic circuits is fast, and the execution


speed of computer programs is slower. However, the process of making
electronic circuits is quite cumbersome, requires sophisticated
equipment, complicated processes, a large amount of manpower and
money, and cannot be modified after being made. In contrast, writing
programs is much simpler and easier, and it is easy to adjust on a
computer. The code can store a lot of code. The most important thing is
that every household has a computer.

Time complexity, space complexity

To judge the quality of an algorithm, the most basic indicators are time
and space.

The most intuitive way is to measure the execution time of the program
and the memory usage of the program. However, because the execution
time of the same algorithm varies from computer to computer, and
because the programming language and programming skills used by
each person's algorithm are different, the execution time and memory
usage are not a stable criterion. .

The mathematician then calculates the number of steps.


BUBBLESORT(A, n) | steps
1 for i ← 0 to n-1 do | n
2 for j ← i to ni-1 do | n(n-1)/2
3 if A[j] < A[j+1] then | n(n-1)/2
4 temp ← A[j] | n(n-1)/2
5 A[j] ← A[j+1] | n(n-1)/2
6 A[j+1] ← temp | n(n-1)/2
Total = n + 5n(n-1)/2
= n + 2.5n2 - 2.5n
= 2.5n2 - 1.5n
= O(n2)
The mathematician writes the number of steps as an algebraic
expression. For example, when the input data has n = 1000 numbers,
the total number of steps is 2.5 × 10002 - 1.5 × 1000 = 2498500 steps.

Once you have the number of steps, you can further estimate the
execution time. Suppose a step requires 10 clocks, and the CPU of the
CPU is 2GHz: 2000000 clocks per second, then the program execution
time is about 12.2425 seconds.

But this is not the exact number of steps. Due to the actual relationship,
the coefficients are easy to change, so the coefficient is of little
significance. So the mathematician only takes the highest power of the
algebraic expression and specifies that n must be large enough (similar
to the infinite calculus approach). Although this is a very inaccurate way
of estimating, it is still possible to easily classify common algorithms and
roughly compare them.
| time* | space
---------------+-------------+--------
Bubble sort | O(n2) | O(n)
Insertion sort | O(n2) | O(n)
Merge sort | O(n log(n)) | O(n)
Quicksort | O(n2) | O(n)
Heapsort | O(n log(n)) | O(n)
Counting sort | O(n+r) | O(n+r)

*worst case

Space is calculated in a similar way to time, and it is not mentioned.

Problem solving

To judge the quality of an algorithm, in addition to the amount of time


and space, it is mainly the effectiveness of the algorithm to solve the
problem.

Math problems, usually can be clearly answered, for example, the bigger
the number, the better. Usually in this case, there are multiple
algorithms that can get a positive solution, so the performance of these
algorithms is just as good.

In real-world problems, it is often difficult to define absolute good or


bad, such as beauty and ugliness, music noise, emotions, sorrows and
sorrows, right and wrong, etc. At this time, the effectiveness of the
algorithm is judged by humans, using comparisons and voting. And so
on to determine the outcome.

Mathematics and Computing

Mathematics is the basic element to construct things and express


concepts. By mathematics, mathematicians try to construct everything,
express each concept, and piece together the whole picture of the
world. Such as position, shape, relationship, transformation, recursion,
limit, comparison, arrangement, pros and cons, hypothesis, these things
are numbers. For example, there are, no, poly, scattered, sparse, secret,
surplus, loss, bend, straight, cross, wrong, dynamic, static, these things
are also count. The number related to the behavior of an object is
physics; the number related to the change in the nature of matter is
chemistry; the number related to the operation of life is biology. If we
continue to subdivide, all the things we know can be counted.

In the number, it can be measured by the quantity, and it can be


measured. Like emotions, styles, and strategies, it is difficult to use
quantity to represent, and it is difficult to measure or even measure. For
example, movements, melody, and order can be quantified in parts or in
whole, and can be measured. Computation is the construction of things
and the concept of quantity. The computational scientist, by quantity,
tries to measure various things and concepts and grasp the exact extent
and level.

A l go r i thm

Learning programming language


There are two levels of learning programming languages: first, the
syntax of the programming language itself; and second, the conversion
of ideas into code.

The first level is called "Programming Language". The goal is to


familiarize yourself with the specification and use the programming
language flexibly.

The second level is called "Programming Programming." The goal is to


design the code to solve the problem. However, there is no recognized
and fixed learning process in the world today.

Learning algorithm

Learning algorithms have two levels: first, the operation process of the
algorithm itself; second, the idea is converted into an algorithm.

The first level is called "algorithm Algorithm". The goal is to understand


the algorithm and use the algorithm flexibly. Readers can refer to the
major fields on the homepage of the site, such as graph theory,
computational geometry, string learning, and so on.

The second level is called "Algorithm Design". The goal is to design


calculation steps to solve the problem. Readers can refer to the
Algorithm Design field on the homepage of this site, and learn from
various algorithms to draw inspiration.

Learning libraries, tools

Many real-world problems and their calculation steps have become


standard processes, and there is no room for change to become an
algorithm. Therefore, scientists have written these algorithms into
"Library Library", and then wrote the common needs of real life as
"Tools Toolkit", making the programming process more rapid. There are
already many ready-made libraries and tools on the Internet, usually
with detailed instruction manuals for engineers to use.
In today's software industry, almost all of them are directly using ready-
made libraries and tools; only when they are engaged in innovative
research and development, they will design code and design algorithms
from scratch. Excellent engineers are always good at using libraries and
tools to quickly realize the functions they want.

Algorithm book
Algorithms
Robert Sedgewick and Kevin Wayne

Introduction to Algorithms
Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest and Clifford Stein

Algorithm Design: Foundations, Analysis, and Internet Examples


Michael T. Goodrich and Roberto Tamassia

Algorithm Design
Jon Kleinberg and Éva Tardos

An Introduction to the Analysis of Algorithms


Robert Sedgewick and Philippe Flajolet

An Introduction to the Analysis of Algorithms


Michael Soltys

The Algorithm Design Manual


Steven Skiena

Encyclopedia of Algorithms
Editors: Kao, Ming-Yang

The Art of Computer Programming


Donald E. Knuth

Algorithm storybook
Out of their Minds: The Lives and Discoveries of 15 Great Computer Scientists
Fan: "The new future of brave information: the hero behind the creation of
information technology"
Jane: "Thinking: 15 computer geniuses and their major discoveries"
Nine Algorithms That Changed the Future: The Ingenious Ideas That Drive Today's
Computers
Fan: "The nine algorithms that change the world: the strongest concept that makes
today's computers omnipotent"
Jane: "Nine Algorithms for Changing the Future"

Automate This: How Algorithms Came to Rule Our World


Fan: "The algorithm rules the world"
Jane: Algorithmic Empire

Algorithm course
Adam Blank
There are many course notes on the algorithm course website.

Jeff Erickson
There are many course notes on the algorithm course website.

Erik Demaine
There are many special topics on the algorithm course website.

Algorithm website
GeeksforGeeks
Programming language, math, algorithm puzzle problem.

Algorithms Notes
Algorithmic puzzle problem.

Rosetta Code
The implementation code for a wide variety of problems.

LeetCode
The interview question bank is mainly an algorithmic puzzle topic. You can practice
solving problems online.

Math website
AMS Open Math Notes
The mathematician's teaching notes.

Wolfram Math World


Mathematics Encyclopedia.
The On-Line Encyclopedia of Integer Sequences
Encyclopedia.

Potrebbero piacerti anche