0% found this document useful (0 votes)
25 views

DSA (1)

The document provides an introduction to algorithms, detailing their definition, properties, and the analysis of algorithms in terms of time and space complexity. It discusses the importance of efficient algorithm design, various approaches to problem-solving, and the use of pseudocode for algorithm representation. Additionally, it outlines the specifications and conventions for writing algorithms and pseudocode.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

DSA (1)

The document provides an introduction to algorithms, detailing their definition, properties, and the analysis of algorithms in terms of time and space complexity. It discusses the importance of efficient algorithm design, various approaches to problem-solving, and the use of pseudocode for algorithm representation. Additionally, it outlines the specifications and conventions for writing algorithms and pseudocode.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 157

CSCI2201T

Data
S t ru c tu r es

Lect-1-
Introduction to
Algorithms

P re s e nt ed B y : R a m a R a ni
R e se a rch S c hola r,
D e p t. of C om p ut e r S cie nce ,
P u nj a bi Un ive rs it y , P a t ia la .
Section-A

Analysis of Algorithms: In t r o d u c t io n t o A lg o r it h m s ,
th e ir d es ign an d a n aly s is , c o m p a r is o n o f a lg o r it h m s ,
co m p lex it y m ea s u r e in t er m s o f t im e a n d s p a c e.
Title:
INTRODUCTION

▶Algorithm : S te p b y s te p pr oc e d u re t o s o lve a co m p u ta tio n al


pr ob le m is ca lle d A lg o r ith m .
or
▶A n A lg o rit h m is a s t e p -b y-s t e p p lan for a co m p u ta tio n al
pr oc e du r e t h at po s s ib ly b e g in s w it h a n in pu t an d yie ld s an
ou t p u t valu e in a fin ite n u m b e r o f s t e p s in or de r t o s o lve a
pa rt icu lar pr ob le m .
▶Th e w or d A lg or it h m m e a n s ” A set of finite rules or
instructions to be followed in calculations or other problem-solving
operations ”
▶ ” A procedure for solving a mathematical problem in a fin it e
n u m b e r of s t e ps that fr e q u e n t ly in volve s re cu r s ive
op e ra t io n s ” .
Contd…

▶ A n alg o rit h m is a s e t o f s te p s o f o p e ra tio n s t o s o lve a


p ro b le m p e rfo rm in g calc u lat ion , d at a p ro ce s s in g , an d
au t o m at e d r e as on in g t as k s .

▶ A n a lg o r it h m is an e fficie n t m e th o d t h a t ca n b e e x p re s s e d
w it h in fin it e am o u n t o f Tim e an d s pa ce .

▶ Th e im po r ta n t as pe c t s o f alg o rit h m d e s ig n in c lu de cr e at in g
an e fficie n t alg o r it h m t o s olve a p ro b le m in an e fficie n t w ay
u s in g m in im u m t im e an d s p ace .

▶ To s o lve a p ro b le m , d iffe re n t ap pr o ach e s c an b e fo llo w e d .


S o m e of t h e m ca n b e e ffic ie n t w it h r e s p e ct t o t im e
co n s u m p t io n , w h e r e as o t h e r a pp r oa ch e s m ay b e m e m o ry
e ffic ie n t.
Contd..
▶ Th e c o n ce p t o f a n alg o rit h m or ig in a t e d as a m e an s o f
re co rd in g p ro ce d u re s fo r s o lvin g m a th e m a t ic al pr ob le m s s u c h
as fin din g t h e co m m o n divis o r of t w o n u m b e rs o r m u lt ip lyin g
tw o num bers.
PROPERTIES OF ALGORITHM
TO EVALUATE AN ALGORITHM WE HAVE TO SATISFY THE FOLLOWING
CRITERIA:

1.INPUT: Th e A lg o rit h m s h o u ld b e g ive n z e ro or m or e in pu t .


2.OUTPUT: A t le as t o n e q u an t it y is p ro du c e d . F or e ac h in p u t t h e
alg or it h m p ro d u ce d valu e fro m s p e cific t a s k .
3.DEFINITENESS: E a ch in s t ru c t ion is cle a r an d u n am b ig u o u s .
4.FINITENESS: If w e t rac e ou t t h e in s t ru ct io n s o f an alg o rit h m , t h e n
for all cas e s , t h e a lg or ith m t e rm in at e s aft e r a fin it e n u m b e r of
s t e ps .
5.EFFECTIVENESS: E v e ry in s t ru c tio n m u s t ve r y b a s ic s o t h at it can
b e c ar rie d o u t , in p rin cip le , b y a pe r s o n u s in g on ly p e n cil & p ap e r.
6. Feasible: It m u s t b e fe as ib le e n o u g h t o p ro du c e e ac h
in s t ru c t ion .
7. Flexibility: It m u s t b e fle x ib le e n o u g h t o car ry o u t de s ire d
ch a n g e s w it h n o e ffo rt s .
8. Efficient: T h e te rm e fficie n c y is m e as u re d in t e rm s of t im e
an d s pa ce re q u ir e d b y an alg o rit h m t o im p le m e n t. Th u s , a n
alg o rit h m m u s t e n s u re t h at it ta k e s lit t le t im e a n d le s s
m e m o ry s pa ce m e e t in g t h e ac ce p t ab le lim it o f de ve lop m e n t
t im e .
9. Independent: A n a lg or ith m m u s t b e lan g u ag e in d e p e n de n t ,
w h ich m e an s t h a t it s h ou ld m ain ly fo cu s o n th e in p u t a n d t h e
p ro ce d u re re q u ire d t o de r ive t h e o u tp u t in s t e ad o f de pe n d in g
u p on t h e lan g u ag e .
ALGORITHM (CONTD… )

▶A w e ll-d e fin e d c om p u t at io n al pr oc e d u re t h at t ak e s
s om e valu e , o r s e t o f va lu e s , as an d p ro du c e s
s om e valu e , o r s e t o f va lu e s , as

▶W r it t e n in a ps e u do c od e w h ich c an b e im p le m e n t e d
in t h e la n g u ag e o f p r og r am m e r’ s ch o ice .

PSEUDO CODE: A n o t at io n r e s e m b lin g a s im p lifie d


p ro g ra m m in g la n g u ag e , u s e d in p ro g ra m d e s ig n .
Pseudocode

▶P s e u d o co de r e fe rs to an in fo rm al h ig h -le ve l de s cr ipt io n o f t h e
o pe r at in g pr in cip le o f a c om p u t e r p ro g ram o r alg o rit h m .
▶It u s e s s tr u ct u r al co n ve n t io n s o f a s t an d ar d p ro g r am m in g
lan g u a g e in t e n de d fo r h u m an r e ad in g r at h e r th a n t h e m ac h in e
re ad in g .
Advantages of Pseudocode

▶It can b e q u ick ly t ran s fo rm e d in t o an ac tu a l p ro g r am m in g


lan g u a g e t h an a flo w ch a rt s in c e it is s im ilar t o a p ro g r am m in g
lan g u a g e .
▶Th e lay m an o r u s e r c an e as ily u n de r s t an d it.
▶It can b e e as ily m od ifie d a s co m pa re d t o flo w ch ar t s .
▶It s im ple m e n t a tio n is b e n e fic ia l for s t ru c tu r e d , d e s ig n e d
e le m e n t s . It ca n e as ily d e t e ct an e rr or b e f or e t r an s fo r m in g it
in t o a c od e .
Disadvantages of Pseudocode

▶S in c e it do e s n o t in co rp or at e an y s t an d ard iz e d s t yle or for m at ,


it c an var y fro m on e u s e r o r pr o g ram m e r to an o t h e r.
▶E r ro r p os s ib ility is h ig h e r w h ile t ra n s fo rm in g in t o a c od e .
Differences

A lg o rit h m P r o g ram
1 .A t d e s ig n ph a s e 1 .A t Im ple m e n t a tio n ph a s e
2 .N a t u ral lan g u a g e 2 .w rit t e n in a n y
p ro g ra m m in g
lan g u a g e

3 .P e r s o n s h ou ld h ave 3 .P ro g ra m m e r
D o m ain k n ow le d g e
4 .A n alyz e 4 .Te s tin g
ALGORITHM SPECIFICATION
A lg o rit h m can b e d e s cr ib e d (R e p re s e n t) in fo u r w ay s .
1 .N at u ra l la n g u ag e lik e E n g lis h :
W h e n t h is w ay is c h oo s e s , c are s h o u ld b e t ak e n , w e
s h o u ld e n s u r e th a t e a ch & e ve r y s t a te m e n t is d e fin ite .
(n o a m b ig u it y)

2 . G ra ph ic re p re s e n ta t io n ca lle d flo w ch ar t :
T h is m e t h o d w ill w o rk w e ll w h e n th e alg o rit h m is s m all&
s im ple .
3 . P s e u d o -co d e M e th o d :
In th is m e t h o d, w e s h ou ld t yp ically de s cr ib e alg o rit h m s as
pr og r am ,w h ic h r e s e m b le s la n g u ag e lik e P as cal &
A lg o l(A lg o rit h m ic La n g u ag e ) .
4 .P ro g ram m in g L an g u ag e :
w e h ave t o u s e p ro g r am m in g la n g u ag e t o w rit e alg o rit h m s lik e
C , C + + ,JA V A e t c.
PSEUDO-CODE CONVENTIONS
1. C om m e n t s b e g in w ith // an d c o n tin u e u n t il t h e e n d o f lin e .
2. B lo ck s a re in d ic at e d w it h m at ch in g b ra ce s { a n d }.
3. A n id e n t ifie r b e g in s w ith a le t t e r. Th e d at a t yp e s o f varia b le s
ar e n ot e x p lic it ly de c lare d .
n o de = r e co rd
{
d at a t yp e 1 da t a 1 ;
d at a t y pe n da t a n ;
n od e * lin k ;
}
4 . Th e r e are t w o B oo le an va lu e s TRUE a n d FALSE.
L o g ica l O p e r at or s
AND, OR, NOT
R e lat io n al O p e rat o rs
<, <=,>,>=, =, !=
5 . A s s ig n m e n t of valu e s t o va riab le s is do n e u s in g t h e
as s ig n m e n t s t a te m e n t. <V a riab le > :=
<e x p re s s ion > ;
6 . C o m po u n d d at a t yp e s ca n b e fo r m e d w ith r e co rd s . H e re is an
e x am p le ,
N od e . R e co rd
{
d at a t yp e – 1 d at a-1 ;
.
.
.
d at a t yp e – n d at a – n ;
n o de * lin k ;
}
H e re lin k is a po in t e r t o t h e re c o rd t y pe n o de . In d iv id u al
da ta it e m s o f a re c o rd c an b e ac ce s s e d w it h  a n d p e rio d.
7 . T he f ollow ing lo op ing s t a te m e nt s a re em p loy e d .
F o r, w h ile a n d re p e a t- un til W hile Loo p:
W hile < c ond it ion > d o
{
< s t at e m en t- 1 >
..
..
<s t a te m e nt -n >
}
For Loop:
F or v a ria ble : = va lu e- 1 t o v al ue -2 s t ep s t ep d o
{
< s ta t em e nt - 1 >
.
.
.
< s ta t em e nt - n>
}
repeat-until:

re pe a t
<s t at e m e n t -1 >
.
.
.
<s t at e m e n t -n >
u n t il<co n d itio n >

8 . A co n d itio n al s t a te m e n t h as th e fo llow in g fo rm s .

 If < co n dit io n > t h e n <s t at e m e n t >


 If < co n dit io n > t h e n <s t at e m e n t -1 >
E ls e < s t at e m e n t -1 >
Case statement:

C as e
{
: < co n d it io n -1 > : < s t at e m e n t -1 >
.
.
.
: < co n d it io n -n > : <s t at e m e n t -n >
: e ls e : < s t at e m e n t -n + 1 >
}

9 . In p u t an d ou t p u t ar e do n e u s in g t h e in s t ru c tio n s
re ad & w r ite . N o fo rm a t is u s e d t o s pe cify t h e s iz e of
in p u t or ou t p u t q u a n tit ie s
1 0 . T h e re is o n ly on e t yp e o f p ro c e du r e : A lg or it h m , t h e
h e ad in g t a k e s t h e fo r m ,
A lg o rit h m N am e (P ara m e t e r lis t s )
co n s id e r a n e x am ple , t h e follo w in g a lg or ith m fie ld s &
re t u rn s th e m a x im u m o f n g ive n n u m b e r s :
1. alg o rit h m M ax (A ,n )
2. / / A is a n a rr ay o f s iz e n
3. {
4. R e s u lt := A [ 1 ] ;
5. fo r i:= 2 to n do
6. if A [ i] > R e s u lt t h e n
7. R e s u lt := A [ i] ;
8. re tu r n R e s u lt;
9. }
Issue in the study of algorithm

1. How to create an algorithm.


2. How to validate an algorithm.
3. How to analyses an algorithm
4. How to test a program.

1 . How to create an algorithm: To c re a t e a n alg or it h m w e h ave


fo llow in g de s ig n t e c h n iq u e
a) D ivid e & C o n q u e r
b ) G re e d y m e t h od
c) D y n am ic P ro g ra m m in g
d ) B ra n ch & B o u n d
e ) B a ck t ra ck in g
2.How to validate an algorithm: O n ce a n alg o rit h m is c re a te d it is
n e ce s s ar y to s h o w t h at it co m p u te s t h e co r re ct o u tp u t fo r a ll
po s s ib le le g a l in p u t , t h is p ro ce s s is c alle d a lg or ith m
valida t io n .
3.How to analyses an algorithm: A n alys is of a n a lg or ith m o r
pe r for m an c e an a ly s is re fe r s t o t as k o f d e t e rm in in g h o w m u ch
co m pu t in g
Tim e & s t o rag e a lg or ith m s r e q u ire d .
a) C o m pu t in g t im e -T im e co m p le x it y : Fr e q u e n cy o r S te p c o u n t
m e th o d
b ) S t or ag e s pa ce - T o c alcu la te s p ace c o m ple x it y w e h a ve t o
use num ber
o f in p u t u s e d in alg o rit h m s .
4.How to test the program: P ro g ra m is n o th in g b u t an
e x pr e s s io n
for t h e alg o rit h m u s in g a n y p ro g ra m m in g lan g u ag e . T o
te s t a p r og r am
w e n e e d fo llo w in g
a) D e b u g g in g : It is p ro ce s s e s of e x e cu t in g p ro g r am s on
s am p le da ta s e t s
t o d e t e rm in e w h e t h e r fau lt y r e s u lt s oc cu r & if s o c o rre c t
th e m .
b ) P r o filin g or p e rfo r m an ce m e as u re m e n t is t h e p ro c e s s
of e x e c u tin g a
co rr e ct p ro g r am o n d at a s e t a n d m e as u rin g th e t im e &
s p ace it ta k e s
to co m p u te t h e re s u lt.
A N A L Y S IS O F A LG O R IT H M

PRIORI POSTERIORI
1 .D o n e pr ior i t o r u n a lg or it h m 1 .A n alys is a ft e r ru n n in g on a
s pe cific s y s t e m it o n s ys t e m .
2 .Har dw a re in de p e n d e n t 2 .D e p e n de n t o n h a rd w are
3 .Ap p ro x im at e an aly s is 3 .A ct u al s t a tis tic s o f a n
alg o rit h m
4 .D e pe n d e n t on n o of t im e 4 .T h e y d o n o t d o p o s t e rio ri
s ta te m e n ts a re e x e c u te d an aly s is
Problem: Suppose there are 60 students in the class.
How will you calculate the number of absentees in the
class?
P s e u do A p p ro ac h
1 .In it ializ e a va riab le ca lle d as Count t o z e r o, absent t o
z e ro , total to 6 0
2 .FO R E A C H S t u d e n t P R E S E N T DO the follo w in g :
In c re a s e t h e Count b y O n e
3 .Th e n S u b t r act Count fr om total an d s t or e the re s u lt
in absent
4 .D is pla y t h e n u m b e r of a b s e n t s t u de n t s
Problem: Suppose there are 60 students in the class.
How will you calculate the number of absentees in the
class?
A lg o rit h m ic A p p ro ac h :
1 .C o u n t < - 0 , a b s e n t <- 0 , t ot a l < - 6 0
2 .R E P E A T till all s t u d e n ts co u n t e d
Count <- Count + 1
3 .ab s e n t <- t o t al - C o u n t
4 .P rin t " N u m b e r a b s e n t is :" , a b s e n t
Need of Algorithm

1 . T o u n d e rs ta n d t h e b as ic id e a o f t h e pr ob le m .
2 . T o fin d an ap pr o ach t o s olve t h e p ro b le m .
3 . T o im p ro ve t h e e fficie n cy of e x is t in g t e ch n iq u e s .
4 . To u n d e r s t an d t h e b a s ic pr in cip le s o f d e s ig n in g t h e
alg or it h m s . T o c o m pa re t h e p e rfo r m an ce o f t h e a lg or ith m
w it h re s pe c t t o ot h e r t e c h n iq u e s .
6 . It is t h e b e s t m e t h od of d e s c ript io n w it h o u t d e s c rib in g t h e
im p le m e n t at io n d e t ail.
7 . Th e A lg o r ith m g ive s a c le ar d e s c rip tio n o f re q u ir e m e n ts a n d
g oa l o f t h e p r ob le m t o t h e de s ig n e r.
8 . A g oo d de s ig n c an p ro du c e a g oo d s o lu tio n .
9 . T o u n d e rs t an d t h e flo w o f t h e p r ob le m .
Algorithm Design Techniques

▶ An alg o rit h m d e s ig n t e c h n iq u e (o r “ s tr at e g y” or
“ p ara dig m ” ) is a g e n e r al ap p ro ac h to s o lvin g p ro b le m s
alg o rit h m ica lly t h at is a pp licab le to a va rie t y of p ro b le m s
fr om d if fe re n t ar e as o f co m pu t in g .
▶ Learning these techniques is of utmost importance for the
following reasons.
▶ First, they p ro vid e g u id an c e fo r d e s ig n in g alg o rit h m s fo r
n e w pr ob le m s , i.e ., pr ob le m s fo r w h ich t h e re is n o k n o w n
s a t is fac to r y alg o rit h m .
▶ Second, algorithms are t h e c or n e rs to n e o f co m p u te r
s c ie n ce . A lg or it h m d e s ig n t e c h n iq u e s m a k e it p os s ib le t o
cla s s ify alg o rit h m s ac co rd in g t o an u n d e r ly in g d e s ig n
id e a; t h e r e fo re , th e y c an s e r ve as a n a tu r al w ay t o b o t h
ca t e g or iz e an d s tu d y a lg o r ith m s .
Algorithm Design Techniques
▶ P o pu lar A lg o rit h m D e s ig n Te ch n iq u e s
▶ D ivide -a n d- C o n q u e r A pp ro ac h
▶ G re e d y Te c h n iq u e s
▶ D yn a m ic P r o g ram m in g
▶ B r an ch a n d B o u n d
▶ B a ck t ra ck in g A lg o rit h m
▶ R an d o m iz e d A lg o rit h m
Asymptotic Analysis (Growth of Function)
▶ A s ym p t ot ic A n alys is
▶ W h y is A s y m pt o tic A n aly s is Im p or t an t ? A s y m pt o t ic
N o t at ion
▶ B ig O N o t at ion
▶ B ig O m e g a N o t at io n
▶ B ig Th e t a N o t at io n
Brute Force
Description
Br ut e f orc e is a s t ra ig ht f o rw a rd a lg o rit hm ic a p p ro a ch that
sy s t e m a tic a lly e nu m e ra t e s a ll p o s s ib le s olu tio n s to f ind t he co rr ec t
on e . It in v olv e s tr y ing a ll com b in a tio n s w it ho u t us in g a n y o p t im iz a t ion s .
Example Algorithms
▶ Subset Sum Problem
▶ Traveling Salesman Problem (TSP)
▶ String Matching (Naive Approach)
Strengths
▶ Guaranteed Solution: A lw ays fin d s t h e co rr e ct s o lu t ion if o n e
e x is t s .
▶ Simplicity: E a s y t o u n d e rs t an d a n d im p le m e n t.
Weaknesses
▶ Inefficient for Large Inputs: Its t im e co m p le xit y of t e n gr ow s
e xp on e nt ia lly w ith t h e s iz e of t h e in p u t.
▶ Resource-Intensive: C a n be s lo w a nd con s u m e s ign if ica n t
co m p ut a t ion a l r e s ou rce s .
Divide-and-Conquer

▶ Description: B r e ak s a pr ob le m in to s m alle r s u b p ro b le m s ,
s o lve s e a ch re c u rs ive ly, a n d c om b in e s t h e re s u lt s .

▶ Example Algorithms: M e rg e S o r t, Q u ick S o rt , B in ar y S e ar ch .

▶ Strengths: E fficie n t for p ro b le m s t h at c an b e n at u ra lly


d ivid e d (e .g ., s or t in g a n d s e a rc h in g ).

▶ Weaknesses: O ve r h e ad o f re c u rs ive c alls c an b e s ig n ific an t


fo r s m a ll s u b pr ob le m s .
Greedy Techniques

▶ Description: M ak e s t h e b e s t loc al ch o ice at e ac h s t e p, aim in g


fo r a g lo b al op t im u m .

▶ Example Algorithms: P r im 's A lg or ith m , K r u s k al's A lg o rit h m ,


H u ffm a n C o din g .

▶ Strengths: S im p le an d fa s t fo r pr o b le m s w h e r e loc al d e c is ion s


le ad t o t h e g lob a l s olu t io n .

▶ Weaknesses: M ay fail for p ro b le m s t h at r e q u ire c o n s id e rat io n


o f fu tu r e ch o ice s .
Dynamic Programming (DP)

▶ Description: S o lv e s p r ob le m s b y b re a k in g t h e m in t o
o ve rla pp in g s u b pr ob le m s a n d s t o rin g s o lu tio n s t o av oid
re co m pu t at io n .

▶ Example Algorithms: F ib o n ac ci S e q u e n c e , D ijk s t ra’ s


A lg o r it h m , Lo n g e s t C om m o n S u b s e q u e n ce .

▶ Strengths: E f fic ie n t fo r o p tim iz at io n p ro b le m s w it h


o ve rla pp in g s u b pr o b le m s .

▶ Weaknesses: R e q u ire s s ig n ific an t m e m o ry fo r s t or in g


in t e rm e d iat e re s u lts .
Branch and Bound

▶ Description: S y s t e m at ica lly e x p lor e s a ll p os s ib le s o lu t ion s


b y divid in g t h e p ro b le m in t o b ran c h e s an d e lim in at in g t h os e
t h at ca n n ot yie ld b e t t e r r e s u lt s t h an t h e cu rr e n t b e s t .

▶ Example Algorithms: T ra ve lin g S ale s m a n P ro b le m , 0 / 1


K n a ps ack P ro b le m .

▶ Strengths: F in ds t h e e x a ct s olu t io n fo r c o m b in a t or ial


p ro b le m s .

▶ Weaknesses: C om p u t at ion a lly e x p e n s ive d u e t o e x h a u s t ive


s e a rc h .
Backtracking

▶ Description: E x plo re s all p o t e n tia l s olu t io n s an d a b an d on s


p at h s t h at can n o t yie ld va lid re s u lts .

▶ Example Algorithms: N -Q u e e n s P r o b le m , S u d o k u S olve r .

▶ Strengths: U s e fu l fo r c o n s t rain t s a tis fac tio n pr o b le m s .

▶ Weaknesses: C a n b e s low d u e t o e x h au s t ive s e arc h .


Randomized Algorithms

▶ Description: U s e s r an d om n u m b e r s t o m ak e d e cis io n s w it h in
t h e alg o rit h m , in t ro d u cin g ran d o m n e s s t o s o lve pr ob le m s .

▶ Example Algorithms: Q u ic k S or t (R an d o m iz e d ) , M on t e C a rlo


M e t h o ds .

▶ Strengths: O ft e n s im ple r an d fas t e r t h an de t e r m in is tic


co u n t e rp ar t s .

▶ Weaknesses: M a y yie ld diffe re n t r e s u lt s in d if fe re n t r u n s or


fail t o p ro d u ce t h e e x act s o lu t ion .
Linear Search

Description: Lin e a r s e ar ch is a b as ic s e arc h in g alg o rit h m t h a t


s e q u e n t ially ch e c k s e ac h e le m e n t o f a lis t or arr ay t o fin d a
ta rg e t valu e . It d oe s n ot re q u ir e th e d at a t o b e s or t e d.

Example Algorithm
▶ S e ar ch in g for a s p e cific n u m b e r in a n u n s or t e d lis t .
Strengths
▶ S im plicit y: E a s y t o im p le m e n t a n d u n d e rs ta n d.
▶ N o P r e pr o ce s s in g R e q u ire d : W or k s d ire c tly on u n s or t e d
da ta .
Weaknesses
▶ S low for L a rg e In pu t s : H as a t im e c om p le x it y of
𝑂 (𝑛 )O (n ), as it m ay n e e d t o c h e ck e ve r y e le m e n t .
▶ In e fficie n t : N o t s u it ab le fo r s e a rc h in g in larg e d at as e t s .
PERFORMANCE ANALYSIS
Performance Analysis: A n alg or it h m is s aid to b e e fficie n t an d fas t
if it t a k e le s s tim e t o e x e c u te an d co n s u m e s le s s m e m or y s p ace a t
ru n t im e is c alle d P e r for m an c e A n aly s is .
1. SPACE COMPLEXITY:
Th e s p ac e co m p le x ity o f an a lg o r it h m is th e am o u n t of
M e m or y S p ac e re q u ire d b y an alg o rit h m d u rin g c ou r s e of
e x e cu t io n is calle d s p ac e co m p le x it y .T h e re ar e t h re e t yp e s of
s p ace
a) In s t ru c tio n s p ace :e x e c u ta b le p r og r am
b ) D a t a s p ac e : R e q u ire d t o s t or e all t h e c o n s t an t an d va riab le
da ta s pa ce .
c) E n viro n m e n t : It is re q u ir e d t o s t o re e n viro n m e n t in fo r m at ion
n e e de d t o r e s u m e t h e s u s p e n de d s p ac e .
2. TIME COMPLEXITY:
Th e tim e c om p le x it y of a n a lg or ith m is t h e to t al am o u n t of
tim e re q u ir e d b y a n alg o rit h m t o co m ple t e it s e x e cu t io n .
S p ac e c o m p le x it y

N o w th e r e are t w o t y pe s o f s p ac e co m ple x it y

a) C o n s t an t s p ac e co m ple x it y

b ) L in e ar (v aria b le )s p ac e co m ple x it y
1 .C o n s t an t s pa ce co m ple x it y: A fix e d a m ou n t o f s pa ce for a ll
th e in p u t va lu e s .

E x am p le : in t s q u ar e (in t a)
{
re t u rn a* a;
}
H e re alg o rit h m re q u ir e s fix e d a m ou n t o f s p ace fo r all t h e in p u t
valu e s .
2 .L in e ar s pa ce co m p le x ity : T h e s pa ce n e e d e d fo r a lg or ith m is
b as e d o n s iz e .
▶S iz e o f t h e va riab le ‘ n ’ = 1 w o r d
▶A rra y o f a valu e s = n word
▶L oo p varia b le = 1 w o rd
▶S u m var iab le = 1 w o rd
E x a m ple :
in t s u m (in t A [ ] ,in t n )
{ n
in t s u m = 0 ,i; 1
fo r (i= 0 ;i<n ;i+ + ) 1
S u m = s u m + A [ i] ; 1
R e t u rn s u m ;
} A n s : 1 + n + 1 + 1 = n + 3 w o rd s
E x a m ple s :
1 .A lg o r it h m s u m (a,,b ,c )
{
a= 1 0 ; a-1
b=2 0; b -1
c= a+ b ; c -1
}
s (p )= c + s p
3+0 =3
0 (n )= 3
2 . alg o rit h m s u m (a,n )
{
t o ta l-= 0 ; -1
F or i= 1 t o n do -1 ,1
To t al= t o t al+ a[ i] -- n
R e t u rn t ot a l
DAA

Algorithm-1 Algorithm-2 Algorithm-3:recursive procedure


1 .C o n s t an t t im e co m ple x it y : If a pr o g ram re q u ir e d fix e d
am o u n t o f t im e fo r all in p u t va lu e s is c alle d C o n s t an t tim e
co m ple x it y .

E x am p le : in t s u m (in t a , in t b )
{
re t u rn a+ b ;
}
2 .Lin e a r t im e c o m ple x it y: If t h e in p u t va lu e s ar e in c re a s e d t h e n
th e t im e co m ple x it y w ill c h an g e s .
▶ c om m e n t s = 0 s t e p
▶A s s ig n m e n t s ta t e m e n t = 1 s t e p
▶co n dit io n s t at e m e n t = 1 s t e p
▶ lo o p c on d it ion fo r n t im e s = n + 1 s t e p s
▶ b o dy o f t h e lo o p = n s t e p s
E x a m ple : in t s u m (in t A [ ] ,in t n )
{
in t s u m = 0 ,i;
fo r (i= 0 ;i<n ;i+ + )
s u m = s u m + A [ i] ;
re tu r n s u m ;
co s t re pe t at io n t o ta l
1 1 1
1+1 +1 1 + (n + 1 )+ n 2n+2
2 n 2n
1 1 1
4n+4
TIME COMPLEXITY
Th e t im e T(p) ta k e n b y a p ro g ra m P is t h e s u m of t h e co m p ile t im e
an d t h e r u n tim e (e x e c u t io n t im e )

Statement S/e Frequency Total

1 . A lg o rit h m S u m (a,n ) 0 - 0
2.
{ 0 - 0
3. S = 0 .0 ; 1 1 1
4. for i= 1 to n do 1 n+1 n+1
5. s = s + a [ I] ; 1 n n
6. re t u rn s ; 1 1 1
7.} 0 - 0

2n+3
Types of Time Complexity Analysis
W e h ave t h re e t y pe s of a n alys is re la te d t o t im e co m p le x ity ,
w h ic h a re :
1. Worst-case time complexity: F o r 'n ' in p u t s iz e , t h e w o rs t -c as e
tim e c om p le x it y ca n b e de fin e d a s t h e m ax im u m am o u n t of
tim e n e e d e d b y an alg o rit h m to co m p le te it s e x e cu t io n . Th u s ,
it is n o t h in g b u t a fu n ct io n d e fin e d b y t h e m ax im u m n u m b e r
of s t e p s pe rfo rm e d on an in s t an c e h a vin g a n in p u t s iz e o f n .
C o m pu t e r S cie n t is t s ar e m or e in te r e s t e d in t h is .

2. Average case time complexity: F o r 'n ' in pu t s iz e , t h e ave r ag e -


cas e t im e co m ple x it y can b e de f in e d a s t h e av e rag e am o u n t
of tim e n e e de d b y an alg o rit h m t o co m ple t e it s e x e cu t io n .
Th u s , it is n o th in g b u t a f u n ct ion d e fin e d b y t h e ave rag e
n u m b e r o f s te ps pe rfo rm e d o n an in s t an c e h avin g an in p u t
s iz e of n .
▶ Th e c o m ple x it y o f t h e as y m pt o t ic co m p u t at ion O (f)
d e t e rm in e s in w h ic h o rd e r t h e re s o u rce s s u ch a s C P U t im e ,
m e m o ry , e tc . ar e co n s u m e d b y th e a lg o r it h m t h at is
ar t ic u lat e d a s a fu n c tio n of t h e s iz e o f th e in p u t d at a.

▶ Th e co m ple x it y ca n b e fo u n d in an y fo rm s u ch as co n s t an t ,
lo g arit h m ic, lin e ar, n * log (n ) , q u ad ra t ic , c u b ic, e x po n e n t ial,
e t c. It is n o t h in g b u t t h e or d e r o f c o n s t an t , lo g ar ith m ic , lin e ar
an d s o on , t h e n u m b e r o f s t e p s e n co u n t e re d fo r t h e co m ple t io n
o f a pa rt icu lar a lg o r it h m . To m a k e it e ve n m or e p re c is e , w e
o ft e n c all t h e c o m ple x it y o f a n alg o rit h m a s "r u n n in g t im e " .
Typical Complexities of an Algorithm
W e t ak e a lo ok a t t h e diffe re n t ty pe s of c om p le x it ie s of a n
alg o rit h m an d on e o r m o re o f ou r a lg or it h m o r p ro g ra m w ill fall
in t o a n y of t h e fo llo w in g cat e g o rie s ;
▶ Constant Complexity
▶ Im p os e s a co m p le x ity o f O (1 ) . It u n d e r g oe s an e x e cu t io n o f a
co n s ta n t n u m b e r o f s t e p s lik e 1 , 5 , 1 0 , e tc . fo r s olvin g a
g ive n pr o b le m . Th e c ou n t o f o p e ra tio n s is in d e pe n de n t o f t h e
in p u t d at a s iz e .
▶ Logarithmic Complexity
▶ Im p os e s a co m p le x ity o f O (log (N ) ). It u n d e rg o e s t h e
e x e c u tio n of t h e o rd e r o f lo g (N ) s te p s . To pe r fo rm
o pe r at io n s on N e le m e n t s , it o ft e n t ak e s t h e log a rit h m ic
b as e as 2 .
▶ F or N = 1 ,0 0 0 ,0 0 0 , a n a lg or it h m t h at h as a co m ple x it y o f
O (lo g (N )) w o u ld u n de rg o 2 0 s t e p s (w it h a c on s t an t
p re c is ion ) . H e r e , t h e log a rit h m ic b as e d oe s n o t h o ld a
n e c e s s a ry co n s e q u e n ce fo r t h e o pe rat io n co u n t o rd e r , s o it
▶ Linear Complexity

▶ Im p os e s a co m p le x it y of O (N ) . It e n co m pa s s e s t h e s a m e
n u m b e r o f s t e p s as t h a t o f t h e t o t al n u m b e r o f e le m e n ts
t o im p le m e n t a n o p e ra tio n o n N e le m e n t s .

▶ F or e x a m ple , if t h e r e e x is t 5 0 0 e le m e n t s , th e n it w ill
t ak e ab o u t 5 0 0 s t e ps . B as ic ally, in lin e a r c om p le x it y, t h e
n u m b e r of e le m e n t s lin e a rly d e p e n d s o n t h e n u m b e r o f
s t e p s . F o r e x a m ple , t h e n u m b e r o f s t e p s for N e le m e n ts
ca n b e N / 2 o r 3 * N .
▶ Quadratic Complexity
▶ It im p o s e s a c o m ple x it y o f O (n 2 ).
▶ F or N in p u t d at a s iz e , it u n de r g oe s th e o rd e r o f N 2 c o u n t o f
o pe r at io n s on N n u m b e r of e le m e n t s for s o lvin g a g ive n
p ro b le m . If N = 1 0 0 , it w ill e n d u re 1 0 ,0 0 0 s t e p s .
▶ In ot h e r w o rd s , w h e n e ve r t h e o rd e r o f op e ra t io n t e n ds t o
h ave a q u ad ra t ic re lat io n w it h t h e in pu t da t a s iz e , it
re s u lt s in q u a dr at ic co m p le x ity . F o r e x am p le , fo r N n u m b e r
o f e le m e n t s , t h e s te ps ar e fou n d to b e in th e o rd e r o f
3*N2 /2.
▶ Cubic Complexity
▶ It im p o s e s a co m p le x ity o f O (n 3 ). Fo r N in pu t da t a s iz e , it
e x e c u te s t h e o rd e r o f N 3 s t e p s o n N e le m e n t s t o s o lve a
g ive n p ro b le m . F o r e x a m p le , if th e re e x is t 1 0 0 e le m e n t s , it
is g o in g t o e x e cu t e 1 ,0 0 0 ,0 0 0 s te p s .

▶ Exponential Complexity
▶ It im p o s e s a co m p le x ity o f O (2 n ) , O (N !) , O (n k ) , … . F o r N
e le m e n t s , it w ill e x e c u t e t h e o rd e r o f co u n t o f o pe r at ion s
t h at is e x po n e n t ially d e pe n da b le o n th e in p u t d at a s iz e . F or
e x am p le , if N = 1 0 , t h e n t h e e x p on e n tia l fu n ct io n 2 N w ill
re s u lt in 1 0 2 4 .
KINDS OF ANALYSIS

1.Worst-case: (u s u ally)
• T(n ) = m a x im u m t im e o f a lg or ith m o n an y in p u t o f
s iz e n .
2.Average-case: (s o m e t im e s )
• T(n ) = e x p e c te d t im e of a lg or ith m o ve r a ll in p u t s o f
s iz e n .
• N e e d as s u m pt io n o f s t a tis tic al dis tr ib u tio n o f in p u t s .
3.Best-case:
• T(n ) = m in im u m t im e o f alg or it h m on a n y in pu t o f
s iz e n .
COMPLEXITY:
C om p le x it y re fe r s t o t h e ra te a t w h ich t h e s t o rag e t im e g ro w s as
a fu n c t io n o f t h e p ro b le m s iz e
Analysis of an Algorithm

▶Th e g o al o f a n alys is o f a n alg o rit h m is t o co m p are alg o rit h m


in ru n n in g t im e a n d als o M e m o ry m a n ag e m e n t .
▶R u n n in g t im e o f a n alg o rit h m d e p e n ds on h o w lo n g it t ak e s a
co m p u t e r t o r u n t h e lin e s o f c o de o f th e a lg or ith m .
R u n n in g t im e o f an alg o rit h m d e p e n d s on
1 .S p e e d o f co m p u te r
2 .P ro g r am m in g la n g u ag e
3 .C om p ile r an d t ran s lat o r
E x a m ple s : b in ar y s e a rch , lin e ar s e ar ch
ASYMPTOTIC ANALYSIS:
▶ E x pr e s s in g t h e co m p le x it y in t e rm o f it s r e lat ion s h ip t o k n o w
fu n c tio n . T h is t yp e a n alys is is calle d as y m pt o t ic an a ly s is .
▶Th e m a in id e a o f A s ym p t ot ic a n alys is is t o h a ve a m e as u re o f
e ffic ie n cy o f an alg o rit h m , t h at do e s n ’ t de pe n d on
1 .M ac h in e c o n s t an t s .
2 .D o e s n ’ t re q u ir e a lg or ith m t o b e im p le m e n t e d.
3 .T im e t ak e n b y p ro g r am to b e pr e p are .
ASYMPTOTIC NOTATION
ASYMPTOTIC NOTATION: T h e m a t h e m at ical w ay o f r e pr e s e n t in g t h e
Tim e co m ple x it y.
Th e n o t at io n w e u s e t o de s cr ib e t h e as ym p to t ic r u n n in g t im e o f an
alg or it h m ar e d e fin e d in te r m s of fu n ct io n s w h o s e d om a in s a re
th e s e t o f n at u ra l n u m b e r s .

D e f in it io n : It is t h e w a y to de s cr ib e t h e b e h avio r o f fu n ct ion s in
th e lim it o r w it h ou t b o u n ds .
A s y m pt o t ic g ro w t h : Th e r at e at w h ic h th e fu n c tio n g ro w s …
“ g ro w th r at e ” is t h e co m ple x it y o f t h e fu n ct io n or th e a m o u n t
of r e s o u rc e it t ak e s u p to co m p u te .
Growth rate Time +memory
Classification of growth

1 .G ro w in g w it h t h e s a m e rat e .
2 . G r o w in g w it h t h e s lo w e r r at e .
3 .G ro w in g w it h th e fa s t e r r at e .
Th e y are 3 a s y m p t o tic n ot at io n s ar e m os tly u s e d t o re p re s e n t
t im e c o m ple x it y o f alg o rit h m .

1 .B ig o h (O ) n ot a tio n
2 .B ig o m e g a (Ω ) n o t at ion
3 .T h e ta (Θ ) n ot at io n
4 .L it tle o h n ot at io n
5 .L it tle o m e g a(Ω ) n o t at io n
1.Big oh (O)notation

1 .B ig o h (O )n o t at io n : A s ym p t o tic “ le s s t h an ” (s lo w e r
rat e ) .T h is n o t at io n m a in ly r e pr e s e n t u pp e r b o u n d o f a lg or ith m
ru n t im e .
B ig o h (O )n o t at io n is u s e fu l t o calc u lat e m ax im u m a m o u n t of
tim e o f e x e cu t io n .
B y u s in g B ig -o h n o ta t io n w e h ave t o c alc u la t e w o r s t cas e tim e
co m ple x it y.
Fo rm u la : f(n )<= c g (n ) n > = n 0 , c >0 ,n 0 > = 1

D e f in it io n : L e t f (n ) ,g (n ) b e t w o n o n n e g at ive (po s it ive ) fu n c tio n


n o w th e f(n )= O (g (n )) if t h e r e e x is t tw o p o s it iv e co n s t a n t c,n 0
s u ch t h at
f(n )< = c .g (n ) fo r a ll va lu e o f n >0 & c> 0
1.BigO-notation

 For a given function , we denote by the set of


functions

 We use -notation to give an asymptotic upper bound of a


function, to within a constant factor.
 means that there existes some constant s.t. is
always for large enough .
Examples
E x am p le : f(n )= 2 n + 3 & g (n ) = n
Fo rm u la : f(n )<= c g (n ) n > = n 0 , c >0 ,n 0 > = 1
f(n ) = 2 n + 3 & g (n )= n
N ow 3 n + 2 <= c.n
3 n + 2 <= 4 .n
P u t t h e valu e of n = 1
5 <= 4 fals e
N=2 8 <= 8 t r u e n ow n 0 > 2 F or all valu e of n > 2 & c= 4
n o w f(n )< = c.g (n )
3 n + 2 <= 4 n fo r a ll va lu e o f n >2
A b o ve c on d it ion is s a tis fie d th is n o ta t io n t a k e s m a x im u m
am o u n t o f t im e to e x e c u t e . S o t h at it is ca lle d w o rs t c as e
co m ple x it y.
2.Ω-Omega notation
Ω-Omega notation : A s y m p t o tic “ g re at e r t h a n ” (f as t e r r at e ). It
re pr e s e n t Lo w e r b o u n d o f alg o rit h m ru n t im e . B y u s in g B ig
O m e g a n o t at ion w e can ca lcu lat e m in im u m am o u n t o f t im e .
W e ca n s ay t h a t it is b e s t cas e tim e co m p le x ity .
F or m u la : f(n ) >= c g (n ) n > = n 0 , c> 0 ,n 0 >= 1
w h e re c is c o n s t an t , n is fu n c tio n
Lo w e r b o u n d
B e s t ca s e
Ω-Omeganotation

 For a given function , we denote by the set of


functions

 We use -notation to give an asymptotic lower bound on a


function, to within a constant factor.
 means that there exists some constant s.t.
is always for large enough .
Ex a m p le s

E x a m ple : f (n ) = 3 n + 2
F or m u la : f(n ) >= c g (n ) n > = n 0 , c> 0 ,n 0 >= 1
f(n )= 3 n + 2
3 n + 2 >= 1 * n , c= 1 p u t t h e valu e o f n = 1
n=1 5 > = 1 t ru e n 0 >= 1 fo r a ll va lu e o f n
It m e an s t h a t f(n ) = Ω g (n ).
3. -Theta notation

Th e ta (Θ ) n o t at aio n : A s ym p t ot ic “ E q u alit y” (s am e r at e ).
It re p r e s e n t ave r ag e b on d o f a lg or it h m r u n n in g t im e .
B y u s in g t h e ta n o ta tio n w e ca n ca lc u lat e ave r ag e a m ou n t o f
t im e . S o it c alle d a ve ra g e cas e tim e c om p le x ity of a lg o r it h m .
For m u la : c 1 g (n ) <= f(n ) <= c 2 g (n )

w h e re c is co n s t an t , n is fu n ct io n
A ve r ag e b o u n d
-Thetanotation

 For a given function , we denote by the set of


functions

 A function belongs to the set if there exist


positive constants and such that it can be “ sand-
wiched” between and or sufficienly large
 means that there exists some constant and s.
t. for large enough .
Ex a m p les

E x a m ple : f(n )= 3 n + 2
F or m u la : c 1 g (n ) <= f(n ) <= c 2 g (n )
f(n )= 2 n + 3
1 * n <= 3 n + 2 < = 4 * n n ow p u t t h e valu e o f n = 1
w e g e t 1 < = 5 < = 4 fals e
n = 2 w e g e t 2 < = 8 < = 8 t ru e
n = 3 w e g e t 3 <= 1 1 <= 1 2 t ru e
N o w all va lu e of n > = 2 it is t r u e a b ov e c o n dit ion is s at is fie d .
4 .L ittle oh n o tatio n

• L itt le o n ot a tio n is u s e d to de s cr ib e an u pp e r b o u n d t h at
ca n n o t b e t ig h t. In ot h e r w o rd s , loo s e u p p e r b o u n d o f f(n ) .
S lo w e r g ro w t h r at e
f(n ) g r ow s s lo w e r t h an g (n )
• L e t f(n ) a n d g (n ) ar e t h e fu n ct io n s t h at m a p p os itive re al
n u m b e rs . W e c an s a y t h at t h e fu n c tio n f(n ) is o (g (n ) ) if for
an y re al p os it ive c o n s t an t c, th e r e e x is t s an in t e g e r c on s t an t
n 0 ≤ 1 s u c h th a t f(n ) > 0 .
U s in g m a th e m a t ic al r e la t ion , w e ca n s ay t h a t f(n ) = o(g (n ))
m e an s ,
if

Example on little o asymptotic notation:

1 .If f(n ) = n 2 an d g (n ) = n 3 t h e n c h e ck w h e t h e r
f(n ) = o (g (n ) ) o r n o t.
S o l:

Th e re s u lt is 0 , a n d it s at is fie s t h e e q u at io n m e n t io n e d
ab o ve . S o w e can s a y t h at f(n ) = o(g (n )) .
5 .L it t le o m e ga (ω ) n o t a t io n

• A n ot h e r a s ym p t o tic n o t at io n is lit t le om e g a n o t at io n . it is
d e n ot e d b y (ω ).
• L itt le o m e g a (ω ) n o t at ion is u s e d t o d e s c rib e a lo os e lo w e r
b o u n d o f f(n ).
• F as t e r g r o w t h ra t e
• F (n ) g ro w s fas te r th a n g (n )

• If

F or m ally s t a te d as f(n ) = ω g (n )
D ata
S t ru ctu re s
G S Lehal

1
D at a S t ru ctu res
A data structure is a scheme
for organizing data in the
memory of a computer.

Bi nar y Tr e e

Que ue
2
D at a S t ru ctu res
The way in which the data is organized affects the
performance of a program for different tasks.
The choice of data structure and algorithm can
make the difference between a program running in
a few seconds or many days.

Computer programmers decide which data


structures to use based on the nature of the data
and the processes that need to be performed on
that data.

3
Selecting a Data Structure
Select a data structure as follows:
1. Analyze the problem to determine the resource constraints
a solution must meet.
2. Determine the basic operations that must be supported.
Quantify the resource constraints for each operation.
3. Select the data structure that best meets these
requirements.

4
Data Structure Philosophy
Each data structure has costs and benefits.
Rarely is one data structure better than another in all
situations.
A data structure requires:
■ space for each data item it stores,
■ time to perform each basic operation,
■ programming effort.

5
Data Structure Philosophy (cont)
Each problem has constraints on available space and time.
Only after a careful analysis of problem characteristics can we
know the best data structure for the task.
Bank example:
■ Start account: a few minutes
■ Transactions: a few seconds
■ Close account: overnight

6
C o m m o n o p e ra tio n s D ata
S tru ctu re s
■ Add
■ Change
■ Delete
■ Traverse
■ Search

7
D at a S t ru ctu re s

■ Example:library
■ is composed of elements (books)
■ Accessing a particular book requires
knowledge of the arrangement of the
books
■ Users access books only through the
librarian
C o m m o n D ata S tru ct u re s

■ Array
■ Stack
■ Queue
■ Linked List
■ Tree
■ Heap
■ Hash Table
■ Priority Queue

9
A rrays

■ Groups of nearly identical variables are very common and


very tedious to program.
■ Example: make one variable to hold the total marks of each student
in the class. We would have to declare 22 integer variables that all
represent something similar (total marks) and all have similar
names (e.g. marks1, marks2, ..., marks22):

i nt ma r ks 1, mar ks 2, mar k s 3, . . .

■ Arrays allow us to "group variables together" under a


common name.

i nt mar ks [22]; / * ho l ds 22 i n t e ge r s * /
10
A rrays

■ Variable = a named memory location for just one


stored value
■ Array = named memory location for many values,
stored sequentially.

■ Think of an array as a sequence of boxes, each one


holding a value:

v al ue 0 v a l ue 1 v al ue 2 v al ue 3 v al u e 4 . . .

11
A rrays

■ Each box holds one value


■ All boxes hold values of the same type
■ The entire set of boxes is given one name
■ We can select any box by using the common name and the
index of the box
■ The indices start at zero.

mar ks
223 256 312 299 329 ...

0 1 2 3 4
12
Th e A rray d at a s tru ctu re
■ An array is an indexed sequence of
components stored in contiguous memory
locations.
■ The length of the array is determined when the array is created, and
cannot be changed
■ Each component of the array has a fixed, unique index
■ Indices range from a lower bound to an upper bound
■ Any component of the array can be
inspected or updated by using its index
■ This is an efficient operation: O(1) = constant time

13
A rray variatio n s
■ The array indices may be integers (C, Java) or other
discrete data types (Pascal, Ada)

■ The lower bound may be zero (C, Java), one (Fortran), or


chosen by the programmer (Pascal, Ada)

■ In most languages, arrays are homogeneous (all


components must have the same type); in some (Lisp,
Prolog) the components may be heterogeneous (of varying
types)

14

14
A rrays in C / C ++

■ Array indices are integers


■ An array of length n has bounds 0 and n-1
■ Arrays are homogeneous

15

15
A rrays

■ To create an array, we must first declare it (just as we need


to declare variables).
■ In the declaration we need to specify the name of the array,
the type of the elements and the number of elements:

i nt ma r k s [5] ;

■ After the declaration:

mar ks ( Al l bo x
c o n t e n t s ar e
i ni t i al l y
u nde f i ne d)
16
A rrays

■ Array declaration syntax:

e l e me n t _ t ype a r r a y_ n a me [s i ze ];

■ The size must be a constant expression.


■ NO variables allowed
■ You cannot change the size at runtime.

■ The size is often set by a #define directive.

17
A rrays

■ Array indexing:
■ Each element of the array is like an ordinary variable.
■ In order to access that element, you must specify the name of the
array and the index of the element:

a r r a y _ na me [i n de x]
■ Example:

ma r k s [0] = 312; T hi s as s i gns t he


v al ue 312 t o t he
f i r s t e l e me nt o f
t he ar r a y mar ks .
18
A rrays

■ Array indexing:

■ Element indices start at 0: ar r ay[0] is the first element


■ The index of last element is n-1,where n is the size of the array. A
common mistake is to access a[20], when the array contains only
20 elements.
■ CAREFUL! The compiler will not catch a wrong index.
■ For example, it will allow you to write/read in gr ade s[- 10]
or gr ade s [4325].
■ This may corrupt your program or your data.

19
A rrays

■ After you declare an array, you must assign values


(initialize it) before you can use it in any other way.
There are several ways to do this.
1. Initialize the array during declaration:

i nt mar k s [5] = { 219, 320, 199, 256, 311} ;

■ After the initialization:


mar k s
219 320 199 256 311

20
A rray in itializatio n

■ You can initialize some of the elements with non zero values, while
the rest will automatically be initialized to zero.
i nt mar ks [5] = { 219, 320} ;
Equivalent to:
mar ks [0] = 219; mar ks [1]= 320; mar ks [2] = mar ks [3]
= mar ks [4] = 0;
• You can let the compiler figure the size for you:
i nt mar ks [] = { 219, 320, 199, 256, 311} ;
This will set the size of array mar ks equal to the number of entries in
the RHS bracket

21
A rray in itializatio n

2. Use assignment statements to initialize each element


separately:

i nt mar ks [5]; Index


S e le ct s box
mar ks [0] = 219; o#r,
‘ ele m ent
mar ks [1] = 320; ’
The index of the first
mar ks [2] = 199;
element is zero.
mar ks [3] = 256; The index of the last
element is size - 1
mar ks [4] = 311;

22
A rray in itializatio n

3. Use a for-loop to read the values from the user (or


assign some default value).

i nt mar ks [5];
f o r ( i = 0; i < 5; i + + ) {
pr i nt f ( " Type t o t al ma r ks : " ) ;
s c anf ( " %d" , &mar ks [i ]) ;
}

23
A rray in itializatio n

4. For arrays of integers only:


You can initialize all elements to zero using the
following syntax:

i nt s c o r e s [5] = { 0} ;

This is equivalent to:

i n t s c o r e s [5] = { 0, 0, 0, 0, 0} ;

It only works for 0, no other initial value!

24
A rray p it falls

■ You CANNOT assign a value to the entire array as if it


were one big variable:
i nt s c o r e s [5];

s c o r e s = 18; E R RO R!

■ You CANNOT change the size of an array:

i nt s i ze ;
i nt s c o r e s [s i ze ]; E R RO R!
pr i nt f ( " Type ar r ay s i ze : " ) ;
s c anf ( " %d" , &s i ze ) ;
25
A rray p it falls

■ You CANNOT rely on C to enforce valid index values

i nt s c o r e s [5];

s c o r e s [- 1] = 18; CA REFUL !

■ You CANNOT assign one array name to another:

i n t s c o r e s [30] , gr ade s [30];


s c o r e s = gr ade s E R RO R!
■ If you need to do something like this, use a for-loop to
assign the elements one at a time.
26
R e p re s e n t a tio n o f A rra y in a M e m o ry

■ T he pr oce s s to d e te rm in e t he a d d re s s in a m em ory :
a ) Fir st a d d re s s – ba s e a d d res s .
b ) R e la t ive a dd re s s t o ba s e a dd r es s t hro ugh ind e x f unct ion.

E xam p le: ch a r X [ 1 00 ] ;
L et u s es 1 loca t ion s tora ge .
If t he b as e a d d re s s is 1 2 0 0 t he n t he n ex t e lem e nt is in 1 20 1 .
Ind e x F un ct ion is w ritt e n as :
Loc (X[i]) = Loc(X[0]) + i , is s u bs crip t a nd LB = 0

12 0 0 12 0 1 1 2 02 12 0 3

X[0 X[1 X[2 28 2


8

] ] ]
R e p r e s e n ta tio n o f A rra y in a M e m o ry

■ In g e n e ra l, in d e x fu n ct ion :
L o c (X [ i] ) = L o c(X [ 0 ] ) + w *(i);

w h e r e w is le n g t h o f m e m o ry lo ca t ion r eq u ire d .
F o r re al n u m b e r: 4 b yt e , in t e g e r: 2 b yt e an d c h ara ct e r : 1
b yt e .

■ E x a m p le :
If L o c(X [ 0 ] ) = 1 2 0 0 , a n d w = 4 , fin d L oc (X[ 8 ] ) ?
L o c(X [ 8 ] ) = L o c(X [ 0 ] ) + 4 * (8 )
= 1232
28
In s e rtio n in to A rray

What happens if you want to insert an item at a specified


position in an existing array?
■ Write over the current contents at the given index (which might
not be appropriate), or
■ The item originally at the given index must be moved up one
position, and all the items after that index must

29
In s e rt in g a n e le m e n t in to an array
■ Suppose we want to insert the value 8 into this sorted array
(while keeping the array sorted)

1 3 3 7 12 14 17 19 22 30
■ We can do this by shifting all the elements after the mark
right by one location
■ Of course, we have to discard the 30 when we do this

1 3 3 7 8 12 14 17 19 22
• M o vin g a ll t h o s e elem en t s m a k es t h is a
o p er a t io n (lin ea r in t h e s iz e o f t h e a rr a y)

30
In s ert in g an e lem e n t in t o an

array
Suppose we want to insert the value 8 into this sorted array
(while keeping the array sorted)

1 3 3 7 12 14 17 19 22 30
■ We can do this by shifting all the elements after the mark
right by one location
■ Of course, we have to discard the 30 when we do this

1 3 3 7 8 12 14 17 19 22
• M o vin g a ll t h o s e elem en t s m a k es t h is a
o p er a t io n (lin ea r in t h e s iz e o f t h e a rr a y)

31
R e m o val f ro m A rrays

What happens if you want to remove an item from a


specified position in an existing array?
■ Leave in the array, i.e. indices that contain no elements,
which in practice, means that the array element has to be given a
special value to indicate that it is “empty”, or
■ All the items after the (removed item’s) index must be

32
D ele t in g an e le m e n t f ro m an array
■ Deleting an element is similar--again, we have to
move all the elements after it

1 3 3 7 8 12 14 17 19 22

1 3 3 7 12 14 17 19 22 ?

■ Deletion is a slow operation; we don’t want to do it very often


■ Deletion leaves a “vacant” location at the end
■ How do we mark it vacant?
■ Every bit pattern represents a valid integer
■ We must keep a count of how many valid elements are in the array

34 4
P a s s in g A rra ys a s P a ra m e te rs in
F u n ctio n s

• A rray s a re alw ay s p as s e d b y re f ere n ce.


• T h e “ [ ]” in th e f o rm al p ara m e te r
s p ecif ica tion in d icate s th at th e va ria ble is
a n a rra y.
• It is a go od p ractice to p a s s th e d im e n s ion o f
th e array as an oth e r p aram e te r, a s th e
co m p ile r d oe s n o t k n ow th e s iz e of th e array .

34
S m alle s t V alu e

■ Problem
■ Find the smallest value in an array of integers
■ Input
■ An array of integers and a value indicating the
number of integers
■ Output
■ Smallest value in the array
■ Note
■ Array remains unchanged after finding the
smallest value!
35
P a s s in g A n A rray
No t i c e e mpt y br ac ke t s

i nt L i s t Mi ni mum( i n t Ar [], i n t as i z e ) {
i nt Smal l e s t Val ue So Far = Ar [0];
f o r ( i nt i = 1; i < as i ze ; + + i ) { Co ul d we
i f ( Ar [i ] < Smal l e s t Val ue So Far ) {
jus t
Sma l l e s t Val ue So Far = Ar [i ]; as s i gn a
} 0 and
} h av e i t
r e t ur n Smal l e s t Val ue So Far ; wo r k?
}

36
R e ver s e fu n c tion

v o i d r e v e r s e ( i n t l i s t [], i nt n e wL i s t [], i n t s i ze )
{
f o r ( i n t i = 0, j = s i z e - 1; i < s i z e ; i + + , j - - )
{
ne wL i s t [j] = l i s t [i ];
}
}
i nt mai n( ) {
i nt l i s t 1[5] = { 1, 2 , 4, 5, 6} ;
i nt l i s t 2[5] ; r e v e r s e ( l i s t 1, l i s t 2 , 5) ;
r e t ur n 0;
}

1 2 3 4 5
lis
6
t
n e w Lis t 6 5 4 3 2 1

37
A f ew e xe rcis es on arrays

■ Write down the function


■ int del_array(int a[], int size, int pos) to delete the value stored
at position pos in the array a.
■ Write down the function
■ void insert(int a[], int size, int pos, int val) to insert val at
position pos in the array a.
■ Write down the function
■ void reverse(int a[], int size) to reverse the contents of array a.

38
2 D A rrays

■ Arrays are not limited to type int; we can have arrays of...
any other type including float, char , double and even
arrays !!!
■ An array of arrays is called a two-dimensional array and
can be regarded as a table with a number of rows and
columns:

39
2 D A rrays

■ Each row corresponds to marks of a student. Each


column corresponds to marks in a subject.

50 50 50 60
i s a 5 × 4 ar r ay : 80 80 88 72
5 r o ws , 4 c o l umns 49 50 40 60
60 60 60 76
75 80 75 81

40
D e clarin g 2 D A rrays

50 50 50 60
80 80 88 72
49 50 40 60
60 60 60 76
75 80 75 81
■ This is
an array of size 5 marks[5
whose elements are arrays of size 4 ]
whose elements are integer [4]
■ Declare it like this: in
i nt ma r k s [5] [4];
t

t ype o f e l e me nt i n name o f ar r ay numbe r numbe r


e ac h s l o t o f r o ws o f c o l umns
41
In it ializin g 2 D arrays

■ An array may be initialized at the time of declaration:

i nt mar ks [5][4] = {
{ 50, 50, 50, 60} ,
{ 80, 80, 88, 72} ,
{ 49, 50, 40, 60} ,
{ 60, 60, 60, 76} ,
{ 75, 80, 75, 81}
};

42
In it ializin g 2 D arrays

■ An array may be initialized by using a loop

#de f i ne ROWS 5
#de f i ne COL S 4

i nt mai n ( ) {
i nt i , j;
i nt mar ks [5][4];

/* i ni t i al i ze al l e l e me nt s t o ze r o e s : * /
f o r ( i = 0; i < ROWS; i + + )
f o r ( j= 0; j< COL S; j+ + ) mar ks [i ][j]
= 0;
}

43
In it ializin g 2 D arrays

■ An array may be initialized by the user

#de f i ne ROWS 5
#de f i ne COL S 4

i nt mai n ( ) {
i nt i , j;
i nt mar ks [5][4];
f o r ( i = 0; i < ROWS; i + + )
f o r ( j= 0; j< COL S; j+ + )
s c anf ( " %d" , &mar ks [i ][j]) ;
}

44
In it ializin g 2 D arrays

■ An integer array may be initialized to all zeros as


follows:
i nt nums [5][4] = { 0} ;

■ This only works for zero.

45
U s in g 2 D arrays

■ To access an element of a 2D array, you need to


specify both the row and the column:
nums [0][0] = 16;

pr i nt f ( " %d" , nums [1][2]) ;

46
Storing Two-Dimensional Array in memory

■ A t w o -d im en s io n al a r r a y c a n be s t o r ed in
t h e m em o ry in t w o w ay s :
■ R ow - m a jo r im ple m e nt at ion
■ C olum n-m a j or im p le m e nt at ion

47
Row-major Implementation
■ R o w -m a j o r im p le m en t a t io n is a lin ea r iz a tio n
t e ch n iq u e in w h ic h elem e n t s o f a r r a y a re s t o re d
r o w -w ise t h a t m ea n s t h e co m p let e fir s t ro w is
s t o r ed t h e n t h e c o m p let e s ec o n d ro w is s t o re d
and so on.
■ F o r ex a m p le an a r r a y [ 3 ] [ 3 ] is s t o re d in th e
m e m o r y a s s h o w be llo w :

A00 A0 1 A02 A10 A11 A12 A20 A21 A2 2

Ro w Ro w Ro w 4

1 2 3
9
Row-major Implementation

■ T h e s t o ra g e c an b e c le arly u n de r s t o od b y a rr an g in g
a rray a s m a tr ix as s h o w b e llow :

A00 A01 A02 Ro w 1

a= A10 A11 A12 Ro w 2

A20 A21 A22


Ro w 3

49
Row-major Implementation

■ Address of elements in row major implementation:


■ t h e c o m p u t e r d o e s n o t k eep t h e tr a c k o f a ll e le m en t s
o f t h e a rr a y , r a t h e r it ke ep s a b a s e a d d r es s a n d
c a lc u la te s t h e a d d re s s o f r eq u ir e d ele m en t w h en
n eed ed .
■ It ca lc u la t es b y t h e fo llo w in g r elat io n : a d d re s s o f
e le m en t a [ i][ j ]= B +W (n (i)+ (j ) )
■ W he re , B = B a se ad d re ss
■ W = siz e of e a ch e lem e nt ar ra y e le m en t
■ n =t he n um be r of colum ns
50
Row-major Implementation

■ A two-dimensional array defined as a[4][5] requires


4 bytes of storage space for each element. If the array
is stored in row-major form , then calculate the
address of element at location a[2][3] given base
address is 100.
■ Sol: B = 1 0 0 , i= 2 , j =3 , n =5 , W =4 (e le m en t s iz e)
■ A d d re s s o f a [ i] [ j ]= B + W (n * (i) +(j ))
■ A d d re s s o f a [ 2 ][ 3 ] = 1 0 0 +4 (5 * 2 + (3 ) )
= 1 0 0 + 4 (1 3 )
=100+52
= 15 2 51
Column-major Implementation

■ In c o lu m n m a j o r im p lem en t a tio n m em o r y
a llo ca t io n is d o n e c o lu m n b y c o lu m n t h a t
m e an s f ir s t t h e ele m en t s o f t h e c o m p let e fir s t
c o lu m n is s t o r ed t h en ele m en t s o f c o m p let e
s e co n d co lu m n is s t o r ed a n d s o o n .
■ F o r ex a m p le an a r r a y [ 3 ] [ 3 ] is s t o re d in th e
m e m o r y a s s h o w be llo w :

A00 A10 A2 0 A01 A11 A21 A 0 2 A 12 A22

Co l Co l Co l 5

1 2 3
3
Column-major Implementation

■ T h e s t o ra g e c an b e c le arly u n de r s t o od b y a rr an g in g
a rray a s m a tr ix as s h o w b e llow :

A00 A01 A02

a= A10 A11 A12

A20 A21 A22

Co l 1Co l 2Co l 3

53
Column-major Implementation

■ Address of elements in column major implementation:


■ t h e c o m p u t e r d o e s n o t k eep t h e tr a c k o f a ll e le m en t s
o f t h e a rr a y , r a t h e r it ke ep s a b a s e a d d r es s a n d
c a lc u la te s t h e a d d re s s o f r eq u ir e d ele m en t w h en
n eed ed .
■ It ca lc u la t es b y t h e fo llo w in g r elat io n : a d d re s s o f
e le m en t a [ i][ j ]= B +W (i+ m * (j ) )
■ W he re , B = B a se ad d re ss
■ W = siz e of e a ch e lem e nt ar ra y e le m en t
■ m = t he nu m be r of row s
54
A rrays an d
■ In C , t h e r e isPa oin
s t ro n t
g rers
e lat io n s h ip b e tw e e n po in t e rs an d
a rr ays . Th e n a m e of an ar ray is a po in t e r to t h e firs t
e le m e n t. H o w e ve r , t h e ar ra y n a m e is n o t a po int e r
. It is a t h at a lw a ys p oin t s t o th e
fir s t e le m e n t o f t h e a rra y a n d it s valu e c an n o t b e
c ha n ge d .
■ A ls o w h e n an a rr ay is d e c la re d , s p ace fo r all e le m e n t s in
t h e a rr ay is a llo c at e d, h o w e ve r w h e n a p o in te r var iab le is
d e cla re d on ly s p ac e s u fficie n t t o h o ld an ad dr e s s is
allo cat e d.
■ T h e d e cla ra t io n in t a [ 1 0 ] ; d e fin e s a n a rr a y o f 1 0 in t e ge r s
a nd r e s e rv e s s p a ce f or 1 0 in te g er s .
■ T h e d e cla ra tio n int *p ; d e f in e s p a s a “ p o in t e r t o a n in t ”
a nd
r e s e rv e s s p a ce f or 1 p oin t e r v a ria b le.
55
A rr a ys an d P o in te rs

i s a po i nt e r o nl y t o t he f i r s t
e l e me nt — no t t h e who l e ar r ay

56
A rr a y s a n d
P■ oIfin
a istters
h e n am e o f a n ar ra y, t h e e xp re s s io n
a is eq uiv a le nt to & a [ 0 ]. a [ 0 ] is eq uiv a len t to *a .
a [ i ] is e quiv a le nt t o *(a + i).

■ It fo llo w s t h e n t h at & a[ i ] an d (a + i) a re als o eq u ivale n t.


B o t h r e pr e s e n t t h e ad dr e s s o f t h e ith e le m e n t b e y on d a.
■ O n t h e o t h e r h an d , if p is a po in t e r, t h e n it m ay b e u s e d
w it h a s u b s crip t as if it w e re t h e n a m e o f an a rra y. p[
i ] is id e n t ic al t o * (p + i)

57
A rr a ys an d P o in te rs

T o ac c e s s an ar r ay, a ny po i nt e r t o
t he f i r s t e l e me nt c an be us e d
i ns t e a d o f t he name o f t he a r r ay.

58
A rray E xa m p le U s in g a P o in te r

in t x[ 4 ] = {1 2 , 2 0 , 3 9 , 4 3 },
*y
y =; & x[ 0] ; / / y p oin ts t o t he be ginn ing o f t he
p rin tf ( "% d \n ", x[ 0 ]); a rra y
p rin tf ( "% d \n ", *y ); / / out p ut s 1 2
p rin tf ( "% d \n ", *y + 1 ); / / als o ou tp u ts 1 2
p rin tf ( "% d \n ", / / out p ut s 1 3 ( 1 2 + 1 )
( *yint
pr )+1f();
"% d \n ", /
/// als
outop ut
oustpx[
u ts
1 ]1or
3 20
*(y +1 )); y+ = 2 ; / / y no w p oint s t o x[ 2 ]
pr int f( "% d \n ", *y ); / / pr int s out 39
*y = 38 ; pr int f( "% d \ n", / / cha ng es x [ 2 ] t o 38
* y- 1 ); / / pr int s out x[ 2 ] - 1 or 3 7
*y+ + ; p r int f( "% d \n ", / / se t s y to p o int a t t he n e xt a rra y e le m e nt
*y); (*y )+ +; // out p ut s x[ 3 ] ( 4 3)
pr int f( "% d \n ", *y ); / / s e ts w h at y p oin ts t o t o be 1 g rea te r
/ / ou tp u ts t he ne w v al ue of x[ 3 ]
( 4 4)
59
A rr a ys an d P o in te rs

What wi l l be t h e v al u e o f
e l e me n t s of ar r ay a ?
mai n( )
{
i nt * p, * p2, x = 50;
i nt a[5] = { 1, 2, 3, 4, 5} ; p : 4000 4028
4008
?
p= &x ; p2= a;
p2 : 4004 4012
?
* ( p2+ 1) = * p;
* p= * p2 + * ( p2+ 2) ; x : 4008 3
50
* p2 = 10; p= p2+ 4; a a [0] : 4012 1
p[- 2] = 60;
p2[3] = x; p2[4] = a [1] : 4016 50
2
p[- 1]; a [2] : 4020 60
3
a[3] : 4024 3
4

} a[4] : 4028 3
5
60
A rrays an d
P■ oin t ers
Consider the declaration:
■ int b[10], *bPtr=b;
■ Element b[n] same as
■ *(b+n)
■ *( bPtr + n )
■ bPtr[n]
■ n[b] (Why?)
■ b[n] = *(b+n) = *(n+b) = n[b]

61
A rrays a n d P o in te rs

i nt i; i n t * p;
i nt a r r a y [10]; i nt a r r a y [10];

f o r ( i = 0; i < 10; i + + ) f o r ( p = a r r ay ; p < &a r r a y [10] ; p+ + )


{ {
a r r a y[i ] = 0; * p = 0;
} }

These two blocks of code are functionally equivalent

62
A rra ys an d
P oin
W h a t w ill be t h e oter
utps
u t?

vo id m ain ()
{
in t b [ 1 0 ] , * b P t r = b , i;
fo r (i= 0 ; i< 1 0 ;
p
i+rin
+ )bt[f("% d \n",
i] = i; 5
* (bP t r+ 5 ) ); 5
p r in t f(" % d \ n " , bP t r[ 5 ] ); 5
p r in t f(" % d \ n " , 5 [ b P tr ] ); 5
} p r in t f(" % d \ n " , 5 [ b ] );

63
A rra ys an d
W ha t w ill be th ePv aoin
lue ofter
el em esnt s o f a rra y a ?
m a in()
{
in t *p , *p 2 , x= 5 0 , i=2 ; int a [ 5 ] ={1 ,2 ,
3 ,4,5 };
p = & x; p2 = a ;
*( p 2 +1 )=*p ;
* p= *p 2 + *(p 2 + 2 );
* p2 = 1 0 ; p= p 2 +4 ; p [ - 2 ] = 6 0;
p 2 [ 3 ] = *p2 * i;
p 2 [ 4 ] = p[ - 1 ] + * (p - 1 );
}

6
5
A rra ys an d
W ha t w ill be th ePv aoin
lue ofter
el em esnt s o f a rra y a ?
m a in()
{
in t *p , *p 2 , x= 5 0 , i=2 ; int a [ 5 ] ={1 ,2 ,
3 ,4,5 };
p = & x; p2 = a ;
*( p 2 +1 )=*p ;
* p= *p 2 + *(p 2 + 2 );
* p2 = 1 0 ; p= p 2 +4 ; p [ - 2 ] = 6 0;
p 2 [ 3 ] = *p2 * i;
p 2 [ 4 ] = p[ - 1 ] + * (p - 1 );
} 10, 50, 60, 20, 40

6
6
A rra ys an d
W h at w ill b e t hePv aoin
lue ofter s ts of a rr ay x ?
e le m en
m ain( )
{
int i, x [ 1 0 ], * p , *q; f or( i= 0 ; i< 1 0 ;
i++ )
x[ i]= i+ 1 ;
p = x+9 ;
q = p-8;
f or( i=3 ; i>0 ; i- - , p -- )
*p + = 1 0 ;
f or( i=1 ; i< 5 ; i+ + , q ++)
*q += i;
}

66
A rra ys an d
W h at w ill b e t hePv aoin
lue ofter s ts of a rr ay x ?
e le m en
m ain( )
{
int i, n = 8 , x[ 1 0 ], * p, * q, j= 1 0 ;
f or( i=0 ; i< 1 0 ; i++ )
x[ i]= i+ 1 ;
p = x+9 ;
q = p-8;
f or( i=3 ; i>0 ; i- - , p -- )
*p + = 1 0 ;
f or( i=1 ; i< 5 ; i+ + , q ++)
*q += i;
} 1, 3 , 5 , 7 , 9 , 6 , 7 , 1 8 , 19 , 2 0

67
P a s s in g Tw o-D im e n s io n a l A rr a ys to
F u n ctio n s

You ca n p as s a tw o - d im en s ion al a rray to a


f u n ctio n ; h o w e ve r, C + + re qu ires th a t th e co lu m n
s iz e t o b e s p ecifie d in th e f u n ctio n d e claratio n .
In th e n e xt e xa m p le , w e h a ve a fu n ction th at
sum s
u p tw o t wo - di me ns i o nal ar r ay in to a th ird on e .

68
P as s in g T w o -D im en s ion al A rra ys to
F u n ction s

// Sum up t wo 2- di me ns i o nal ar r ays i nt o a t hi r d o ne


#i nc l ude <i o s t r e am>
us i ng name s pac e s t d; c o ns t i nt max_ c o l s = 5;
// c [i ][j] = a[i ][j] + b[i ][j]

voi d add_ ar r ay( do ubl e a[][max_ c o l s ],


do ubl e b[][max_ c o l s ],
do ubl e c [][max_ c o l s ], i nt
r o ws )
{ i nt i , j;
f o r ( i = 0; i < r o ws ; i + + )
f o r ( j= 0; j < max_ c o l s ; j+ + )
c [i ][j] = a[i ][j] + b[i ][j];
}
69
S u m m ary o f A rrays

■ Good things:
■ Fast, random access of elements
■ Very memory efficient, very little memory is required other
than that needed to store the contents (but see bellow)
■ Bad things:
■ Slow deletion and insertion of elements
■ Size must be known when the array is created and is fixed
(static)

70

You might also like