C # data structure and algorithm secrets 1

Source: Internet
Author: User

Here, let's talk about the data structure of C.

① What is a data structure. Data structure is a method of studying data and how data is organized in a program. A Data Structure is a set of data elements that have one or more specific relationships with each other. If the programming world is a little classic, Program Design = Data Structure + algorithm. It is embodied in source code. The data structure is programming. What are their specific relationships,

(1) Set: As shown in 1.1 (a), the data elements in this structure do not have any other relationships except for the "same Set. A set is similar to a set of mathematics. It has an ordinal, unique, and deterministic nature.

(2) Linear Structure: As shown in 1.1 (B), the data elements in this Structure have a one-to-one relationship. We. net programmers do the most work on the database table crud, the smallest data unit of the two tables is rows. Each row of data is the most obvious linear structure.
(3) Tree Structure: As shown in 1.1 (c), there is a one-to-many relationship between the data elements in the Structure. In reality, family relationships are the most obvious tree structures.

For our. net programmers, the tree control for operations is also the most obvious tree structure.

(4) Graphic Structure: as shown in Figure 1.1 (d), the data elements in this Structure have many-to-many relationships. In reality, there are too many graph applications ,:

For us. Net programmers have fewer applications. When you use C ++ for some underlying applications, such as search engines and map navigation applications, there are quite a few.

The above is an introduction to the data structure.

Developers know this truth, and algorithms are closely related to data structures and programs. When designing a program, first determine the corresponding data structure, and then design the corresponding algorithms based on the data structure and the needs of the problem.

② What is an algorithm? An algorithm is a computing method, a solution to the problem, a description of the steps for solving a specific type of problem, and a finite sequence of commands. It is embodied in source code, and the algorithm is the embodiment of programming. An algorithm should have the following five features:

1. Finity: An algorithm always ends after a poor step is executed, that is, the execution time of the algorithm is limited. When we were new to. net, we often wrote an endless loop. This is not an algorithm, because it is infinite.
2. Unambiguousness: each step of an algorithm must have a definite meaning, that is, it has no ambiguity and can only have the same output for the same input. For our. net programmers to write the source code with ambiguity, the compiler won't let you through.
3. Input: An algorithm has zero or multiple inputs. It is the data structure given before the algorithm starts. These inputs are data objects in a data structure. Programming solves the problem. If it cannot be entered, how can it solve the problem.
4. Output: An algorithm has one or more outputs, and there is a specific relationship between these outputs and inputs. Programming is to solve the problems in life, and you will lose the meaning of programming if you don't let users see the final result.
5. realizability: each step in an algorithm can be achieved through a limited number of basic operations that have been implemented. This is closely related to poverty.

What is the evaluation standard of the algorithm?

The main criteria for evaluating an algorithm are as follows: 1. Correctness (Correctness ). 2. Readability 3. Robustness ). 4. Running Time ). 5. Storage Space ).

The first three properties are very good. What is closely related to our programmers is the running time and occupied space. However, as hardware gets cheaper and cheaper, we have nothing to do with increasing hardware space. In the face of massive data, we are particularly concerned with the Running Time ). The running time of the computer is determined by the following factors:

1. Hardware conditions. Including the type and speed of the processor used (for example, using a dual-core or single-core processor), available memory (Cache and RAM), and available external storage.
2. The computer language used to implement the algorithm. The higher the language level of the algorithm, the lower the execution efficiency.
3. Compiler/interpreter of the language used. In general, compilation is more efficient than interpretation, but interpretation is more flexible.
4. operating system software used. The main function of the operating system is to manage the software and hardware resources of the computer system and provide an interface for computer users to conveniently use the computer. Various language processing programs, such as compiling and interpreting programs, and applications are all running under the control of the operating system.

Evaluating the running Time is an algorithm's Time Complexity. the Time Complexity of an algorithm is the correspondence between the running Time of the algorithm and the scale of the problem.

Basic operations in an algorithm generally refer to the statements in the deepest loop of an algorithm. Therefore, in an algorithm, the frequency of basic operation statements is a function f (n) with the problem scale n: T (n) = O (f (n )). "O" indicates that, as the problem scale n increases, the algorithm execution time growth rate is the same as that of f (n), or the concept of an order of magnitude is represented by the "O" symbol. These are just some theoretical concepts. Here we use timers to prove this concept.

For example:

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

Theoretically, this is a loop program. The while loop has n cycles. Therefore, the frequency of statement ① In this program segment is n, and the time complexity of the program segment is T (n) = O (n ).

Verify from the program. When n = 10, run the result:

Running result when n = 100000

It is proved that the time complexity of the algorithm is indeed close to O (n)

For (I = 1; I <n; ++ I ){
For (j = 0; j <n; ++ j)
{
A [I] [j] = I * j; ①
}
}

Theoretically, this is a double loop program. The number of loops in the outer for loop is n, and the number of loops in the inner for loop is n. Therefore, if the frequency of statement ① In this program segment is n * n, the time complexity of the program segment is
T (n) = O (n² ).

It is proved from the program that when n = 10, the running effect is:

When n = 100000, the running effect is as follows:

It is proved 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 loop program. The while loop has n cycles. Therefore, the frequency of statement ① In this program segment is n, and the time complexity of the program segment is T (n) = O (√ n ).

From the program proof: WHEN n = 10, the running effect:

When n = 100000, the running effect is as follows:

It is proved that the time complexity of the algorithm is indeed close to O (√ n)

This article introduces the basic concepts of Data Structure, introduces the basic concepts of algorithms, focuses on the time complexity of algorithms, and uses programs 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.