Introduction to Algorithms Study Notes Chapter One The Role of Algorithms in Computing

Informally speaking, an algorithm is any well-defined calculation process (referring to a definition that is unambiguous, does not cause contradictions, and meets all the requirements that it should meet). The process takes a certain value or a set of values ​​as input and produces A certain value or a collection of values ​​is output. An algorithm is a sequence of computational steps that transform input into output.

We can also think of algorithms as tools for solving well-described computational problems. The problem statement describes the expected input/output relationship, and the algorithm describes a specific calculation process to realize the input/output relationship.

Regarding the definition of the sorting problem:
Input: a sequence of n numbers <a1, a2,…, an>.
Output: an arrangement of the input sequence <a1', a2', …, an'>, which satisfies a1'<=a2'<=…<=an'.

For example, given the input sequence <31, 41, 59, 26, 41, 58>, the sorting algorithm will return the sequence <26, 31, 41, 41, 58, 59> as the output. Such an input sequence is called an instance of a sorting problem. The problem instance consists of the inputs necessary to calculate the solution of the problem (satisfying the various constraints imposed in the problem statement).

For the sorting algorithm, which is better depends on the following factors: the number of items to be sorted, the degree to which these items have been sorted slightly, possible restrictions on item values, computer architecture, and the type of storage device used (main memory, disk , Tape).

If the algorithm stops with the correct output for each input instance, the algorithm is said to be correct, and the correct algorithm is said to solve the given calculation problem. An incorrect algorithm may not stop at all for some input instances, or it may stop with an incorrect answer. An incorrect method may sometimes be useful as long as its error rate is controllable.

The algorithm can be described in English, it can also be described as a computer program or even as a hardware design. The only requirement is that the description must accurately describe the calculation process to be followed.

The factorial function grows faster than the exponential function.

The set of n elements has 2 n subsets (including the empty set and itself), because for each element, it can only be in the set and not in the set.

Data structure is a way of storing and organizing data, designed to facilitate access and modification. No single data structure is effective for all purposes. It is important to know the advantages and limitations of several data structures.

In order to obtain the best performance from a multi-core computer, parallelism must be considered when designing an algorithm.

Different algorithms designed to solve the same problem often have significant differences in efficiency, and these differences may be more important than the differences caused by hardware and software. For example, there are two sorting algorithms: the first is insertion sort, which is sorting n items. The time spent by this algorithm is roughly equal to c1n 2 , where c1 is a constant that does not depend on n; the second is merge sort, which is sorting n items Term, the time taken by the algorithm is roughly equal to c2nlgn, where lgn represents the logarithm of n with base 2 and c2 is a constant that does not depend on n. Compared with merge sort, insertion sort usually has a smaller constant factor (c1<c2). In terms of running time, the constant factor may be far less important than the dependence on the input size n. Therefore, when n is relatively small, insertion sort It is usually faster than merge sort, but once the input size n becomes larger, the advantages of merge sort lgn to n will be sufficient to compensate for the difference in constant factors. No matter how much c1 is smaller than c2, there will always be an intersection. Beyond this point, merge sort is more fast.

The above example shows that we should treat algorithms as a technology like computer hardware. The performance of the entire system depends not only on the choice of fast hardware but also on the choice of effective algorithms.

Guess you like

Origin blog.csdn.net/tus00000/article/details/114440980