Basic concepts of data structure and algorithms

Source: Internet
Author: User

It is the core meaning of computer programming to solve real life and production problem by computer, so how to solve the problem efficiently becomes the problem that we must pay attention to, and the data structure and algorithm is the two concepts which are introduced in order to solve the problem efficiently, the two concepts are connected and inseparable.

Let us clarify their relationship from the following practical questions:

1. Problems with books on shelves

For books on the shelves, we only deal with two core operations: 1) How to insert a new book, 2) How to find a specific book;

For the two problems mentioned above, we can propose three kinds of image placement methods:

    1. Put it anywhere. (A collection without indexes)
    2. Emitted in the phonetic alphabet order of the titles. (Sequential structure)
    3. First of all, in large categories, and then in alphabetical order. (Tree-like structure)
    4. More ideas? (Build Index Table)

In addition, for the 3rd method, we have some things to consider, such as: the size of each type of allocation space and the number of image book categories.

For each pendulum in fact corresponds to a data structure, then for the different data structures, the two operations involved in the implementation of the book efficiency is not the same. From the books placed above the problem, you can know

The efficiency of the problem-solving approach is related to how the data is organized.

2. Printing a positive integer 0~n problem

For this problem, we can have two methods: 1) using loops from 0 to N, 2) using recursion, first print 0~n-1, then Print N.

We know that the method can solve the problem quickly by using the loop, and the method two uses recursion to make the program occupy a lot of space. When n is very large, it may overflow, and the program will not output the result. So, from this example we know:

The efficiency of the problem-solving approach is related to the efficiency of space utilization.

3. Polynomial evaluation problems

For polynomial $f (x) = a_0+a_1x+\cdots+a_nx^n$, we also have two methods to solve. For any $x$, we can bring it directly into the original polynomial, except that each of these items needs to be counted as a $x$ exponent. Another way is to convert the original polynomial to $f (x) = A_0+x (A_1+x (\cdots (a_{n-1}+x)) A_n) $, we avoid the exponential operation from the beginning of the \cdots term. Obviously, method two is more efficient because it avoids a lot of exponential operations. We can also save the value of each $x^i$ in the algorithm, the next time only for one multiplication. From this example, we can see:

The efficiency of the problem solving method is related to the ingenious procedure of the algorithm.

4. What is a data structure

The way data objects are organized in computers can be divided into logical structure and physical storage structure.

The data object must be associated with a series of actions added to it.

The method used to complete these operations is the algorithm.

/*abstract data type definitions for matrices*/Template<typename elementtype>classcmatrix{Private: ElementType*data; introws; intcols; Public: Cmatrix (intMintN);//Create an empty matrix of m*n    intGetmaxrow ();//returns the total number of rows in a matrix    intGetmaxcol ();//returns the total number of columns in a matrixElementType Getentry (intIintj);//returns the element in column J of line Ifriend Cmatrix Add (Cmatrix A, Cmatrix B);//addition of two matricesFriend Cmatrix Multiply (Cmatrix A, Cmatrix B);//returns C=AB if the number of columns of a is equal to the number of columns in B, otherwise an error is returned
5. Definition of Algorithm 5.1 algorithm

The algorithm is actually the steps to solve the problem, it has several characteristics:

    • It is a limited set of instructions;
    • Accept some input, or no input;
    • There must be at least one output;
    • After the limited steps are terminated;
    • Each instruction must have a clear objective and be able to process it with a computer and not rely on every computer language.

Good algorithms need to have good time complexity and space complexity.

Analysis of the good and bad of 5.2 algorithm

When it comes to analyzing the efficiency of a general algorithm, we often focus on the following two types of complexity:

    • Worst case Complexity $t_{worst} (N) $
    • Average complexity $t_{avg} (N) $

They have the following relationship: $T _{avg} (N) \le T_{worst} (N) $

5.3 Progressive representation of complexity
    • $T (n) =o (f (n)) $ denotes the existence of a constant $c>0,n_0>0$ so that when $n\ge n_0$, there is $t (n) \le C\cdot f (n) $
    • $T (n) =\omega (g (n)) $ indicates that there is a constant $c>0,n_0>0$ so that when $n\ge n_0$, there is $t (n) \ge C\cdot g (n) $
    • $T (n) =\theta (h (n)) $ denotes both $t (n) =o (h (n)) $ and $t (n) =\omega (h (n)) $
5.4 Tips for complexity analysis
    1. If the two-stage algorithm has a complexity of $t_1 (n) =o (F_1 (n)) $ and $t_2 (n) = O (F_2 (n)) $, then
      • $T _1 (n) +t_2 (n) =\max (O (f_1 (n)), O (f_2 (n))) $
      • $T _1 (n) \times t_2 (n) = O (F_1 (n) \times f_2 (n)) $
    2. If $t (n) $ is a $k$-order polynomial about $n$, then $t (n) =\theta (n^k) $
    3. The time complexity of a For loop equals the number of cycles multiplied by the complexity of the Loop body code
    4. The complexity of the if-else structure is determined by the complexity of the IF condition judgment and the complexity of the two branches, and the total complexity is the largest.

Basic concepts of data structure and algorithms

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.