27
2 Data Structures and Algorithm s: An Introduction CHAPTER OUTLINE 2.1 Overview 2.2 Concept of Data Structure 2.! Dei"n of a Suita#$e A$"orit%& 2.' A$"orit%& Ana$(i 2.1 OVERVIEW  A nor&a$ ) e*top co&puter i #ae) on Von Neumann architecture. It i a$o ca$$e) a stored program computer #ecaue t%e pro"ra&+ intruction an) it aociate) )ata are tore) in t%e a&e &e&or( a %own in ,i"ure 2.1. T%e pro"ra& intruction te$$ t%e (te& w%at operation i to #e perfor&e) an) on w%at )ata. T%e a#ove &o)e$ i #ae) on t%e fact t%at ever( practica$ pro#$e& t%at nee) to #e o$ve) &ut %ave o&e aociate) )ata. Let u coni)er a ituation w%ere a $it of nu&#er i to #e orte) in acen)in" or)er. T%i pro#$e& o#viou$( %a t%e fo$$owin" two part- 1. T%e $i t- a( / 0/ 1/ 1/ '/ 2 / / 13/ ! 2. A pro ce)ure to ort t %e "i ven $it of nu &#er. It &a( #e note) t%at elements of list  an) t%e procedure repreent t%e )ata an) pro"ra& of t%e pro#$e&/ repective$(. Now/ t%e efficienc( of t%e pro"ra& wou$) pri&ari$( )epen) upon t%e or"ani4ation of )ata. 5%en t%e )ata i poor$( or"ani4e)/ t%en t%e pro"ra& cannot efficient$( acce it. ,or intance/ t%e ti&e ta*en to open t%e $oc* of (our %oue )epen) upon w%ere (ou %ave *ept t%e *e( to it- in (our %an)/ in front poc*et/ in t%e tic*et poc*et of (our pant or t%e inner poc*et of (our #riefcae/ etc. T%ere &a( #e a variet( of &otive for e$ectin" one or t%e ot%er of t%ee p$ace to *eep t%e *e(. 6e( in t%e %an) or in t%e front poc*et wou$) #e 7uic*$( accei#$e a co&pare) to ot%er p$ace w%erea t%e *e( p$ace) in t%e inner poc*et of #riefcae i co&parative$( &uc% ecure). ,or i&i$ar reaon/ it i a$o necear( to

Chapter 2

Embed Size (px)

DESCRIPTION

chap2

Citation preview

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 1/27

2

Data Structures and Algorithms: An Introduction

CHAPTER OUTLINE

2.1 Overview 

2.2 Concept of Data Structure

2.! Dei"n of a Suita#$e A$"orit%&

2.' A$"orit%& Ana$(i

2.1 OVERVIEW

 A nor&a$ )e*top co&puter i #ae) on Von Neumann architecture. It ia$o ca$$e) a stored program computer #ecaue t%e pro"ra&+intruction an) it aociate) )ata are tore) in t%e a&e &e&or( a%own in ,i"ure 2.1.T%e pro"ra& intruction te$$ t%e (te& w%at operation i to #eperfor&e) an) on w%at )ata.

T%e a#ove &o)e$ i #ae) on t%e fact t%at ever( practica$ pro#$e& t%atnee) to #e o$ve) &ut %ave o&e aociate) )ata. Let u coni)er aituation w%ere a $it of nu&#er i to #e orte) in acen)in" or)er. T%ipro#$e& o#viou$( %a t%e fo$$owin" two part-

1. T%e $it- a( / 0/ 1/ 1/ '/ 2/ / 13/ !

2. A proce)ure to ort t%e "iven $it of nu&#er.

It &a( #e note) t%at elements of list  an) t%e procedure repreent t%e)ata an) pro"ra& of t%e pro#$e&/ repective$(.

Now/ t%e efficienc( of t%e pro"ra& wou$) pri&ari$( )epen) upon t%eor"ani4ation of )ata. 5%en t%e )ata i poor$( or"ani4e)/ t%en t%epro"ra& cannot efficient$( acce it. ,or intance/ t%e ti&e ta*en to opent%e $oc* of (our %oue )epen) upon w%ere (ou %ave *ept t%e *e( to it-in (our %an)/ in front poc*et/ in t%e tic*et poc*et of (our pant or t%einner poc*et of (our #riefcae/ etc. T%ere &a( #e a variet( of &otive fore$ectin" one or t%e ot%er of t%ee p$ace to *eep t%e *e(. 6e( in t%e %an)or in t%e front poc*et wou$) #e 7uic*$( accei#$e a co&pare) to ot%erp$ace w%erea t%e *e( p$ace) in t%e inner poc*et of #riefcae i

co&parative$( &uc% ecure). ,or i&i$ar reaon/ it i a$o necear( to

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 2/27

c%ooe a uita#$e repreentation or tructure for )ata o t%at a pro"ra&can efficient$( acce it.

Fig. 2.1 T%e tore) pro"ra& co&puter

2.2 CONCE! O" DA!A S!R#C!#RES

In fact t%e )ata/ #e$on"in" to a pro#$e&/ &ut #e or"ani4e) for t%efo$$owin" #aic reaon-

1. Suitable representation: Data %ou$) #e tore) in uc% a &anner t%at it

repreent t%e variou co&ponent of t%e pro#$e&.

,or e8a&p$e/ t%e $it of nu&#er can #e tore) in p%(ica$ continuou

$ocation. Arra( in 9C+ can #e ue) for t%e re7uire) repreentation a %own

in ,i"ure 2.2.

Fig. 2.2 List stored in physical continuous locations

2. Ease of retrieval: T%e )ata %ou$) #e tore) in uc% a &anner t%at t%e

pro"ra& can eai$( acce it.

 A an arra( i an index–value pair/ a )ata ite& tore) in t%e arra(

:ee ,i"ure 2.2; can #e ver( eai$( accee) t%rou"% it in)e8. ,or

e8a&p$e/ List [5] wi$$ provi)e t%e )ata ite& tore) at 3t% $ocation/ i.e. 92+.

,ro& t%i point of view/ t%e arra( i a random-access tructure.

!. Operations allowed- T%e operation nee)e) to #e perfor&e) on t%e )ata

&ut #e a$$owe) #( t%e repreentation.

,or e8a&p$e/ component by component  operation can #e )one on t%e )ata

ite& tore) in t%e arra(. Coni)er t%e )ata tore) in List of ,i"ure 2.2.

,or t%e purpoe of ortin"/ t%e &a$$et nu&#er &ut co&e to t%e %ea) of

t%e $it. T%i %a$$ re7uire t%e fo$$owin" two operation-

1. <iit ever( e$e&ent :co&ponent; in t%e $it to earc% t%e&a$$et an) it poition :91+ at $ocation !;.

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 3/27

2. E8c%an"e t%e &a$$et wit% t%e nu&#er tore) at t%e%ea) of t%e $it :e8c%an"e 9=1+ tore) at $ocation ! wit% 9+tore) at $ocation >;. T%i can #e eai$( )one #( uin" ate&porar( $ocation :a( Te&p; a %own #e$ow-

  Temp = List[0];

  List[0] = List[3];

  List[3] = Temp;

T%e new content of t%e Lit are a %own in ,i"ure 2.!.

Fig. 2.3 Contents of the List after exchange operation

T%e operation 1 an) 2/ "iven a#ove/ can #e repeate) on t%e unorte)part of t%e List to o#tain t%e re7uire) orte) List.,ro& a#ove )icuion/ it can #e conc$u)e) t%at an arra( provi)e auita#$e repreentation for t%e tora"e of a $it of nu&#er. Suc% arepreentation i a$o ca$$e) a data structure. Data structure i a$o"ica$ or"ani4ation of a et of )ata ite& t%at co$$ective$( )ecri#e ano#?ect. It can #e &anipu$ate) #( a pro"ra&. ,or e8a&p$e/ arra( i auita#$e )ata tructure for a $it of )ata ite&.It &a( #e o#erve) t%at if t%e arra( ca$$e) 9List+ repreent t%e data

structure t%en t%e operation carrie) out in tep 1 an) 2 )efinet%e algorithm nee)e) to o$ve t%e pro#$e&. T%e overa$$ perfor&ance of apro"ra& )epen) upon t%e fo$$owin" two factor-

1. C%oice of ri"%t )ata tructure for t%e "iven pro#$e&.

2. Dei"n of a uita#$e a$"orit%& to wor* upon t%e c%oen )ata tructure.

2.2.1 Choice o$ Right Data Structures

 An i&portant tep of pro#$e& o$vin" i to e$ect t%e )ata tructure

 w%erein t%e )ata wi$$ #e tore). T%e pro"ra& or a$"orit%& wou$) t%en wor* on t%i )ata tructure an) "ive t%e reu$t. A )ata tructure can eit%er #e e$ecte) fro& t%e built-in )ata tructureoffere) #( t%e $an"ua"e or #( )ei"nin" a new one. @ui$tin )atatructure in 9C+ are arra(/ tructure/ union/ fi$e/ pointer/ etc.Let u coni)er a ituation w%ere it i re7uire) to create a &erit $it of >tu)ent. T%ee tu)ent appeare) in a c$a e8a&ination. T%e )ata of atu)ent conit of %i name/ roll number/ marks/ percentage/an) grade. Out of &an( poi#i$itie/ t%e fo$$owin" two c%oice of )ata

tructure are wort% coni)erin"-

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 4/27

Choice I: T%e variou )ata ite& re$ate) to a tu)ent are- na&e/ ro$$nu&#er/ &ar*/ percenta"e/ an) "ra)e. T%e tota$ nu&#er of tu)ent i>. T%erefore/ t%e i&p$et c%oice wou$) #e to %ave a &an( para$$e$ $ita t%ere are )ata ite& to #e repreente). T%e arran"e&ent i

%own ,i"ure 2.'.

Fig. 2. Para$$e$ $it for tu)ent )ata

 An e7uiva$ent )ec$aration for )ata tructure of ,i"ure 2.' i "iven #e$ow-

char nameList

[60][15];

int roll[60];

int marks[60];

float percent[60];

char grade[60];

Now in t%e a#ove )ec$aration/ ever( $it %a to #e coni)ere) ain)ivi)ua$. T%e "eneration of &erit $it wou$) re7uire t%e $it to #eorte). T%e ortin" proce wou$) invo$ve a $ar"e nu&#er of e8c%an"eoperation. ,or eac% $it/ eparate e8c%an"e operation wou$) %ave to #e

 written. ,or e8a&p$e/ t%e e8c%an"e #etween )ata of i t% an) j t% tu)ent

 wou$) re7uire t%e fo$$owin" operation-

char tempName[15],chTemp;

int i,j, temp;

float fTemp;

strcpy (tempName, nameList[i]);

strcpy (nameList[i], nameList[j]);

strcpy (nameList[j], tempName);

temp = roll[i];

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 5/27

roll[i] = roll[j];

roll[j] = temp;

temp = marks[i];

marks[i] = marks[j];marks[j] = temp;

fTemp = percent[i];

percent[i] = percent[j];

percent[j] = fTemp;

chTemp = grade[i];

grade[i] = grade[j];

grade[j] = chTemp;

T%ou"% t%e a#ove co)e &a( wor*/ #ut it i not on$( a c$u&( wa( ofrepreentin" t%e )ata #ut wou$) re7uire a $ot of t(pin" effort a$o. In fact/a &an( a fifteen statements are re7uire) to e8c%an"e t%e )ata of i t%tu)ent wit% t%at of j t% tu)ent.Choice II: ,ro& t%e #ui$tin )ata tructure/ t%e struct can #ee$ecte) to repreent t%e )ata of a tu)ent a %own #e$ow-

struct student {

  char name[15];

  int roll;

  int marks;

  float percent;

  char grade;

  };

Si&i$ar$(/ fro& #ui$tin )ata tructure/ an array ca$$e) studList can #e e$ecte) to repreent a $it. However/ in t%i cae eac% $ocation %a to #e of t(pe tu)ent w%ic% ite$f i a tructure. T%e re7uire) )ec$aration i"iven #e$ow- struct student studList [60];

 It &a( #e note) t%at t%e a#ove "iven arra( of tructure i a uer)efine))ata tructure contructe) fro& two #ui$tin )atatructure- struct  an) array. Now in t%i cae/ e8c%an"e #etween )ataof i t% an) j t% tu)ent wou$) re7uire t%e fo$$owin" operation-

int i,j;struct student temp;

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 6/27

temp = studList[i];

studList [i] = studList [j];

studList [j] = temp;

T%u/ on$( three statements are re7uire) to e8c%an"e t%e )ata of i t%tu)ent wit% t%at of j t% tu)ent.

 A co&parion of t%e two c%oice )icue) a#ove eta#$i%e t%at C%oiceII i )efinite$( #etter t%anC%oice I a far a t%e e$ection of )atatructure for t%e "iven pro#$e& i concerne). T%e nu&#er of $ine ofco)e of C%oice II i )efinite$( $e t%an t%e C%oice I. oreover/ t%e co)eof C%oice II i co&parative$( ea( an) e$e"ant.,ro& t%e a#ove )icuion/ it can #e conc$u)e) t%at c%oice of ri"%t )atatructure i of para&ount i&portance fro& t%e point of view of

repreentin" t%e )ata #e$on"in" to a pro#$e&.

2.2.2 !%&es o$ Data Structures

 A $ar"e nu&#er of )ata tructure are avai$a#$e to pro"ra&&er t%at can #e uefu$$( e&p$o(e) in a pro"ra&. T%e )ata tructure can #e #roa)$(c$aifie) into two cate"orie- linear an) non-linear.

!1" #inear data structures: T%ee )ata tructure repreent ae7uence of ite&. T%e e$e&ent fo$$ow a $inear or)erin". ,or intance/

one)i&eniona$ arra( an) $in*e) $it are $inear )ata tructure. T%eecan #e ue) to &o)e$ o#?ect $i*e 7ueue/ tac*/ c%ain :$in*e) $it;/etc./ a %own in ,i"ure 2.3.

Fig. 2.$ Linear )ata tructure

 A $inear )ata tructure %a fo$$owin" two propertie-

1. Eac% e$e&ent i 9fo$$owe) #(+ at &ot one ot%er e$e&ent.

2. No two e$e&ent are 9fo$$owe) #(+ t%e a&e e$e&ent.

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 7/27

,or e8a&p$e/ in t%e $it of ,i"ure 2.!/ t%e 2n) e$e&ent :91+; i fo$$owe) #( !r)

e$e&ent :9+; an) o on.

!2" %on&linear data structures: T%ee )ata tructure repreento#?ect w%ic% are not in e7uence #ut are )itri#ute) in a p$ane. ,or

intance/ two)i&eniona$ arra(/ "rap%/ tree/ etc. are non$inear )atatructure. T%ee can #e ue) to &o)e$ o#?ect $i*e ta#$e/ networ* an)%ierarc%(/ a %own in,i"ure 2..

Fig. 2.' Non$inear )ata tructure

T%e tree tructure )oe not fo$$ow t%e firt propert( of $inear )atatructure an) t%e "rap% )oe not fo$$ow an( propert( of t%e $inear )atatructure. A )etai$e) )icuion on t%i iue i "iven $ater in t%e #oo*.

2.2.' (asic !erminolog% Related )ith Data Structures

T%e #aic ter& connecte) wit% t%e )ata tructure are-

1. Data ele(ent: It i t%e &ot fun)a&enta$ $eve$ of )ata. ,or e8a&p$e/

a Date i &a)e up of t%ree part- Da(/ ont%/ an) Bear a %own in ,i"ure

2.. T%e Date i ca$$e) a )ata o#?ect an) DD/ / an) BB are ca$$e) t%e )ata

e$e&ent or )ata ite&.

It &a( #e note) t%at t%e )ata o#?ect )enote a "roup an) t%e )ata e$e&ent

)enote ato&ic entitie.

2. )ri(itive data t*pes: It i t(pe of )ata t%at a varia#$e &a( %o$). ,or

e8a&p$e/ int / float / char/ etc. are )ata t(pe.

!. Data ob+ect: It i a et of e$e&ent. ,or e8a&p$e/ t%e et of fruit 9,+ referto t%e et , 9&an"o+/ 9oran"e+/ 9"rape+FG. Si&i$ar$(/ a et of countin"

nu&#er 9C+ refer to t%e et C 1/ 2/ !/ 'F.G.

Fig. 2.7 Data elements of data object Date

'. Data structure: It i a et of re$ate) e$e&ent.A et of operation t%at are

a$$owe) on t%e e$e&ent of t%e )ata tructure i )efine).

3. Constant: It i t%e &e&or( $ocation t%at )oe not c%an"e it content

)urin" t%e e8ecution of a pro"ra&.

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 8/27

.  ,ariable: It i t%e &ot fun)a&enta$ apect of an( co&puter $an"ua"e an)

can #e )efine) a a $ocation in t%e &e&or( w%erein a va$ue can #e

&anipu$ate)/ i.e./ tore)/ accee)/ an) &o)ifie).

2.' DESI*N O" A S#I!A(+E A+*ORI!,-

 After e$ectin" t%e )ata tructure/ an a$"orit%&/ #ae) on t%e )atatructure/ nee) to #e )ei"ne). T%e et of ru$e t%at )efine %ow aparticu$ar pro#$e& can #e o$ve) in a finite e7uence of tep i ca$$e)analgorithm. An a$"orit%& written in a co&puter $an"ua"e i ca$$e)a program. An algorith( can #e )efine) a a finite e7uence ofintruction/ eac% of w%ic% %a a c$ear &eanin" an) can #e e8ecute) wit%a finite a&ount of effort in finite ti&e.

 A co&puter i i&p$( a &ac%ine wit% no inte$$i"ence. In fact/ t%e

proceor of t%e co&puter i a paive )evice t%at can on$( e8ecuteintruction. T%erefore/ t%e co&puter &ut #e intructe) inuna&#i"uou tep uc% t%at it fo$$ow t%e& one after t%e ot%er toperfor& t%e "iven ta*. T%u/ it #eco&e pro"ra&&er+ )ut( to precie$()efine %i pro#$e& in t%e for& of an a$"orit%&/ i.e./ una&#i"uou tep

 written in i&p$e En"$i%. T%e )eira#$e features of an a$"orit%& are-

1. Eac% tep of t%e a$"orit%& %ou$) #e i&p$e.

2. It %ou$) #e una&#i"uou in t%e ene t%at t%e $o"ic %ou$)

 #e crisp an) clear.!. It %ou$) #e effective/ i.e./ it &ut $ea) to a uni7ue o$ution of t%e pro#$e&.

'. It &ut en) in a finite nu&#er of tep.

3. It %ou$) #e a efficient a poi#$e.

 Accor)in"$(/ an a$"orit%& %a t%e fo$$owin" c%aracteritic-

1. Input: T%i part of t%e a$"orit%& rea) t%e )ata of t%e "iven pro#$e&.

2. )rocess- T%i part of t%e a$"orit%& )oe t%e re7uire) co&putation in

i&p$e tep.

!. Finiteness- T%e a$"orit%& &ut co&e to an en) after a finite nu&#er oftep.

'. Effectiveness- Ever( tep of t%e a$"orit%& &ut #e accurate an) precie. It

%ou$) a$o #e e8ecuta#$e wit%in a )efinite perio) of ti&e on t%e tar"et

&ac%ine.

3. Output- It &ut pro)uce t%e )eire) output.

T%u/ we can a( t%at t%e a$"orit%& te$$ t%e co&puter w%en to rea))ata/ co&pute on it/ an) write t%e anwer.

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 9/27

2.'.1 ,o) to Deelo& an Algorithm/

In or)er to )eve$op an a$"orit%&/ t%e pro#$e& %a to #e proper$(ana$(e). T%e pro"ra&&er %a to un)ertan) t%e pro#$e& an) co&e out

 wit% a &et%o) or $o"ic t%at can o$ve t%e pro#$e&. T%e $o"ic &ut #e

i&p$e an) a)apta#$e on a co&puter. T%e fo$$owin" tep can #eu""ete) for )eve$opin" an a$"orit%&-

1. Un)ertan) t%e pro#$e&.

2. I)entif( t%e output of t%e pro#$e&.

!. I)entif( input re7uire) #( t%e pro#$e& an) c%ooe t%e aociate) )ata

tructure.

'. Dei"n a $o"ic t%at wi$$ pro)uce t%e )eire) output fro& t%e "iven input.

3. Tet t%e a$"orit%& for )ifferent et of input )ata.

. Repeat tep 1 to 3 ti$$ t%e a$"orit%& pro)uce t%e )eire) reu$t for a$$

t(pe of input an) ru$e.

 5%i$e o$vin" pro#$e& wit% t%e %e$p of co&puter/ t%e fo$$owin" fact%ou$) #e *ept in &in)-A $ocation in t%e &ain &e&or( %a t%e propert(- Writing-in i)etructive an) Reading-out  i non)etructive. In i&p$e wor)/ wecan a( t%at if a va$ue i ai"ne) to a &e&or( $ocation/ t%en it previoucontent wi$$ #e $ot. However/ if we ue t%e va$ue of a &e&or( $ocationt%en t%e content of t%e &e&or( )o not c%an"e/ i.e./ it re&ain intact.

,or e8a&p$e/ in t%e fo$$owin" tate&ent/ t%e va$ue of varia#$e @ an) Care #ein" ue) an) t%eir u& i tore) in varia#$e A 

 -    J C

 w%ere/ t%e (&#o$ 9+ i an ai"n&ent operator.If previou va$ue of A/ @/ C were 13/ '3/ 2>/ repective$( t%en aftere8ecution of t%e a#ove tate&ent/ t%e new content of A/ @/ C wou$) #e3/ '3/ an) 2>/ repective$(.Coni)er t%e fo$$owin" tate&ent-

Tota$ Tota$ J 1>>K

T%e a#nor&a$ $oo*in" tate&ent i ver( &uc% nor&a$ a far a co&puter&e&or( i concerne). T%e tate&ent revea$ t%e fo$$owin" fact-O$) va$ue of varia#$e 9Tota$+ i #ein" a))e) to an inte"er 1>> an) t%ereu$t i #ein" tore) a new va$ue in t%e varia#$e 9Tota$+/ i.e./ rep$acin"it o$) content wit% t%e new content.

 After t%e e8ecution of t%e tate&ent wit% o$) content of 9Tota$+ :a( 2';/t%e new content of 9Tota$+ wi$$ #e 12'.

 

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 10/27

2.3.1.1 Identifying Inputs and Outputs

It i ver( i&portant to i)entif( input an) output of an a$"orit%&. T%einput to an a$"orit%& &ean t%e )ata to #e provi)e) to t%e tar"etpro"ra& t%rou"% input )evice uc% a *e(#oar)/ &oue/ input fi$e/ etc.

T%e output of t%e a$"orit%& &ean t%e fina$ )ata to #e "enerate) for t%eoutput )evice uc% a &onitor/ printer/ output fi$e/ etc.T%e input an) output %ave to #e i)entifie) at t%e #e"innin" of t%epro"ra& )eve$op&ent. T%e &ot i&portant )eciion i to )ifferentiate

 #etween major an) minor input an) output. T%e &inor input areo&eti&e o&itte) at t%e a$"orit%& )eve$op&ent ta"e. T%e ot%eri&portant iue i to fin) at w%ic% part of t%e pro"ra& t%e input wou$)

 #e re7uire) an) t%e output "enerate).

E/a(ple 1: Let u reconi)er t%e pro#$e& of ortin" a "iven $it ofnu&#er. In t%i cae/ t%e re7uire)inputs are-

1. Lit of nu&#er

2. Si4e of $it

!. T(pe of nu&#er to #e tore) in t%e $it

'. In w%ic% or)er to #e orte)

T%e outputs are-

1. T%e orte) $it

2. ea"e for a*in" t%e i4e of t%e $it

!. ea"e for )ip$a(in" t%e output $it

T%e &ea"e are t%e &inor output. After i)entif(in" t%e input an) t%e output/ t%e a))itiona$ infor&ation&ut #e pecifie) #efore writin" t%e fina$ a$"orit%&.

Input specifications

1. In w%at or)er an) for&at/ t%e input va$ue wi$$ #e rea)

2. 5%at are t%e $ower an) upper $i&it of t%e input va$ue ,or e8a&p$e/ t%ei4e of t%e $it %ou$) not #e $e t%an 4ero an) it t(pe %a to #e neceari$(

an inte"er.

!. 5%en to *now t%at t%ere i no &ore input to #e rea)/ i.e./ w%at &ar* t%e

en) of t%e $it

Output specifications

1. In w%at or)er an) for&at t%e output va$ue wi$$ #e pro)uce)

2. 5%at t(pe of va$ue to #e pro)uce) wit% w%at pacin" i.e./ )eci&a$ pacin"/

i"nificant )i"it/ etc.

!. 5%at &ain %ea)in" an) co$u&n %ea)in" to #e printe) on t%e output pa"e.

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 11/27

2.'.2 Ste&)ise Re$inement

 5e *now t%at if t%ere i even a &a$$ $o"ica$ error in a pro"ra&/ t%eco&pi$er cannot )etect it. T%e (ear 2>>> pro#$e& :B26 pro#$e&; i a"$arin" e8a&p$e of t%i t(pe. T%u/ an a$"orit%& re7uire a ver( carefu$

)ei"n/ epecia$$( pertainin" to a $ar"e an) co&p$e8 pro#$e&.In or)er to )ei"n an a$"orit%&/ t%e pro"ra&&er &ut a)opt o&e&et%o)o$o"ica$ approac% uc% a &o)u$arMtepwie refine&entapproac% of tructure) pro"ra&&in" tec%ni7ue. Stepwie refine&entue t%e )ivi)e an) con7uer trate"(. In t%i tec%ni7ue/ t%e pro#$e& i)ivi)e) into a nu&#er of tep. Eac% tep i )ecri#e) #( a u#pro#$e&

 w%ic% i &a$$er an) i&p$er a co&pare) to t%e ori"ina$ pro"ra&. T%eu#pro#$e& i a"ain )ivi)e) into ti$$ &a$$er tep an) o on. T%erefine&ent continue ti$$ we reac% to a ta"e w%ere t%e tep #eco&e

fair$( i&p$e to i&p$e&ent.

E/a(ple 2: Suppoe t%e fo$$owin" table i to #e printe) on t%e creen.

T%e pro#$e& at %an) i How to print t%e table. T%e firt verion of t%ea$"orit%& i "iven #e$ow-

 A$"orit%& Print_Table Ver1.{

  Step 1. Print table

}

 5%en we $oo* at t%e table/ we fin) t%at t%e ta#$e i not%in" #ut aco$$ection of ros an) we %ave to print on$( 1> row. So/ t%e pro#$e& %a

 #een re)uce) to printin" a row 1> ti&e. T%e econ) refine) verion oft%e a$"orit%& i "iven #e$ow-

 A$"orit%& Print_Table Ver2.{

Step

  1. Row = 1

  2. Print the row

  3. Row = Row + 1

  4. If Row <= 10 repeat steps 2 and 3

  5. Stop

}

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 12/27

Now our focu i on %ow to print a ro. A c$oer $oo* at t%e pro#$e&u""et t%at a row i not%in" #ut a co$$ection of columns an) we %ave toprint on$( 1> co$u&n for eac% row. T%e t%ir) refine) verion of t%ea$"orit%& i "iven #e$ow-

 A$"orit%& Print_Table Ver3.

{

Step

  1. Row = 1

  2. Col = 1

  2.1 Print the col.

  2.2 Col = Col + 1

  2.3 If Col <= 10 repeat steps 2.1 and 2.2

  2.4 Go to a new line.

  3. Row = Row + 1

  4. If Row <=10 repeat steps 2 and 3

  5. Stop.

}

Now our concern i %ow to print a column. 5e fin) t%at it i ver(i&p$e/ i.e./ we "et t%e va$ue to #e printe) #( &u$tip$(in" t%e va$uecontaine) in t%e varia#$e Ro an) !ol  T%e fina$ verion of t%e refine)a$"orit%& i "iven #e$ow-

 A$"orit%& Print_Table Ver4.

{

Step

  1. Row = 1

  2. Col = 1

  2.1 ColVal = Row * Col

  2.2 Print the ColVal.

  2.3 Col = Col + 1

  2.4 If Col <= 10 repeat steps 2.1 to 2.3

  2.5 Go to a new line.

  3. Row = Row + 1

  4. If Row <=10 repeat steps 2 and 3

  5. Stop.

}

T%e pro"ra&&er top at t%i ta"e w%ere %e fin) t%at t%e tep:; itrivia$ an) can #e eai$( i&p$e&ente).

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 13/27

2.'.' #sing Control Structures

Ever( $an"ua"e upport e7uence contro$ tructure. T%ee tructurecan #e eai$( ue) towar) writin" a pro"ra& for a "iven pro#$e&.

!1" Se0uence structure: T%e or)er in w%ic% t%e tate&ent ortate&ent #$oc* are written wit%in a pair of cur$( #race :{,}; in ana$"orit%& are e7uentia$$( e8ecute) #( t%e co&puter.

E/a(ple : 5rite an a$"orit%& t%at co&pute t%e area of a trian"$e.

Solution: T%e o$ution i trivia$. T%e input are #ae an) %ei"%t. T%efo$$owin" for&u$a wi$$ #e ue) for t%e co&putation of t%e area oftrian"$e-

 Area #ae %ei"%t

 w%ere area i t%e output to #e )ip$a(e). T%e i&p$e e7uence tructurere7uire) for t%i pro#$e& i "iven #e$ow-

 A$"orit%& AreaTriangle ()

{

Step

  1. Read base, height;

  2. Area = 0.5 * base * height;

  3. Print Area;

  4. Stop

}

It &a( #e note) t%at in a e7uence tructure-

1. T%e tate&ent are e8ecute) e7uentia$$(.

2. T%e tate&ent are e8ecute) one at a ti&e.

!. Eac% tate&ent i e8ecute) on$( once.

'. No tate&ent i repeate).3. T%e e8ecution of t%e $at tate&ent &ar* t%e en) of t%e e7uentia$

tructure.

!2" Selection !conditional control" structure: T%e e$ection or)eciion tructure i ue) to )eci)e w%et%er a tate&ent or a #$oc* i to

 #e e8ecute) or not. T%e )eciion i &a)e #( tetin" w%et%er a con)itioni true or fa$e. T%e ife$e $a))er tructure can #e ue) for t%i purpoe.

E/a(ple : 5rite an a$"orit%& t%at print t%e #i""et of "iven t%ree

uni7ue nu&#er.

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 14/27

Solution:

Input- T%ree nu&#er- Num1, Num2, an) Num3

T(pe of nu&#er- inte"er.Output- Dip$a( t%e #i""et nu&#er an) a &ea"e in t%i re"ar).

 A$"orit%& Biggest ()

{

Step

1. Read Num1, Num2, Num3;

2. If (Num1 > Num2)

  2.1 If (Num1 > Num3)

  Print “Num1 is the largest”

  Else

  Print “Num3 is the largest”

  Else

  2.2 If (Num2 > Num3)

  Print “Num2 is the largest”

  Else

  Print “Num3 is the largest”

  3. Stop

}

!" Iteration: T%e iteration tructure i ue) to repeate)$( e8ecute atate&ent or a #$oc* of tate&ent. T%e $oop i repeate)$( e8ecute) unti$a certain pecifie) con)ition i atifie).

E/a(ple $: 5rite an a$"orit%& t%at fin) t%e $ar"et nu&#er fro& a"iven $it of nu&#er.

Solution:

Input: Lit of nu&#er/ i4e of $it/ pro&pt for input.

*pe of nu(bers: inte"er.Output: Dip$a( t%e $ar"et nu&#er an) a &ea"e in t%i re"ar).

T%e &ot basic solution to t%i pro#$e& i t%at if t%ere i on$( onenu&#er in t%e $it t%en t%e nu&#er i t%e $ar"et. T%erefore/ t%e firtnu&#er of t%e $it wou$) #e ta*en a $ar"et. T%e $ar"et wou$) t%en #eco&pare) wit% t%e ret of t%e nu&#er in t%e $it. If a nu&#er i foun)$ar"er t%an t%e current $ar"et/ t%en it i tore) in t%e $ar"et. T%iproce i repeate) ti$$ t%e en) of t%e $it. ,or iteration/ "or loop can #e

e&p$o(e).T%e concerne) a$"orit%& i "iven #e$ow-

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 15/27

 A$"orit%& Largest

{

Step

  1. Prompt “Enter the size of the list”;

  2. Read N;  3. Prompt “Enter a Number”;

  4. Read Num;

  5. Largest = Num; /* The first number is the

largest*/

  6. For (I = 2 to N)

  {

  6.1 Prompt “Enter a Number”

  6.2 Read Num

  6.3 If (Num > Largest) Largest = Num  }

  7. Prompt “The largest Number =”

  8. Print Largest;

  9. Stop

}

It &a( #e note) %ere t%at if a $oop ter&inate after a nu&#er ofiteration/ t%en t%e $oop i ca$$e) afinite $oop. On t%e ot%er %an) if a

$oop )oe not ter&inate at a$$ an) *eep on e8ecutin" en)$e$(/ t%en it ica$$e) an infinite $oop.

!" 3se of accu(ulators and counters:  #ccumulator i a varia#$et%at i "enera$$( ue) to co&pute u& or pro)uct of a erie. ,ore8a&p$e/ t%e u& of t%e fo$$owin" $it of nu&#er can #e co&pute) #(initia$i4in" a varia#$e :a( 9$otal% ; to 4ero.

T%e 1t nu&#er of t%e $it i a))e) to Tota$ an) t%en t%e 2n)/ !r) an) o

on ti$$ t%e en) of t%e $it. ,ina$$(/ t%e varia#$e $otal  wou$) "et t%eaccu&u$ate) u& of t%e $it "iven a#ove.

E/a(ple ': 5rite an a$"orit%& t%at rea) a $it of 3> inte"er va$ue an)print t%e tota$ of t%e $it.

Solution: Let u au&e t%at a varia#$e ca$$e) 9u&+ wou$) #e ue) a anaccu&u$ator. A varia#$e ca$$e) Nu& wou$) #e ue) to rea) a nu&#erfro& t%e input $it one at ti&e. ,or $oop wi$$ #e e&p$o(e) to )o t%eiteration.

T%e A$"orit%& i "iven #e$ow-

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 16/27

 A$"orit%& SumList

{

Step

  1. Sum =0;

  2. For (I = 1 to 50)  {

  2.1 Read Num;

  2.2 Sum = Sum + Num; /* The accumulation of the

list */

  }

  3. Prompt “The sum of the list =”;

  4. Print Sum;

  5. Stop

}

It &a( #e note) t%at for pro)uct of a erie/ t%e accu&u$ator i initia$i4e)to 1.!ounter i a varia#$e ue) to count nu&#er of operationMactionperfor&e) in a $oop. It &a( a$o #e ue) to count nu&#er of &e&#erpreent in a $it or a et of ite&.T%e counter varia#$e i "enera$$( initia$i4e) #( >. On t%e occurrence of aneventMoperation/ t%e varia#$e i incre&ente) #( 1. At t%e en) of t%e

countin" proce/ t%e varia#$e contain t%e re7uire) count.E/a(ple 4: 5rite an a$"orit%& t%at fin) t%e nu&#er of 4ero preentin a "iven $it of N nu&#er.

Solution: A counter varia#$e ca$$e) Nu&ofQero wou$) #e ue) tocount t%e nu&#er of 4ero preent in a "iven $it of nu&#er. A varia#$eca$$e) Nu& wou$) #e ue) to rea) a nu&#er fro& t%e input $it one atti&e. ,or $oop wi$$ #e e&p$o(e) to )o t%e iteration.T%e A$"orit%& i "iven #e$ow-

 A$"orit%& Count_zeros()

{

Step

  1. Num_of_zeros = 0;

  2. For (I = 1 to N)

  {

  2.1 Read Num;

  2.2 If (Num == 0)

  Num_of_zeros = Num_of_zeros +1;  }

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 17/27

  3. Prompt “The number of zero elements =”;

  4. Print Num_of_zeros;

  5. Stop

}

E/a(ple 5: 5rite a pro"ra& t%at co&pute t%e u& of firt N &u$tip$eof an inte"er ca$$e) 96+/ i.e.

  u&Serie 16 J 26 J !6 J FFN6 

Solution: An accu&u$ator ca$$e) sumSeries wou$) #e ue) toco&pute t%e re7uire) u& of t%e erie. A t%e nu&#er of tep are*nown :i.e./ N;/ 9,or $oop+ i t%e &ot appropriate for t%e re7uire)co&putation. T%e in)e8 of t%e $oop wi$$ act a t%e counter.

Input- N/ 6 Output- sumSeries /* This program computes the sum of a series */

#include <stdio.h>

void main()

{

int sumSeries, i;

int N; /* Number of terms */int K; /* The integer multiple*/

  printf(“\n Enter Number of terms and Integer

Multiple”);

  scanf (“%d %d”, &N,&K);

  sumSeries = 0; /*Accumulator initialized */

  For (i=1; i<=N; i++)

  {

  sumSeries = sumSeries + i*K;

  }printf (“\n The sum of first %d terms = %d”, N,

sumSeries);

}

In fact/ it can #e conc$u)e) t%at irrepective of t%e pro"ra&&in"para)i"&/ a "enera$ a$"orit%& i )ei"ne) #ae) on fo$$owin"contruct-

1. Se0uence structure- State&ent after tate&ent are written in e7uence

in a pro"ra&.

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 18/27

2. Selection- @ae) on a )eciion :i.e./ ife$e;/ a ection of tate&ent i

e8ecute).

!. Iteration- A "roup of tate&ent i e8ecute) wit%in a $oop.

'. Function call- At a certain point in pro"ra&/ t%e tate&ent &a( ca$$ a

function to perfor& a particu$ar ta*.

It i t%e reponi#i$it( of t%e pro"ra&&er to ue t%e ri"%t contruct atri"%t p$ace in t%e a$"orit%& o t%at an efficient pro"ra& i pro)uce). A)icuion on ana$(i of a$"orit%& i "iven in ne8t ection.

2.0 A+*ORI!,- ANA+SIS

 A critica$ $oo* at t%e a#ove )icuion in)icate t%at t%e )ata tructurean) a$"orit%& are re$ate) to eac% ot%er an) %ave to #e tu)ie) to"et%er.

,or a "iven pro#$e&/ an a$"orit%& i )eve$ope) to o$ve it #ut t%ea$"orit%& nee) to operate on )ata. Un$e t%e )ata i tore) in auita#$e )ata tructure/ t%e )ei"n of a$"orit%& re&ain inco&p$ete. So/

 #ot% t%e )ata tructure an) a$"orit%& are i&portant for pro"ra&)eve$op&ent.In fact/ an a$"orit%& re7uire fo$$owin" two resources-

1. 6e(or* space- Space occupie) #( pro"ra& co)e an) t%e aociate) )ata

tructure.

2. C)3 ti(e- Ti&e pent #( t%e a$"orit%& to o$ve t%e pro#$e&.

T%erefore/ an a$"orit%& can #e ana$(4e) on twoaccount- space an) time 5e nee) to )eve$op an a$"orit%& t%atefficient$( &a*e ue of t%ee reource. @ut t%en/ t%ere are &an( wa(to attac* a pro#$e& an) %ence &an( a$"orit%& at %an) to c%ooe fro&.O#viou$(/ %avin" )eci)e) t%e )ata tructure/ execution time can #e a&a?or criterion for e$ection of an a$"orit%&.

 A a firt tep/ it i un)ertan)a#$e t%at an a$"orit%& wou$) #e fat for a&a$$ input )ata. ,or e8a&p$e/ t%e earc% for an ite& in a $it of 1>

e$e&ent wou$) )efinite$( #e fater a co&pare) to earc%in" t%e a&eite& wit%in a $it of 1/>>> e$e&ent. T%erefore/ t%e e8ecution ti&e T ofan a$"orit%& %a to #e )epen)ent upon N/ t%e i4e of input )ata. Now t%e7uetion i %ow to &eaure t%e e8ecution ti&e T. Can we e8act$(co&pute t%e e8ecution ti&e Per%ap not #ecaue t%ere are &an( factort%at "overn t%e actua$ e8ecution of pro"ra& in a co&puter (te&. So&eof t%e i&portant factor are $ite) #e$ow-

1. T%e pee) of t%e co&puter (te&

2. T%e tructure of t%e pro"ra&

!. ua$it( of t%e co&pi$er t%at %a co&pi$e) t%e pro"ra&'. T%e current $oa) on t%e co&puter (te&.

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 19/27

T%u/ it i #etter t%at we co&pare t%e a$"orit%& #ae) on t%eir re$ativeti&e intea) of actua$ e8ecution ti&e.T%e a&ount of ti&e ta*en for co&p$etion of an a$"orit%& i ca$$e) ti(eco(ple/it*  of t%e a$"orit%&. It i a t%eoretica$ eti&ate t%at &eaure

t%e groth rate of t%e a$"orit%& T:n; for $ar"e nu&#er of n/ t%e inputi4e. T%e ti&e co&p$e8it( i &eaure) in ter& of @i"O% notation. A #rief )icuion on t%i notation i "iven in ne8t ection.

2.0.1 (igOh Notation

T%e ig&Oh notation )efine t%at for a $ar"e nu&#er of input 9n+/ t%e"rowt% rate of an a$"orit%& T:n; i of t%e or)er of a function 9"+ of 9n+ ain)icate) #( t%e fo$$owin" re$ation-

!n" 7 O!g!n"".T%e ter& 9of the order+ &ean t%at T:n; i $e t%an a contant &u$tip$eof ":n; for n n>. T%erefore/ for a contant 9c+/ t%e re$ation can #e)efine) a "iven #e$ow-

T:n; c":n; w%ere c>. :2.1;

,ro& a#ove re$ation/ we can a( t%at for a $ar"e va$ue of n/ t%e function9"+ provi)e an upper #oun) on t%e "rowt% rate 9T+.

Let u now coni)er t%e variou contruct ue) #( a pro"ra&&er to write an a$"orit%&.

!1" Si(ple state(ent: Let u au&e t%at a tate&ent ta*e a unitti&e to e8ecute/ i.e./ 1. T%u/ T:n; 1 for a i&p$e tate&ent. To atif(t%e @i"O% con)ition/ t%e a#ove re$ation can #e rewritten a %own

 #e$ow-

T:n; 11 w%ere c 1 an) n> >.

Co&parin" t%e a#ove re$ation wit% re$ation 2.1/ we "et ":n; 1.T%erefore/ t%e a#ove re$ation can #e e8pree) a-

T:n; O:1;

!2" Se0uence structure: T%e e8ecution ti&e of e7uence tructure ie7ua$ to t%e u& of e8ecution ti&e of in)ivi)ua$ tate&ent preent

 wit%in t%e e7uence tructure.Coni)er t%e fo$$owin" a$"orit%&. It conit of a e7uence of tate&ent1 to '-

 A$"orit%& AreaTriangle

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 20/27

{

Step

  1. Read base, height;

  2. Area = 0.5 * base * height;

  3. Print Area;  4. Stop

}

T%e a#ove a$"orit%& ta*e four tep to co&p$ete.T%u/ T:n; '.To atif( t%e re$ation 2.1/ t%e a#ove re$ation can #e rewritten a-

T:n; '1 w%ere c ' an) n>  >.

Co&parin" t%e a#ove re$ation wit% re$ation 2.1/ we "et ":n; 1.T%erefore/ t%e a#ove re$ation can #e e8pree) a-

T:n; O:1;

 5e can a( t%at t%e ti&e co&p$e8it( of a#ove a$"orit%& i O:1;/ i.e./ oft%e or)er 1.

!" he loop structure: T%e $oop tructure iterative$( e8ecutetate&ent written wit%in it #oun). If a $oop e8ecute for N iteration/ it

contain on$( i&p$e tate&ent.Coni)er t%e a$"orit%& Count_zeros of E8a&p$e  w%ic% contain a$oop. T%i a$"orit%& ta*e t%e fo$$owin" tep for it co&p$etion- 

No. of i&p$e tep ' :Step 1/ !/ '/ 3;

No. of Loop of tep 1 to N 1

No. of tate&ent wit%in t%e

$oop

! :1 co&parion/ 1 a))ition/ 1

ai"n&ent;

T%u/ T:n; !N J 'Now for N '/ we can a( t%at

!N J ' '/ !N J N 'N

T%erefore/ we can a( t%at

T:n; 'N w%ere c '/ n>  '

T:n; O:N;

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 21/27

Hence/ t%e "iven a$"orit%& %a ti&e co&p$e8it( of t%e or)er of N/ i.e./O:N;. T%i in)icate t%at t%e ter& ' %a ne"$i"i#$e contri#ution in t%ee8preion- !N J '.%ote: In cae of nete) $oop/ t%e ti&e co&p$e8it( )epen) upon t%e

counter of #ot% outer an) inner $oop. Coni)er t%e nete) $oop "iven #e$ow. T%i pro"ra& e"&ent contain 9S+/ a e7uence of tate&ent wit%in nete) $oop I an) . For (I = 1; I <= N; I++)

{

For (J = 1; J <= N; J++)

{

  S;

}}

It &a( #e note) t%at for eac% iteration of I $oop/ t%e $oop e8ecute Nti&e. T%erefore/ for N iteration of I $oop/ t%e $oop wou$) e8ecute NN N2 ti&e. Accor)in"$(/ t%e tate&ent S wou$) a$o e8ecute N2ti&e.

T%u/ T:n; N2

To atif( t%e re$ation 2.1/ t%e a#ove re$ation can #e rewritten a %own

 #e$ow-T:n; 1 N2  w%ere c 1 an) n> >.

Co&parin" t%e a#ove re$ation wit% re$ation 2.1/ we "et ":n; N2.T%erefore/ t%e a#ove re$ation can #e e8pree) a-

T:n; O:N2;

Hence/ t%e "iven nete) $oop %a ti&e co&p$e8it( of t%e or)er of N2/ i.e./O:N2;.

E/a(ple 8: Co&pute t%e ti&e co&p$e8it( of t%e fo$$owin" nete) $oop- For (I = 1; I <= N; I++)

{

For (J = 1; J <= I; J++)

{

  S;

}

}

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 22/27

Solution: In t%i nete) $oop tructure/ for an iteration of I $oop/ t%e $oop e8ecute in an increain" or)er fro& 1 to N a "iven #e$ow-

1 J 2 J ! J ' FF :N = 1; J N N:N J 1;M2 N2M2 J NM2.

T%u/ T:n; N2M2 J NM2.Now for N2  NM2/ we can a( t%at-

N2M2 J NM2 N2M2 J N2  ! N2M2.

T%erefore/ we can a( t%at

T:n; ! N2M2 w%ere/ c !M2/ n>  1M2T:n; O:N2;

Hence/ t%e "iven a$"orit%& %a ti&e co&p$e8it( of t%e or)er of N2/ i.e./O:N2;. T%i in)icate t%at t%e ter& NM2 %a ne"$i"i#$e contri#ution in t%ee8preion N2M2 J NM2.

!" If&then&else structure: In t%e ift%ene$e tructure/ t%e t%en an)e$e part are coni)ere) eparate$(. Coni)er t%e ift%ene$e tructure"iven in ,i"ure 2.0.

Fig. 2.5 T%e ift%ene$e tructure

Let u au&e t%at t%e 9tate&ent 1+ of t%en part %a t%e ti&e co&p$e8it(T1 an) t%at 9tate&ent 2+ of e$e part %a t%e ti&e co&p$e8it( T2. T%eti&e co&p$e8it( of ift%ene$e tructure i ta*en to #e t%e &a8i&u& oft%e two/ i.e./ &a8:T1/ T2;.

E/a(ple 19: Coni)er t%e fo$$owin" a$"orit%&. Co&pute it ti&eco&p$e8it(. If (x > y)

{  x = x+1;

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 23/27

}

Else

{

  For (i=1; i <= N, i++)

  {  x = x+i;

  }

}

Solution: It &a( #e note) t%at in t%e a#ove a$"orit%&/ t%e ti&eco&p$e8it( of 9then+ an) 9else+ part are O:1; an) O:N;/ repective$(. T%e&a8i&u& of t%e two i O:N;. T%erefore/ t%e ti&e co&p$e8it( of t%ea#ove a$"orit%& i O:N;.

E/a(ple 11: co&pute t%e ti&e co&p$e8it( of t%e fo$$owin" re$ation-

1. T:n; 232

2. T:n; 0n J 1

!. T:n; 13N2 J

'. T:n; 3N! J N2  2N

Solution:

1. T:n; 232 2321/ w%ere c 232 an) n>  >.

  O:1; An. T%e ti&e co&p$e8it( O:1;

2. T:n; 0n J 1 0n J n n for c an) n>  1

  O:N;

 An. T%e ti&e co&p$e8it( O:N;

!. T:n; 13N2 J 13N2 J N for N

Now for N N2

/ we can rewrite t%e a#ove re$ation a "iven #e$ow-13N2 J N 13N2 J N2  1 N2 for c 1 an) n> 

T%u/ T:n; O:N2;

 An. T%e ti&e co&p$e8it( O:N2;

'. T:n; 3N! J N2 J 2N

,or N2  2N/ we can rewrite t%e a#ove re$ation a "iven #e$ow-

3N! J N2 J 2N 3N! J N2 J N2  3N! J 2N2

,or N!  2N2/ we can rewrite t%e a#ove re$ation a "iven #e$ow-

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 24/27

3N! J 2N2  3N! J N!  N! for c an) n>  2

T%u/ T:n; O:N!;

 An. T%e ti&e co&p$e8it( O:N!;

%ote: A$$ t%e re$ation/ "iven in e8a&p$e 1> were po$(no&ia$. ,ro& t%eanwer/ we can a( t%at an( po$(no&ia$ %a t%e ti&e co&p$e8it( of @i"O% of it $ea)in" ter& wit% coefficient of 1.It i a$o un)ertan)a#$e t%at t%e @i"O% )oe not provi)e t%e e8acte8ecution ti&e. Rat%er/ it "ive on$( an a(&ptotic upper #oun) of t%ee8ecution ti&e of an a$"orit%&.,or )ifferent t(pe of function/ t%e "rowt% rate for )ifferent va$ue ofinput i4e n %ave #een ta#u$ate) in Ta#$e 2.1.

It can #e o#erve) fro& Ta#$e 2.1 t%at for a "iven input/ t%e "rowt% rateO:$" n; i fater t%an O:n $" n;. ,or t%e a&e input i4e/ t%e "rowt% rateO:N2; i t%e $owet.Later in t%e #oo*/ for variou a$"orit%&/ ti&in" eti&ate wi$$ #einc$u)e) uin" @i"O% notation. able 2.1 Vrowt% rate of function

EWERCISES

1. 5%at i a tore) pro"ra& co&puter

2. Vive t%e #$oc* )ia"ra& of <on Neu&ann arc%itecture of a co&puter.

!. 5%at i a )ata tructure 5%at i t%e nee) of )ata tructure'. Lit out t%e area in w%ic% )ata tructure are app$ie) e8tenive$(. 5%( i it

i&portant to &a*e a ri"%t c%oice of )ata tructure for pro#$e& o$vin"

3. 5%at are #ui$tin )ata tructure Vive o&e e8a&p$e.

. 5%at are uer)efine) )ata tructure Vive o&e e8a&p$e.

. Coni)er a ituation w%ere it i re7uire) to ran* 1>> e&p$o(ee of an

or"ani4ation. T%e )ata of an e&p$o(ee conit of %i na&e/ )ei"nation/

)epart&ent/ e8perience an) a$ar(. Vive t%e poi#$e c%oice for t%e )ata

tructure. Draw a co&parion #etween t%e poi#$e c%oice of or"ani4in"

)ata.

0. 5%at i t%e )ifference #etween )ata t(pe an) )ata tructure

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 25/27

. Differentiate #etween $inear an) non$inear )ata tructure. E8p$ain wit%

t%e %e$p of e8a&p$e.

1>. E8p$ain t%e fo$$owin" #aic ter&ino$o"ie aociate) wit% t%e )ata

tructure-

i. Data e$e&entii. Pri&itive )ata t(pe

iii. Data o#?ectiv. Contant v. <aria#$e

2. 5%at i an a$"orit%& Differentiate #etween a$"orit%& an) pro"ra&.

!. Lit t%e )eira#$e feature of an a$"orit%&.

'. 5%at are t%e c%aracteritic poee) #( an a$"orit%&

3. 5%at are t%e tep t%at can #e u""ete) for )eve$opin" an a$"orit%&Coni)er a uita#$e cenario an) )eve$op an a$"orit%& for o$vin" t%e

pro#$e&. E8p$ain eac% tep w%i$e )eve$opin" t%e a$"orit%&.

. 5%at are t%e contro$ tructure E8p$ain t%e e7uence contro$ tructure

 wit% t%e %e$p of e8a&p$e.

. 5rite an a$"orit%& t%at co&pute area an) peri&eter of a rectan"$e.

0. 5rite an a$"orit%& t%at co&pute 7uare of a nu&#er #( repititive a))ition.

. 5rite an a$"orit%& t%at co&pute VCD an) LC of two "iven nu&#er.

1>. E8p$ain t%e e$ection :con)itiona$ contro$; tructure wit% t%e %e$p of

e8a&p$e.

11. 5rite an a$"orit%& t%at )eter&ine w%et%er a nu&#er i even or o)).12. 5rite an a$"orit%& t%at co&pute profit or $o incurre) #( a a$eperon.

1!. 5%at i iteration

1'. 5rite an a$"orit%& t%at ort a "iven $it of nu&#er in acen)in" or)er.

13. 5rite an a$"orit%& t%at )eter&ine w%et%er a nu&#er i pri&e or not.

1. 5%at i t%e ue of accu&u$ator an) counter

1. 5rite an a$"orit%& t%at co&pute avera"e &ar* of > tu)ent of a c$a.

10. 5rite an a$"orit%& t%at fin) t%e nu&#er of o)) an) even preent in a

"iven $it of N nu&#er.

1. 5%at are t%e para&eter on t%e #ai of w%ic% an a$"orit%& can #eana$(4e)

2>.Define t%e ter& 9ti&e co&p$e8it(+. How can t%e ti&e co&p$e8it( of a "iven

a$"orit%& #e foun)

21. E8p$ain @i"O% notation wit% t%e %e$p of e8a&p$e.

22.Co&pute t%e ti&e co&p$e8it( of t%e fo$$owin" re$ation-

i. T:n; '1>ii. T:n; 'n 12

iii. T:n; 1!N2 

iv. T:n; !N! J N21'N

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 26/27

2. 5%at i t%e ti&e co&p$e8it( of t%e fo$$owin" $oop

 

for (int i = 0; i < n; i++)

{

statement;

}

 

!. 5%at i t%e @i"O% co&p$e8it( of t%e fo$$owin" nete) $oop

i. for (int i = 0; i < n; i++)

{

for (int j = 0; j < n; j++)

{

  statement;

}

}

ii. for (int i = 0; i < n; i++)

{

for (int j = i + 1; j < n; j++)

{

  statement;

}

}

iii. for (int i = 0; i < n; i++)

{

for (int j = n; j > i; j--)

{

  statement;

}

}

iv. for (int i = 0; i < n; i++)

{

for (int j = 0; j < m; j++)

{

  statement1;

}

statement2;

}

2. Coni)er t%e fo$$owin" a$"orit%& an) co&pute it ti&e co&p$e8it(.

 if (a==b)

7/18/2019 Chapter 2

http://slidepdf.com/reader/full/chapter-2-5691df4b734ab 27/27

statement1;

else if (a > b)

statement2;

else

statement3;