Zhejiang University Chen Yue data structure Lecture: Basic Concepts


The first lecture the basic concepts

What is a data structure

Data Structures and Algorithms

Example 1. The bookshelves book

Book placed to make two related operations to facilitate the achievement:
1. new book how insert (casually put, according to the phonetic put, put by category + alphabetical order)
2. how to find some of this specific book (exhausted, binary search)

Summary: When asked how a data organization, in fact, with the size of the data of a relationship
is not the same scale of the problem, the difficulty of dealing with them is not the same, to solve the efficiency of the process, with the organization of the data has. turn off

Example 2. Write a function, enter a positive integer N, the order of printing of all positive integers of 1-N.

A first method for loop

The second recursion

Summary: Although simple recursion, but recursion occupation of space very frightening, when N is 100000, the wording of the recursive use of space are accounted for, is not enough, then it explodes, causing abnormal termination. Efficient method to solve the problem is also related with the utilization efficiency of space.

Example 3. Write a calculated value to the given point in the polynomial of x

The first method directly translated polynomial (simple and crude directly)

The second use of the law to simplify the re-translation polynomial

How to test the program's running time
clock () function: capture process from start to run clock () is called when the time spent, time in colock tick, that is the time RBI
constant CLK_TCK: clock hit points per second machine clock go

#include<stdio.h>
#include<time.h>//调用clock函数需要的头文件

clock_t start,stop;//clock_t是clock函数返回的变量类型
double duration;
int mian()
{
    start=clock();//开始计时
    myfuncation();//把被测函数加在这里
    stop=clock();//停止计时
    duration=((douboe)(stop-star))/CLK_TCK;
    //得出运行时间
    
}

When the running time is too short less than 1 unit, we take repeated, and finally divided by the number of times the word come running time

Summary: The first method running time of an order of magnitude slower than the second efficiency problem-solving approach, with clever algorithms related degree.

In the end what is a data structure?

  • Data objects are organized in the computer
  • Logical structure (one: many linear structure: many-Tree: FIG.)
  • Physical storage structure (linked list, an array, etc.)
  • It must be applied with the data object on which a series of operations associated
  • These operations complete the method used was the algorithm
  • Abstract data type
  • type of data
    • Set of data objects
    • Operation data set associated with a set
  • Abstract: Description of data types specific implementation does not rely
    • Regardless of the machine to store data
    • Regardless of the physical structure of the data storage
    • And algorithms and programming operations are unrelated

Description only set of data objects and related operations set what does not involve how to do the problem

Summary: The abstract is, relatively clear you know this type of data is doing, what is the use, do not tangle with what the language, they do not tangle data type, allowing you to more clearly understand this data structure, such as functions max (), you use it when you know it is to find the maximum of two numbers, you do not have to tangle its data type, and no tangled realize in what language, the simplest way is the most important reason: Interface and separation! Reusability stronger!

What is the algorithm

  • algorithm
  • A finite set of instructions
  • Accept some input (in some cases need to enter)
  • Generating an output
  • Terminated after a certain limited steps
  • Each instruction must
    • There are sufficiently clear objectives. Not ambiguous
    • Within the scope of computer can handle
    • The description should not rely on any kind of computer language and the specific means of achieving

What is a good algorithm

  • Space complexity S (n)
  • Time complexity T (n)

Example: PRINT N Recursive

When n is large enough, the space will be covered
for loop footprint is always a constant

  • When analyzing the efficiency of algorithms in general, we often focus on two complexity
  • Worst-case complexity
  • Average complexity

The complexity of progressive representation

Algorithm does not do a very fine analysis of
a rough estimate of trend growth

Complexity analysis tips

  • Complexity take maximum
  • for the complexity of the cycle time is equal to the number of cycles * complexity of the loop body code
  • The complexity of the structure depends on if else if the condition is determined complexity and complexity of both the leg portions, overall complexity, whichever is the largest

Application examples: Maximum row and the problem child

在线处理 ,灵活巧妙,简化问题+找规律 
#include<bits/stdc++.h>//最大数据10的5次方时21ms 
using namespace std;
int num[100000];
int maxn=0;
int main()
{
	int n=0,sum=0;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&num[i]);
		maxn=maxn+num[i];
		if(maxn<0)
		{
			maxn=0;
		}
		if(maxn>sum)
		{
			sum=maxn;
		}
		//printf("%d",sum);
	}
	if(sum<0)
	{
		printf("0");
	 } else
	printf("%d",sum);
	return 0;
 } 
#include<bits/stdc++.h>//最大数据10的5次方时3700ms 爆力法 
using namespace std;
int num[100000];
int maxn=0;
int main()
{
	int n=0,sum=0;
	scanf("%d",&n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&num[i]);
	}
	for(int i=0;i<n;i++)
	{
		maxn=0;
		for(int j=i;j<n;j++)
		{
			maxn=maxn+num[j];
			if(maxn>sum)
			{
				sum=maxn;
			}
		}
	}
	if(sum<0)
	{
		printf("0");
	 } else
	printf("%d",sum);
	return 0;
} 
#include<bits/stdc++.h>//最大数据10的5次方时3700ms .分而治之 
略
Published an original article · won praise 0 · Views 13

Guess you like

Origin blog.csdn.net/qq_45090427/article/details/105012520