Introduction to Algorithms reading Notes--chapter 2.2 Analysis algorithm

Source: Internet
Author: User

2.2 Analysis algorithms

The result of the analysis algorithm means the resources required by the predictive algorithm. Although we sometimes care about memory, communication, or computer hardware, we usually want to measure time.

Before analyzing the algorithm, there is a model for implementing the technology, including a model that describes the resources used and their costs. We assume a universal single-processor computing model-random access machine (random-access Machine,ram) as our implementation technology, the algorithm can be implemented by computer programs. In the RAM model, the instruction executes one after the other, and there is no concurrent operation. The RAM model contains the instructions that are common to the computer: arithmetic instructions (such as addition, subtraction, multiplication, remainder, rounding up, rounding down), data movement commands (loading, storing, copying), and control directives (conditional and unconditional transfers, subroutine calls and returns). The time required for each such instruction is constant.

With regard to the relationship between running time and scale, the constant run time is relative to a certain data size, such as two numbers added, these two numbers can not be too large, otherwise it is not a constant time. Most of this book does not consider the impact of memory levels.

Analysis of insertion sorting algorithm

In general, the time required for the algorithm increases with the size of the input, so it is common to describe the run time of a program as a function of its input size. The following strictly defines "input size" and "Run Time":

The best concept of input scale relies on the amount of research. The most natural measure for sorting or calculating discrete Fourier transforms is the number of items in the input; for two number multiplication, the best measure of input size is the total number of digits required for input with the usual binary notation; sometimes it is more appropriate to describe the input size in two numbers instead of a number. For example, a graph, the input scale is described by the fixed-point number and the number of sides.

The run time of an algorithm on a particular input is the number of basic operands or steps performed. We assume that a constant time is required to execute each line of pseudo-code.

We give Insertion-sort algorithm analysis, it is worth noting that: for or while loop in the usual way (that is, because of the test in the loop header), the number of times to execute the test than the number of times the loop body executed 1. Assume that the comment does not occupy time.

Insortion-sort (A) Price Number
For j = 2 to A.length C1 N
Key = A[j] C2 N-1
Insert A[j]into the sorted sequence a[1..j-1] 0 N-1
i = J-1 C4 N-1
While I > 0 and a[i] > key C5 Sigma (T.j,j=2,j=n)
A[I+1] = A[i] C6 Sigma (T.j-1,j=2,j=n)
i = I-1 C7 Sigma (T.j-1,j=2,j=n)
A[I+1] = key C8 N-1

Run time T (n) =

Even for inputs of a given size, the run time of an algorithm may depend on which input is given at that scale. For example, the ranking algorithm is already in the right level. For the insertion sort, the best case is

Formula--

This is a one-time function of N.

The worst case scenario is

Formula--

This is a two-time function about N.

Worst case and average situation analysis

In this book, we generally focus only on the worst running times for three reasons:

1. The worst running time of an algorithm gives the upper bound of the algorithm's running time.

2, for some algorithms, the worst case often occurs.

3, the "average situation" is often as bad as the worst case.

Growth scale

What we are interested in is the growth rate of the running time or the level of growth, that is, with the increase of N, the increase of the running time.

Introduction to Algorithms reading Notes--chapter 2.2 Analysis 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.