Data Structures-basic concepts [Chapter1]

Source: Internet
Author: User

Data

    • There's no definite definition--but there's one I really like.

The data structure is the physical implementation of ADT (abstract data type).

    • Related to Algorithms

The efficiency of the problem solving method is as follows:

    • How data is organized about → bookshelf issues
    • About the efficiency of space utilization

Circular Solution PRINTN

void printn (int N) {    int i;    for (i = 1; i<n; i++)         printf ("%d\n", I);    return;}

Recursive resolution

void printn (int N) {      if (n) {           printn (N-1);           printf ("%d\n", n);     }    return;}    

The principle of recursion here is that when N >=1 will constantly call PRINTN (N-1), such as piling up on a stack ...
Until called to PRINTN (1), at which point the most printn (0) printn (0) does not meet the conditions of if (N), will return directly, and then each call on the stack on each return return, forming from 1 printing to N ....

Process should

PRINTN (N) Print N

|

PRINTN (N-1) Print N-1

|

.......

|

PRINTN (0) return

If n is too large, it will run out of stack memory, and the recursive algorithm will not be able to execute

Note that because the stack is one layer passed to the previous layer, the print order is 1, 2, .... N

    • It's about the sophistication of the algorithm.

The value of the calculated polynomial

Definition-based algorithms

f (x) = a0 + a1x +...+an-1xn-1 + anxn

Double f (int n, double a[], double x) {int i;double p[0] = a[0];for (int i = 1; I <= n; i++) p + = (a[i] * POW (x,i)) retur n P;}

A clever bit of algorithm

f (x) = a0 + x (A1 +x (... + (an-1 + x (AN)))

Double f (int n, double a[], double x) {int i;double p = a[n];for (int i = n; i > 0; i--) p = a[i-1] + X*p;return p;}

Abstract data types

Care about the set of data objects and related operations "what", not so concerned about how to implement

Example

Type name: Matrices (Matrix)

Object Set: Am,n = (AIJ) (i = 1,.... M;j=1,.... N

Operation set:

Matrix Create (int M, int N)

int Getmaxrow (Matrix A)

int Getmaxcol (Matrix B)

ElementType getentry (Matrix, int i, int j)

Matrix Add (Matrix A, Matrix B)

Matrix Multiply (Matrix A, Matrix B)

....

In fact, I think if you learn an object-oriented language will be easier to understand, the class is also abstract, is we encapsulated, and then left the interface for external use

Abstract data types have a bit of an object-oriented sense, the same data type, such as stack, we can write in Python, we can write in C (the object of C is not so strong), but we call the pop function, the return is the stack on the top element, And ElementType just tell us this stack can be installed int,float, even struct, understand a data type, after this kind of implement, use will be presented once and for all the state

In the course of learning, I think the first thing to be abstract, such as a queue, such as a stack, for example, linked list first understand some unique operations and results for this type of data
After abstraction to code, beautify, optimize, at the same time the code of this type of data to stay, because it is always handy to write, and the opportunity to use more ...

Algorithm

The algorithm as its name implies: Calculation method

void Selectionsort (int list[], int N) {
/* n integers list[0] ... LIST[N-1] for non-descending sort */for (i = 0; i < N; i++) {minposition = Scanformin (List, I, N-1);
/* from list[i] to list[n–1], and assign its position to Minposition */swap (List[i], list[minposition]);
/* Change the smallest element of the unsorted part to the last position of the ordered part */}}

Complexity of the algorithm

Space complexity S (n)

Time complexity T (n)

Maximum child columns and issues

Sequence {A1,a2,... for a given n integers An}, the maximum value of the function f (i,j) = max (0,∑JK = i max AK)

Algorithm one:

int MaxSubseqSum1 (int a[], int N) {int thissum, maxsum = 0;int I, j, k;for (i = 0; i < N; i++) {for (j = i; j < N; j+ +) {thissum = 0;for (k = i; k<= j; k++) Thissum + = A[k];if (Thissum > Maxsum) maxsum = Thissum;}} return maxsum;}

Easy to understand

But T (N) = O (N3)

Algorithm two:

int MaxSubseqSum2 (int a[], int N) {int thissum, maxsum =  0;int i, j;for (i = 0; i < N; i++) {thissum = 0;for (j = i; J < N; J + +) {thissum + = a[j];if (Thissum > Maxsum) maxsum = Thissum;}} return maxsum;}

T (N) = O (N2)

Algorithm three
Divide

....

Algorithm Four

int MaxSubseqSum4 (int a[], int N) {int thissum, maxsum =  0;int i; Thissum = Maxsum =  0;for (i = 0; i < N; i++) {thissum + = a[i];if (Thissum > Maxsum) maxsum = Thissum;else if (thiss Um <  0) thissum = 0;}} return maxsum;}

T (n) = O (n)

Data Structures-basic concepts [Chapter1]

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.