1. Data structure and algorithm introduction

Source: Internet
Author: User
Tags alphabetic character

Content: Mainly introduces the basic knowledge of data structure and algorithm, some basic concepts and terminology, as well as the definition of the algorithm, the characteristics of the algorithm, the time complexity of the algorithm and the space complexity

Note: The code of the data structure and algorithm in this series mainly uses the C language syntax, but because some features of C + + (reference type and bool type) are used, the code will be saved as CPP source files!

1. Data structure : The data structure is the way that the computer stores and organizes it. A data structure is a collection of elements that have one or more specific relationships to each other. Typically, a well-chosen data structure can lead to higher operational or storage efficiency.  Data structures are often associated with efficient retrieval algorithms and indexing techniques. ----from Baidu Encyclopedia

2. Basic concepts and terminology :

Data : Is a symbolic representation of an objective thing, which in computer science refers to all the symbols that can be entered into a computer and processed by a computer program such as: integers and real numbers, strings, graphs, images, animations

Data Elements : is the basic unit of data, in the computer program as a whole to consider and deal with. For example, a node in a linked list can be called a data element. A data element can consist of several data items, such as a book's title information as a data element, and each item in the bibliographic information (title, author, publishing house) is a data item. A data item is an indivisible minimum unit of data

data Object : is a collection of data elements of the same nature, and is a subset of data eg: Integer data object, alphabetic character data Object

Data structure : A collection of one or more specific relationships between each other, and the interrelationships between data elements are called structures.

3. logical structure and storage structure in the data structure:

Logical Structure: the logical structure of the data is logically described as data structure, independent of the storage of the information, is separate from the computer, the logical structure of the data can be regarded as a mathematical model from the specific problem abstract

classification of logical Structures :

(1) Aggregate structure: There is no relationship between data elements except those belonging to the same set

(2) Linear structure: There is a one-in-one relationship between data elements, eg: the first node in a single-linked list structure has a pointer to the next node, and the first node and the next node are relationship to the other

(3) tree structure: There is a one-to-many relationship between data elements, eg: in the company's management system, the general manager manages multiple managers, and a manager manages multiple employees

(4) Figure structure or network structure: There are many-to-many relationships between data elements, eg: a friend relationship between many students, any two students can be friends, thus constituting a graph structure or network structure

Note: The aggregate structure, tree structure, graph structure and network structure are all non-linear structures.

Specific data structures for linear structures : Linear tables (sequential tables, single linked lists), stacks and queues, strings, concrete data structures for nonlinear structures : trees, binary trees, graphs, and graphs. I will continue to describe in detail the definition and implementation of these structures later in this topic.

storage structure: The storage structure of the data object in the computer is called the data, the data element has two basic storage structures in the computer, namely the sequential storage structure and the chain storage structure.

Sequential storage structure : The logically adjacent nodes are stored in the adjacent storage unit of the physical location, the logical relationship between nodes is represented by the adjacency relation of the storage unit, and the resulting storage representation is called sequential storage structure----from Baidu Encyclopedia, This means that the stored data elements of the sequential storage structure are adjacent in the computer, and the sequential storage structure is often borrowed from the array. For example, sequential tables in linear tables are sequential storage structures.

chained storage structure : The logically adjacent nodes are not required to be adjacent to the physical location, and the logical relationship between nodes is represented by additional pointer fields. The resulting storage representation is called the chain storage structure----from Baidu Encyclopedia, that is, the chain storage structure in the computer storage location is not necessarily adjacent, they are just adjacent to the logical relationship, chain storage structure is often implemented using pointers. For example, a single-linked list in a linear table is a chained storage structure.

4. algorithm : Algorithm (algorithm) refers to an accurate and complete description of the solution, is a series of clear instructions to solve the problem, the algorithm represents a systematic approach to describe the problem-solving strategy mechanism. ----from Baidu Encyclopedia

5. the characteristics of the algorithm : There are five characteristics of the algorithm, namely, there are poor, deterministic, feasibility, input, output, any algorithm must have these five characteristics.

Poor : The algorithm always ends with a certain number of steps, and each step must be completed within a certain amount of time. This means that the algorithm cannot be executed forever and never ends.

certainty : The meaning of each step of the algorithm must be clear and not two semantic

Feasibility : All operations of the algorithm can be implemented in a limited number of times through the implementation of basic operational operations.

input : An algorithm should have 0 or more inputs. 0 input, the algorithm input in the algorithm has been clearly exist, do not need outside the participation, such as a a+b algorithm, in the algorithm fixed a value of 1,b value of 2; When multiple inputs, when using functions to describe the algorithm, the input of the algorithm is the parameter of the function.

output : An algorithm should have one or more outputs, the output is the result of the algorithm processing. When a function is used to describe an algorithm, the output is typically represented by a return value or a formal parameter of a reference type.

6. Algorithm Evaluation: Evaluation of the algorithm from the following 4 aspects of evaluation: correctness, readability, robustness, efficiency

correctness : The correct result can be obtained in valid time with reasonable data input.

readability : The idea of arithmetic is easy for others to understand and master.

robustness : good handling and response to illegal data.

high efficiency: High efficiency is mainly embodied in time and space, time efficient is the algorithm design reasonable, high execution efficiency, the availability of time complexity to measure; space-efficient means that the algorithm occupies a reasonable storage space, which can be measured by spatial complexity.

7. time complexity and spatial complexity of the algorithm :

time Complexity : The time complexity of the algorithm refers to the computational effort required to perform the algorithm. In general, the computer algorithm is the function f (n) of the problem size n, and the time complexity of the algorithm is also remembered as T (N) =0 (f (n)). ----from Baidu Encyclopedia

Common complexity (complexity from low to high):

(1) Chang : O (1) (2) logarithmic order : O (Logn) (3) linear order : O (n) (4) linear logarithmic order : O (Nlogn) (5) Square order : O (n^2 ) (6) cubic order : O (n^3)

An algorithm is composed of more than one statement, the final time complexity of the algorithm generally refers to the worst complexity of these statements (that is, the most complex).

spatial Complexity : the spatial complexity of the algorithm refers to the memory space that the algorithm needs to consume. The computation and representation method is similar to the time complexity, which is generally expressed by the asymptotic behavior of complexity, and the spatial complexity is denoted as s (n) =o (f (n)). Compared with the complexity, the spatial complexity analysis is much simpler. ----from Baidu Encyclopedia

spatial complexity of common algorithms :

for (int i=0;i<n/2;i++)


temp = A[i];

A[i] = a[n-i-1];

A[n-i-1] = t;


The algorithm above is to store a one-dimensional array of n in reverse order into the original array, it is obvious that the algorithm only used an additional variable temp, and the problem size n independent, the space Complexity of O (1)

for (i=0;i<n;i++)

B[i] = a[n-i-1];

for (i=0;i<n;i++)

A[i] = B[i];

The above algorithm borrows another array B of size n, with a time complexity of O (n)

1. Data structure and algorithm introduction

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.