You are on page 1of 76

Lecture 1

Data Structures and Program


Design in C++
Liang Lin ()

linliang@ieee.org

School of Software, Sun Yat-Sen University


2011

Acknowledgement: many slides come from Prof. Chengying Gao

Data Structures and Program


Design in C++

What is Data structure

Abstract Data Structure

Algorithm Specification

Algorithm Analysis

What is Data Structure?


1

,
)

001
002
003
004

S01
L01
S01
S02

001003

001,

002,

002..

002,.

004

001,003,

004,.

..

What is Data Structure?


2

..

..

...

...

...

...

100M

100M
2

100M

200M

200M

200M

200M

1
2

What is Data Structure?

0,1,2,3,4,5,6,7,8,910

(1,2)(1.1,1.2)

1.

()
(Beijing)

2.

3.:

9,8,7,6,5,4,3,2,1,0

0,2,4,6,8,1,3,5,7,9

Terms

C9292

Terms

Terms

(Data It
em)

Terms
:

3N

6
""""""
""""
""

Terms


""

Terms

4
1.. R1 = {}
2. R1 = {(d1, d2), (d2, d3), , (d(n-1),
dn)}, d1,dn,
.
3.(D, {R1}) ,
, .
4. (D, {R1}),
.

M.
,():
S: D -> M
Dd,S(d) M,
R
, :


:
. .
, D = {d1, d2, d3, d4, d5}
R = {(d1,d2), (d2, d3), (d3, d4),(d4, d5)}
,
d1200.

200

d1

201

d2

202

d3

203

d4

204

d5


,
, :

, . ,
R = {(d1, d2), (d1, d3), (d2, d4), (d2, d5), (d3, d6)}

d1
d1

d3

d3

d2

d4

d2

d5

d6

d4

d5

d6

What is Data structure

Abstract Data Structure

Algorithm Specification

Algorithm Analysis

Abstract Data Type


DefinitionData Type = { Objects } { Operations }
Example int = { 0, 1, 2, , INT_MAX, INT_MIN }
{ , , , , , }
DefinitionAn Abstract Data Type (ADT) is a data
type that is organized in such a way that the specification on
the objects and specification of the operations on the objects
are separated from the representation of the objects and the
implementation on the operations.

Abstract Data Type

ADT =D,S,P
DSD
PD
ADT <> is
Data:

<>

Operations:

<>
End <>

Abstract Data Type

ADT

ADTADT ADT
ADTADT

ADTOO

C++ADTclass,
C++ADT

class
class Circle {
private:
float r, x, y;
public:
Circle(float v1,float v2, float v3);
float area ()= {return pi*r*r};

};

ADTADTtemplate

What is Data structure

Abstract Data Structure

Algorithm Specification

Algorithm Analysis

Algorithm SpecificationDiscussion
DefinitionAn algorithm is a finite set of instructions that, if
followed, accomplishes a particular task. In addition, all
algorithms must satisfy the following criteria:
(1) Input: There are zero or more quantities that are externally
supplied.
(2) Output : At least one quantity is produced.

Algorithm Specification
(3) Finiteness: If we trace out the instructions of an algorithm,
then for all cases, the algorithm terminates after finite number
of instructions.
(4) Definiteness: Each instruction is clear and unambiguous.
(5) Effectiveness: Every instruction must be carried out by
enough basic operators, in principle, by a person using only
pencil and paper.

Algorithm Specification
.
, ,
(

). ,
.
, :

.
, ;
,

Algorithm Specification
:
(1) , ;

(2) ,
;
(3) ;

(4) , .

Algorithm Specification
:
(1) : ;

(2) : , ;
(3) :
;

(4) : ;
(5) : ,
.

Algorithm VS Program: Discussion

A program is written in some programming language, and


does not have to be finite (e.g. an operation system).
Ega wait loop in a operation system
while (1){
if any new incoming job {do it}
}

An algorithm can be described by human languages, flow


charts, some programming languages, or pseudo-code.

:Discussion
1
2

Algorithm Design
Example 1 Selection Sort: Sort a set of n 1 integers
a1, a2, , an in increasing order.
From those integers that are currently unsorted, find
the smallest and place it next in the sorted list.
Where?

Where and how


are they stored?

Sort = Find the smallest integer + Interchange it with list[i].

void selectSort (int a[], const int n) {

// a[0], , a[n-1]
for (int i = 0; i < n -1; i++) {
int j = i;
// a[i]a[n-1]a[j]
for (int k = i + 1; k < n, k++)
if (a[k] < a[j]) j = k;
// j
//a[i]a[j]
int temp t = a[i]; a[i]= a[j]; a[j] = t;
}
}

Template()

C++

Why using Template in program design?

//
int GetMax( int a, int b )
{
return( a > b ) ? a : b;
}
//
long GetMax( long a, long b )
{
return( a > b ) ? a : b;
}

Why using Template in program design?


//
double GetMax( double a, double b ) {
return( a > b ) ? a : b;
}
//
char GettMax( char a, char b ) {
return( a > b ) ? a : b;
}

Why using Template in program design?

//Typeintlongdoublechar

Type GetMax( Type a, Type b ) {


return( a > b ) ? a, b;
}

Why using Template in program design?

C++

Practice

Practice 1Implement Selection Sort Using


Templates

template <typename T> class dataList1{


private:

T *element;
int arraySize;
void swap(const int m1, const int m2);
int minKey( const int low, const int high);
public:
dataList( int size = 10): arraySize(size),
element(new T[size]){}
~dataList(){delete [] element;}
void sort();
};

template <typename T>


void dataList<T> :: swap( const int m1, const int m2){
T temp = element[m1];
element[m1]=element[m2];
element[m2]= temp;

}
template <class T>
int dataList<T>::minKey( const int low, const int high){

int min = low;


for (int k=low+1;k<=high;k++)
if(element[k] < element[min]) min = k;
return min;
}

template <class T> void dataList<T>::sort(){


for(int i =0; i<arraySize-1; i++){
int j = minKey(i, arraySize-1);
if (j!= i ) swap(j,i);
}

What is Data structure

Abstract Data Structure

Algorithm Specification

Algorithm Analysis

Algorithm Analyses
, ().
, ,

.
: .
: (Correctness)

(Robustness)(Readability);
: (Time Complexity)
(Space Complexity).

: .

Algorithm Analyses
()
(1) : ;

(2) : ,
,
;

(3) : ,
.

Algorithm Analyses
()
:
,

.
,
.

Algorithm Analyses
()
,

,
.
.

,
.

Algorithm Analyses
()
(1)
,
. =
+
, .
(2)

: n, T(n).
T(n).
T(n)n, : T(n) = c1nk + c2nk-1 + ,
.

Algorithm Analyses

cg(n)
f(n)

cg(n)

()

(3)
1: f(n)g(n), c, n0

Z+,

Z+,

n0

n n0, :

f(n) cg(n), , f(n) = O(g(n)).


, T(n) = 2.7n3 + 3.8n2 + 53, T(n) = O(n3).
, O(1).
:

O(1) < O(log2n) < O(n) < O(nlog2n) < O(n2) < O(n3) < O(2n)
2: f(n)g(n), c, n0 Z+, n Z+, n n0, :

cg(n) f(n), , f(n) = (g(n)).


3: f(n) = O(g(n)), f(n) = (g(n)), , f(n) = (g(n)).

Time Complexity

f(n)
c, n
T(n) <= c f(n)

T(n) = O(f(n))
f(n) = 3n2 +12n, T(n) = O(n2)

T(n)=O(n3)n
n3

Time Complexity
T(n)= O(f(n))
f(n)n
n
f(n)O
f(n)T(n)T(n)

nT(n)

Time Complexity Analysis


1

CA*B
define n
MATRIXMLT(float A[n][n],float B[n][n],float C[n][n])
{
int i,j,k;
for(i=0;i<n;i++)
//n+1
for(j=0;j<n;j++)
//n(n+1)
{
C[i][j]=0;
//n*n
for( k=0;k<n;k++)
//n*n*(n+1)
C[i][j]=A[i][k]*B[k][j] //n*n*n
}
}
T(n ) 2n 3 3n 2 2n 1

T(n ) O(n 3 )

Algorithm Analyses
()
(4)

: n, T(n).
1). = O(1)
2). 1 = O(1) + O(1)
3). 2(if /else) = O(1) + max(O(1),

O(2))
4). 12 =

max(O(1), O(2), , O(n))


5). =
6). = s T(s)

Algorithm Analyses
()
(5)
1). O(1) + O(1) = O(1)
2). O(1) + O(1) + + O(1) = O(k)
3). O(f1(n)) + O(f2(n)) = max(O(f1(n)), O(f2(n)))
4). O(mf(n)) O(f1(n)) + O(f2(n)) + + O(fm(n)) O(mF(n))

: f(n) = min(O(f1(n)), O(f2(n)), , O(fm(n)))


F(n) = max(O(f1(n)), O(f2(n)), , O(fm(n)))
5). O(1) + O(f(n)) = O(f(n))
6). O(m) O(n) = O(mn)

7). O(m) O(m) O(1) O(0)

Algorithm AnalysesPractice
()
(6)
. Ann, Bnn, A B.
#define N 20
void Multiply(int a[][N], int b[][N])
{
for (i = 0; i < n; i++)
for ( j = 0; j < n; j++) {
c[i][ j] = 0;
for (k = 0; k < n; k++) c[i][ j] += a[i][k]*b[k][ j];
}
}
, O(n3).

n (O(1)
+ O(n))
n O(n2) = O(n3)
= n O(n)
2) = O(n)
n
O(1)
=
O(n

Algorithm Analyses: Practice


()
(6)
. a[0..n-1]
void Sort(int a[][N], int n)
{
for (i = 0; i < n-1; i++) {
j = i;
// i
for (k = i+1; k < n; k++)
if (a[k] < a[ j]) j = k;
if ( j != i) a[ j] a[i];
}
} // End of Sort
, nSortO(n2).

Algorithm Analyses: Practice


i=0..n-2(O(1) + O(n-i-1) + O(1))
= i=0..n-2O(n-i-1)
= O(n-1) + O(n-2) + + O(1)

void Sort(int a[][N], int n)


= O((n-1)n/2)
{
= O(n2 - n)
for (i = 0; i < n-1; i++) {
= O(n2)
j = i;
// i
for (k = i+1; k < n; k++)
(n-i-1) O(1)
if (a[k] < a[ j]) j = k; = O(n-i-1)
if ( j != i) a[ j] a[i];
}
} // End of Sort
, nSortO(n2).

Time Complexity Analysis


1

x = 0; y = 0;

for (k=1;k<=n;k++)
x++;
for (i=1;i<=n;i++)

for (j=1;j<=n;j++)
y++; //n*n
T(n) = On2

Time Complexity Analysis


2if
thenelse
3n

T(n)= O(1)
temp = i;
i = j;
j = temp;

Time Complexity Analysis


4T(n)
n

(The Average Case)

(The Worst Case)

:n
n
void bubble-sort(int a[]int n)
for(I=n-1;change=TURE;I>1 && change;--I)
{// a
change=false;
for(j=0;j<I;++j)
if (a[j]>a[j+1]) {
a[j] a[j+1];
change=TURE}
}//bubble-sort

0;
1+2+3++n-1 =n(n-1)/2;

Practical Complexity
Time
1
log n
n
n log n
n2
n3
2n
n!

Name
constant
logarithmic
linear
log linear
quadratic
cubic
exponential
factorial

1
1
0
1
0
1
1
2
1

Instance characteristic n
2 4
8
16
1 1
1
1
1 2
3
4
2 4
8
16
2 8
24
64
4 16
64
256
8 64
512
4096
4 16
256
65536
2 24 40326 2092278988000

32
1
5
32
160
1024
32768
4294967296
26313 1033

Note: When compare the complexities of two programs


asymptotically, make sure that n is sufficiently large.

2n

n2

60

50

40

n log n

f 30
20

n
10

Log n

10

T1(n) = 100nlog n , T2(n) = n2,


n

T1(n)

T2(n)

100

0.06

0.01

1000

10000

13

100

100000

166

1000000

33

16666=11.5

10

T(n)

n/n

10n

1000

10000

n=10n

10

20n

500

5000

n=10n

10

5nlogn

250

1842

3n<n<10n

7.37

2n2

70

223

n=101/2n

3.16

2n

13

16

n=n+3

n:n:

{
action1(); // action1O(f1(n))
actions(); //action2O(f2(n))
}

O(max(f1(n), f2(n))).
O(f1(n))O(f2(n)),
O(max(f1(n), f2(n))).
(n) = T1(n) + T2(n) = O(f1(n)) + O(f2(n)) = O(max(f1(n), f2(n)))

for (i=1; i<= n, i++)

action()
forT1(n)= O(n),action
O(f(n))
O(nf(n)).
O(f1(n)),
O(f2(n)),
O(f1(n)f2(n)).
(n) = T1(n) T2(n) = O(f1(n)) O(f2(n)) = O(f1(n) f2(n))

Space Complexity

S(n) = O(f(n)).

Write a program that will print the calendar of the current year.
Modify the program so that it will read a year number and print the
calendar for that year. A year is a leap year (that is, February has 29 days
instead of 28 days) if it is a multiple of 4, except that century years
(multiples of 100) and leap years only when the year is divisible by 400.
Hence the year 1900 is not a leap year, but the year 2000 is a leap year.
Modify the program so that it will accept any date(day, month, year) and
print the day of the week for that date.
Modify the program so that it will read two dates and print the number of
days from one to the other.
Using the rules on leap years, show that the sequence of calendars
repeats exactly every 400 years

Sicily
1134

1093

You might also like