C # Data structure uncover a _c# tutorial

Source: Internet
Author: User
Tags data structures

Here, let's say a C # data structure.

① What is a data structure. The data structure, literally, is the method of studying the dataset, which is a way of studying how data is organized in a program. A data structure is a collection of elements that have one or more specific relationships between each other. A bit of a classic in the program world, program design = data structure + algorithm. With the source code to reflect the data structure, is programming. What specific relationships he has,

(1) collection (set): As shown in Figure 1.1 (a), there is no other relationship between the data elements in the structure except for the relationship of "belong to one set". A set is similar to a set of mathematics, having disorder, uniqueness, and certainty.

(2) linear structure (Linear Structure): As shown in Figure 1.1 (b), there is a one-to-one relationship between the data elements in the structure. The most work that we. NET programmers do is to crud the table of the database, and the smallest data unit of the second table is the row. Each row of data is the most obvious linear structure.
(3) Tree Structure: As shown in Figure 1.1 (c), there is a one-to-many relationship between the data elements in the structure. In reality, family relations are the most obvious tree-shaped structure. As shown in the figure

And for our. NET programmers, the tree-shaped controls that operate are the most obvious tree structures


(4) The graph structure (Graphic Structure): As shown in Figure 1.1 (d), there is a many-to-many relationship between the data elements in the structure. In reality, the graph is applied too much, as shown in the figure:

For us. NET programmers use less, when you use C + + for some low-level applications, such as search engines, map navigation applications are quite a lot.

The above is a description of the data structure.

Developers who have done so know that the algorithm is closely related to data structures and programs. In the program design, the corresponding data structure is first determined, then the corresponding algorithm is designed according to the data structure and the problem.

②, what's the algorithm? Algorithm, is the method of calculation, is the solution to the problem, is a specific type of problem solving a description of the steps, is a finite sequence of instructions. With the source code embodiment, the algorithm is the embodiment of programming. An algorithm should have the following 5 features:

1. Poor (finity): An algorithm always ends after a poor execution, that is, the execution time of the algorithm is limited. When we are beginners. NET, we often write dead loops, which is not an algorithm, because it is infinite.
2, Certainty (unambiguousness): Every step of the algorithm must have the exact meaning, that is, no ambiguity, and for the same input can only have the same output. For our. NET programmers to write two semantic source code, the compiler does not make you pass.
3, input (inputs): An algorithm with 0 or more inputs. It is the data structure that is given before the algorithm starts. Programming is the solution to the problem, if not input, how to solve the problem.
4. Output: An algorithm has one or more outputs, and there is a particular relationship between these outputs and inputs. Programming is to solve the problem of life, you do not let users see the final results, this lost the meaning of programming.
5, can line (realizability): Each step of the algorithm can be implemented through the implementation of the basic operation of the limited time to achieve. This is closely related to the poor nature.

What is the evaluation criteria for the algorithm?

The main criteria for evaluating an algorithm are as follows: 1, correctness (correctness). 2, readability (readability) 3, robustness (robustness). 4, Running time (Running times). 5, Occupy space (Storage spaces).

The first 3 properties, we are very well to handle. What is relevant to our programmers is running time and taking up space. However, as the hardware gets cheaper, we add more hardware to the footprint. In the face of massive data, we are particularly concerned about the runtime (Running time). The running time of this computer at this time is determined by the following factors:

1, hardware conditions. Includes the type and speed of the processor used (for example, using a dual-core processor or a single core processor), available memory (cache and RAM), and available external storage.
2, the implementation of the algorithm used in the computer language. The higher the language level of the implementation algorithm, the lower its execution efficiency.
3. Compiler/interpreter for the language used. In general, compilation is more efficient than interpretation, but it is more flexible to interpret.
4, the operating system software used. The function of the operating system is mainly to manage the software and hardware resources of computer system, and to provide an interface for computer users to use the computer conveniently. Various language handlers such as compilers, interpreters, and applications run under the control of the operating system.

The evaluation running time is an algorithm time complexity, and the time complexity of an algorithm (complexity) refers to the corresponding relationship between the running time of the algorithm and the scale of the problem.

The basic operation of the algorithm is generally referred to as the most deep loop in the algorithm, so the frequency of the basic operation statements in the algorithm is a function f (n) of the problem scale N, which is written as: T (n) =o (f (n)).  where "O" indicates that with the increase of the problem scale n, the growth rate of the algorithm execution time is the same as that of F (n), or the concept of order of magnitude is represented by the "O" notation. These are just the concepts of theory, and we use timers to prove the concept.

Such as:

①x=n; /*n>1*/
y=0;
while (Y < x)
{
y=y+1; ①
}

Theoretically, this is a cyclic program, while the loop is n, so the frequency of the statement ① in the program segment is n, the time complexity of the program segment is T (n) =o (n).

Verify from the program, and when n=10, the results of the run are as shown in the figure:

When n=100000, the results of the run are as shown in the figure

This proves that the time complexity of the algorithm is really close to O (n)

Ii

for (I=1;i<n;++i) {
for (J=0;J<N;++J)
{
A[i][j]=i*j; ①
}
}

Theoretically interpreted as this is the second cycle of the program, the outer for Loop loop number is n, the inner for loop of the number of cycles of n, so, the program section of the sentence ① frequency is n*n, then the time complexity of the program section
is t (n) =o (n²).

It is proved from the program that when n=10, its operation effect is as shown in the figure:

When n=100000, it works as shown in the figure:

This proves that the time complexity of the algorithm is indeed close to O (n²)

③x=n; /*n>1*/
y=0;
while (x >= (y+1) * (y+1))
{
y=y+1; ①
}

This is a cyclic program, while the loop of the loop number n, so, the program segment in the frequency of statements ① is N, the program segment time complexity is T (n) =o (√n).

From the program proof: when n=10, the operation effect as shown in the figure:

When n=100000, the effect is as shown in the figure:

This proves that the time complexity of the algorithm is indeed close to O (√n)

In this paper, the basic concept of data structure is introduced, the basic concept of the algorithm is introduced, and the time complexity of the algorithm is discussed, and the program is used to prove it.

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.