Data structure Introduction and algorithm efficiency measurement

Source: Internet
Author: User

First, the data structure introduction 1. characteristics, concepts and relationships of data 1.1. The concept and characteristics of data

In a computer, data refers to objects that are manipulated by a program to describe objective things.
Features: Can be entered into the computer, can be processed by the program.

1.2. New concepts in the data

-Data elements: the basic unit for composing data
-Data item: A data element has several data items composed
-Data Objects: collections of data elements of the same nature
Example Analysis:
[OBJC] View plain copy
struct Student//data type
{
int age;
Charchar *name;
};
Student S//Data elements
Student SARRSY[10]; Data Objects
S.name = "Eric"; Data item
S.age = 23;

1.3. The relationship between data

The data structure refers to the relationship between data elements in the data object, and it is not independent, and there are certain relationships (combinatorial, linear, tree, graph ...). ), or structure.
Data structure is a collection of certain relationships with each other, divided into logical structure and physical structure.

(1), Logical structure


Collection structure: There is no special relationship between data elements, only the same set.
Linear structure: the relationship between data elements
Tree structure: a one-to-many hierarchical relationship exists between data elements
Graphical structure: A many-to-many relationship between data elements

(2), physical structure

The physical structure is the logical structure in the computer storage form, divided into sequential storage structure and chain storage structure.
The sequential storage structure stores the data in a contiguous storage unit.
The chained storage structure stores the data in any storage unit and finds the associated data element by saving the address.

Second, the algorithm introduction

The algorithm is a description of the specific problem solving procedure, and it is a method and idea to solve the problem independently.

1, the characteristics of the algorithm
输入:有0个或多个输入输出:至少有1个或多个输出有穷性:算法在有限的步骤后应该自动结束而不会无限循环。确定性:算法中的每个步骤都有确定的含义,不会出现二义性可行性:算法的每一步都是可行的正确性:算法对于合法数据能够得到满足要求的结果,能够处理非法输入,并得到合理的结果。可读性:算法要便于阅读、理解和交流健壮性:算法不应该得到莫名其妙的结果性价比:利用最少的资源得到满足要求的结果
2. Measurement of algorithm efficiency

Efficiency evaluation is the most important additional characteristic of the algorithm in engineering.

(1), post-mortem statistical method

Compare the processing time of different algorithms for the same set of input data.
Disadvantages:
A, in order to obtain the different algorithm running processing time must write the corresponding program
B, run processing time heavily dependent on hardware and runtime environment
C, the test data selection of the algorithm is difficult

(2), pre-analysis and statistics

Evaluating the efficiency of algorithms based on statistical methods
The main factors that affect the efficiency of the algorithm:
A, the algorithm adopts the strategy and the method
B, the input scale of the problem
C, compiler-generated code
D, the speed of computer execution
Simple estimation of algorithmic efficiency:



The number of operations for the key parts of the three summation algorithms is 2n,n,1. As the size of the problem increases, the number of operations will become larger and the difference in efficiency will increase.

Comparison of the number of different algorithm operations
Example of a comparison of the number of algorithmic operations:

N<=3, algorithm B is better than algorithm a. As the size of n increases, the advantage of the algorithm A is more obvious.
Example of the comparison of the number of algorithmic operations two:

When N=1, the algorithm C is the same as the efficiency of algorithm d. With the increase of N scale, the algorithm C advantage is obviously better than the algorithm d.
When judging the efficiency of an algorithm, the constants and other sub-orders in the number of operations can often be ignored, only the highest order is concerned.

3, the complexity of the algorithm (1) The time complexity of the algorithm

The algorithm time complexity is the qualitative description of the time demand after the algorithm is run.
Because the efficiency of the algorithm is mainly concerned, the time complexity of the algorithm is mainly discussed.
O notation
The efficiency of the algorithm relies heavily on the number of operations (Operations), the estimation of the number of operations can be used as the estimation of time complexity, and the highest order of the number of operations is concerned first.
O (2) ==>o (1)
O (3n+3) ==> O (3n) ==>o (n)
O (3n^2+n+4) ==>o (n^2)
Common time Complexity:

Practice Topics:

(2) Spatial complexity of the algorithm

The spatial complexity of the algorithm is a qualitative description of the space demand after the algorithm is run.
Typically, S (n) is used to represent the spatial complexity of the algorithm. The spatial complexity is deduced using the derivation method of time complexity.
When the size of the memory space required by the algorithm is constant, the spatial complexity of the algorithm is S (1).
Typically, the time complexity of the algorithm is more concerned. You can reduce the complexity of time by adding extra space.
The algorithm is the step to solve the specific problem, and the data structure is the carrier of the algorithm solving problem.

4. Algorithm example
一个数组中存储着1——1000的数字,每个数字可能出现多次或者不出现,找出出现次数最多的数字。
void search(int array[], int len){  //总计可能出现1000种可能值  int sp[1000] = {0};  int max = 0;  for(int i = 0; i < len; i++)  {      //遍历数组,数组中某个数组出现一次增加统计1次      sp[array[i] - 1]++;  }  for(int i = 0; i < 1000; i++)  {      if(max < sp[i])      {          max = sp[i];      }  }  for(int i = 0; i< 1000; i++)  {      if(max == sp[i])      {          cout << "Number:" << i + 1 << endl;          cout << "Count:" << max << endl;      }  }}

The time efficiency of the algorithm is O (n), using the space-changing time.
Reference from the DT course.

Data structure Introduction and algorithm efficiency measurement

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.