Dsa 101
Dsa 101
DSA101
AConciseIntroductiontoData
StructureandAlgorithm
PreparedbyWondmenehFekadu
Datastructureandalgorithm101 Bywondmenehfekadu
Contents
Introduction
Unit
1:
Introduction
to
DSA
● Data
structures
● Abstract
Data
type
● Classification
of
data
structure
○ Primitive
and
Non-primitive
○ Linear
or
Non-linear
● Array
revision
● Pointer
revision
● Algorithm
● Properties
of
algorithms
● Expressing
algorithms
○ Natural
language
○ Flowchart
○ Pseudocode
○ Algorithms
complexity
analysis
○ Operation
count
○ big-O
○ theta
○ Omega
● Best
case
analysis
● Worst
case
analysis
● Average
case
analysis
Datastructureandalgorithm101 Bywondmenehfekadu
WordsfromtheMe
omakethemostofthisbook,hereareafewthingstokeep
T
in mind. The book is designed to help you understand the
basic concepts of datastructuresandalgorithms.Isuggest
reading it before your DSA lectureclassorbeforeyoustart
usingthestudymaterialsfromyourschool.
ostimportantlydon'tforgettosharewhatyouknowwith
M
yourclassmatesorsomeonewhoneedsyourhelpforhis/
hereducationalsuccess.
Happyreading!!!
GoodLuck
Unit
1
Introduction
to
Data
Structure
and
Algorithm
atastructureandalgorithmsserveasthemainpillarsofthe
D
computerrealm.Thedatastructureisthestorageandorganization
ofdata,whereasalgorithmsarestep-by-stepinstructionsfor
solvingacomputingproblem.
atastructureandalgorithmarealsothecoreconceptsof
D
computerscience,softwareengineering,andrelatedfieldssince
theyplayasignificantrolewhenweareworkingwithtimeand
spacecomplexity.
on’tworryifyoudon’tknowthetermsaboveaswewillgothrough
D
eachofthem.
1.1Datastructure
hedatastructuremainlyfocusesonawayoforganizing,
T
collecting,arranging,andorderingdatathatcomesfromvarious
sources(Thedatamaybegeneratedfromapplicationsweusedayto
daytomakeourlifeeasier.). Sothattimeandmemorycomplexity
willbeefficient.
I notherwords,ifdataisputinanorganizedwayspaceittakesto
storeitwillbeverysmallandwecanstorealotofdatausinga
smallspace.Also,thetimeittakestoaccessdatawillbeshort
becausewehavetomakeitveryeasytoaccessandourprogram
willnotspendunnecessarytimesearchingitfromlargeandmessed
updatasets.
Datastructureandalgorithm101 Bywondmenehfekadu
Thefollowingcasewillmaketheexplanationclearer.
Case1Solomon'sclothes
olomonistoolazytoorganizehisclothesasmostofusdo.His
S
clothesarehereandthereallovertheplace.Someareonthechair,
othersareonthebed,afewareonthesofaandevensomearelost.
einginthissituationlookhowmuchspaceittooktoputhis
B
clothes,ifitwasorganizedIbetitwouldn’tbemorethanone
clothesbag.Thesameanalogyworksfordataifitisnotorganized
itwilltakemorespacethanitshouldtake.
emmeaddonequestionforyoutoo.HowfastwouldSolomontake
L
tofindhisredT-shirt,greytrousers,andyellowjacket?Iamsureif
itisnotatthefrontbyluckittakeshimalotoftimeandtiringthat
makeshimdecidetowearrandomclothesthatappearatthefront.
Thesameanalogygoesfortimedataifitisnotorganizedproperly
ittriestochecktheentirefilesonebyone.
utimagineifhisclotheswereorganizedwellbycategories,all
B
T-shirtsononeside,alltrousersononeside,allshortsononeside,
alljacketsononeside,itwouldtakeasmallspaceandSolomon
wouldneverbetiredtofindhisfavoriteclothesandneverbe
disappointed.
2
Datastructureandalgorithm101 Bywondmenehfekadu
1.2AbstractDatatype
ounderstandtheconceptsofADTlet’sexpandthetermsoneby
T
one.
bstract:inprogramming,theabstracthideshowthesystemis
A
working,andjustitgetstheworkdone.Itmeanswedon’tknow
howitdoesitbutweseewhatitdoes.
Case2:TheCoca-Coladrink
lmosteveryoneonthisplanethasdrunkoratleastheardthe
A
nameCoca-Colasoftdrink.Butnooneknowstheformulabehind
Coca-Cola'ssoftdrink.Thatiswhatabstractis.
Data:itisapieceofinformationweuseinprogramming.
Type:Theyarestrings,lists,integers,stacks,queues…
oADTisasetofrulesfororganizingdataandworkingwithdata
S
inprogramming.Sobysayingthis,weunderstandthatitis
independentofprogramminglanguageitisjustaformal
descriptionnotcode.
ecandoprogrammingoperationswithindatainADT.likeadding,
W
andsubtracting….
eneedtodecidewhichdatastructuretousewhenwecreateADT.
W
WecanuseLinkedlists,arrays,orstacks…..TocreateADT.This
decisionisuptoyouwhichyouthinkisthebestchoiceforyour
projectorproblem.
3
Datastructureandalgorithm101 Bywondmenehfekadu
1.3Classificationofdatastructure
I nthedatastructure,weusemorecomplexandsophisticateddata
typesratherthansimpledatatypeslikestringandint...Now
stacks,linkedlists…areadded.Wewilldiscussthemindetailaswe
moveon.
heyaremainlydividedintotwobroadcategories.Primitiveand
T
Nonprimitives.
rimitives:thosearetypesinwhichwecandirectlyinteractand
P
manipulatethedatausingprogramming.Noneedtousethe
indexingmethodorothercomplexwaystoaccessit.Theyare
integers,floatingpointnumbers,characters,stringconstants,and
pointers…andtheyarebasiccomponentsofnon-primitives.
Meaningnon-primitivesareacollectionofprimitives.Ourfocusis
notonprimitives.
on-primitives:Theycanbeagroupofthesamedatatypeslike
N
arraysordifferentdatatypeslikestructures.Simplydatatype
whichiscreatedfromanotherdatatype.
Array,lists,files,linkedlists,trees,andgraphsaregoodexamples.
on-primitivesareagaindividedintotwoLinearlistsand
N
Non-linearlists.Wewilldiscusseachdatastructureonebyone
sincetheyhavetheirchapter.
4
Datastructureandalgorithm101 Bywondmenehfekadu
Case3Thefoods
sweknowwecaneatfoodmainlyintwoformsoneonitsown
A
withoutbanana,orange,andmangowejustpeelandeatthem.
Thesameanalogytoprimitiveswejustusethemontheirown.
therformsoffoodarethosewhichrequirecooking,asyouknowif
O
wearecookingwemostlymixsomeingredients.Sowehavetouse
acollectionoffoodstocreateafood.
hesamethinggoesfornon-primitivesweuseagroupofdata
T
typestocreatedatatype.
uthereisacatchbothofthemareuseful,butweneedtobe
B
carefulwhilechoosingthem.Wecaneatbananasor‘D orowot’ our
entirelifeandmanagetolive,butsincetheirnutrientcontentis
differentitaffectsusinthelongrun.
ecanuseprimitivesforacomplexprojectanditstillgetsthe
W
workdonebutinthelongrun,itwillaffectourprogramtimeand
spacecomplexity.
I naddition,ifthereisaholidaywhatdoweprepareakilosof
mangoesoradelicious‘Dorowot’whichisamixingofdifferent
ingredients?Theanswerisobvious‘Dorowot’.
oifaprogramissimpleanddoesn’tneedacomplexityuse
S
primitive.Butifitisquitelarge,complex,andsophisticatedgofor
non-primitivesasitmakesourlifeeasier(O nlyforprogramming
😄 ).
5
Datastructureandalgorithm101 Bywondmenehfekadu
ewilldiscussmoreindetailhownon-primitivesmake
W
programmingefficientlaterinthisbook.
1.4Arrayrevision
narrayisadatatypethatisusedtostorethesamedatatypesin
A
onevariable.
Wedeclarethearrayinthefollowingform;
datatypename[size];
nt
i price[ 5
]
=
{1
,
2
,
3
,
4
,
5
}
InC++wehavesetthesizeofanarrayatdeclaration,welimitthe
sizeonlytoaspecifiedsizetosaveunnecessarymemoryusage,
eventhoughwecansetitdynamicanditssizewillincreaseasthe
itemsinsideincrease.
Case4Theweddingattendant
ikiyas’sweddingissoon,andheisplanningtoinvitehisfriends,
M
relatives,co-workers, andpeopleheknows.Hethinksoftwoways
todoit.
1. T onamepeopleheknows,countthemandtheresource
willbepreparedbasedonthenumberofpeopletobe
invited.
2. Topreparetheresourceattheexcessamountpeople
willuseasmuchastheywantandthedoorwillbeopen
toanyone.
WhichoneoftheaboveyouwoulddoifyouwereMikiyas?
😄
(Behonestdon’ttrytobeaniceperson )
6
Datastructureandalgorithm101 Bywondmenehfekadu
fcoursethefirstchoice.Andhereiswhyyoudothis.
O
Becauseifyoudon’tlimitthenumberofpeopleyouinvite,
therewillbeextravagancysoyoumightgobankrupt.
orthesamereasontousememoryefficiently
F
limitingthesizeofthearrayisquiteimportant.
1.5Pointerrevision
pointerisavariablethatstorestheaddressofanothervariable.
A
Wedeclareitthefollowingway.
nt
i ptr
,
var
;
var
=
value;
ptr
=
&
var
;
Case5whereisthepolicestation?
omeonestrangerwhoisnewtoyourvillagesuddenlyappearsand
S
asksyouwhereisthepolicestation.Andyoutellhimitis100min
theeastdirection.
ouknowwhatjusthappenedhere?Thepolicestationaddressis
Y
storedinyouandyoujustpointatit.Thatishowapointerworks.
1.6Algorithm
healgorithmisastep-by-stepguidetofollowtosolveagiven
T
problem.Itisaguideaprogrammerhastofollowtobuildaproject
orsolveaproblem.
7
Datastructureandalgorithm101 Bywondmenehfekadu
Case6Beso‘በሶ'makingprocess
suniversitystudentsweknowthat‘በሶ’playsacrucialroleinour
A
campuslife,andwearefamiliarwiththemakingprocessofit.But
lemmeaskyouaquestionbeforeyouknowit. Someonehastaught
youoryousawsomeonedoingit.Right? Whatdidyougettaught
orwhatdidyouseewhensomeonemakesa'በሶ'?Herearethe
thingsyousee.
.
1 tarttheprocess
S
2. Prepareaplasticcup,flour,water,andsugar.
3. Putflour,water,andsugarinaplasticcup.
4. Shakeituntiltheyarecompletelymixed.
5. Testit.
6. Ifit'sokay,finish.ifnotshakeagain.
7. Endtheprocess
ouseetheaboveinstructionsformaking‘በሶ'.Wecallthesame
Y
mechanismanalgorithmforcomputationalproblems.
nalgorithmisnotacodeoritdoesn’tsolveaproblemonitsown.
A
Itisjustalogicofhowthesolutionworks. Soprogrammerslikeyou
willuseitasaguideforcodingsolutions.
1.7Propertiesofthealgorithm
nalgorithmhasthepropertiesthatmakeitagoodalgorithm.Not
A
everyprocedureisanalgorithm.Analgorithmshouldhavethe
followingproperties.
Wewillusecase6todemonstrateproperties.
8
Datastructureandalgorithm101 Bywondmenehfekadu
. Input-itshouldtakeinput.
1
Weprepareflour,water,andsugarinaplasticcup.An
algorithmshouldhavesomethingtotakefromtheuseror
sensor.
2. O
utput-itshouldgiveoutputattheendofthe
program.
Thewholeprocessistodrinkadelicious‘በሶ'attheend.And
wemanagedtodrink.
3. I
tShouldbeclear-itshouldn’tbeambiguous.The
instructionshouldbeclearandsimpletounderstandby
aprogrammer.
Seetheinstructionstheyareeasy,andevensomeonewithno
experiencecanreaditandmakeitbyhimself.
4. T
heremustbeanend- thealgorithmmusthavea
pointwhereitgetsterminated.
Ifyourecognizewestoppedoncewegotour‘በሶ'.Thereis
nothinglefttodoafterthat.
5. S
houldbeeffective-analgorithmshouldconsiderthe
availableresourcesformakingasolution.
Imagineontheinstructionthatitsaystoadd2spoonsof
cheese,100mlofgoat’smilk,and3spoonsofbutter.Asa
student,youcouldn’tgetthosethings.Soyoucan’tmake
‘በሶ'.soinstructionshouldconsiderwhatyoucanaccessinyour
dorm.
9
Datastructureandalgorithm101 Bywondmenehfekadu
6. I
tshouldbelanguage-independent-analgorithm
shouldbeabletobeimplementedinanylanguageof
theprogrammer'schoice.
1.8Expressingalgorithms
nalgorithmcanbeexpressedindifferentways.Basedonthe
A
complexityofaproblemorthepreferenceofapersonwho
preparesit.
Themainwaysofexpressinganalgorithmarethefollowing.
1. Naturallanguage
2. Pseudocode
3. Flowchart
Let’sseethemindetail.
Naturallanguage
I tiswhenyouprepareaninstructionusingjustplainlanguage
usingasequenceofactions.Thismeansyoujustguidea
programmertosolveaproblemlike‘dothisthendothatafterit
comestodothatandgoeson…’
Inthefollowingexample,wewillillustratethesituation.
Case7Theentranceanswersheet
ntheorientationuniversityentrancenationalexamination,we
O
weregiventhefollowinginstructionsonansweringthequestion.
Prepareapencil,sharpener,andrubber.
10
Datastructureandalgorithm101 Bywondmenehfekadu
harpenyourpencil,calmdown,andblackentheansweronthe
S
givenanswersheet.Ifyouthinkyoumadeamistakeeraseitby
yourrubberandre-blacken.Andgiveittotheexaminer,whenyou
aredone.Thisiswhendefinedbynaturallanguage.
Flowchart
I tisawayofexpressingalgorithmsusingdiagrams. Themain
componentsare:
1. Terminal-startandend.
2. Decision
3. Process
4. Inputandoutput
Let’sseethemindetail.
erminal
T
Itisanindicationofwheretheexecutionstartsandends.
I fyouthissymbolatthebeginningtheexecutionstartsthereandif
youseeitattheenditmeanstheexecutionendsthere.
ecision
D
Itisanindicationofaconditionalstatementthatexecutescertain
statementsbasedoncertainconditions.Simplywecanrefertoitas
ifstatement.
11
Datastructureandalgorithm101 Bywondmenehfekadu
I fyouseethissymbolyoucanbesurethatthereisanexecution
thatisbasedonsomeconditions.
rocess
P
Itindicatesaprocesslikeaddition,subtraction,orfetchingdata
fromdatabases…butjustknowthatthereissomeprocessbeing
carriedon.
nput-output
I
Itindicatesthereissomethingthattheprogramtakesfromthe
userorsensorandthereissomethingwegetfromtheprogramas
finaloutput.
Feelconfused?Let’sseetheminuse.Inthenextsection.
12
Datastructureandalgorithm101 Bywondmenehfekadu
Flowchartstructures
Thereare4typesofflowchartstructures.Andheretheygo.
.
1 equence
S
2. Decision
3. Repetition
4. Case
et’sseethemindetail.
L
Sequence-theseriesofactionsguidedbyinstructions.
Example-Gotobookshelf→Locatethebook→Pickupthebook
Credit-https://www.yaclass.in/
ecision-dependingontheconditiononeofstatementswillbe
D
executed.
xample: IfthedecisionisYes,step1willbeexecutedandifitis
E
Nostep2thenstep3willbeexecuted.
13
Datastructureandalgorithm101 Bywondmenehfekadu
Credit-https://www.gleek.io/
epetition-theexecutionwillberepeatedoverandoverbasedon
R
somecondition.Therearetwotypesofrepetition.Pre-Testand
Post-Test.
re-test-Conditionmustbesatisfiedtobeexecuted.Examplesare
P
for-loopandwhile-loop.
Credit-https://www.edrawmax.com/
14
Datastructureandalgorithm101 Bywondmenehfekadu
ost-testrepetition -isakindofrepetitionthatchecksconditions
P
afterthefirstiteration.Foriterationthatcomesaftercondition
assessmentwillbecarried.
Case8Soccertournament
I fwesaysoccerleagueanumberofteamsparticipateinthegame
butonlyoneteamliftsthecup.Ifweseeitdeeplythetournament
organizerusesthesametechniqueaspost-test.Theyletteamsplay
atleastonetimeandtheirnextmatchwilldependontheir
performance.Itmeanstheymayplayornotnexttime.
hebottomflowchartrepresentsthatitwillprintaaslongasais
T
lessthanaislessthanorequalto5.
redit-https://www.authorcode.com/
C
Case-foreveryconditionthatisconsideredtohappenthereisa
statementtobeexecuted.Ithasalsosomethingcalleddefault
casewhichwillbeexecutediftheinputisoutofalternatives.
15
Datastructureandalgorithm101 Bywondmenehfekadu
Case9Gradeanditsamount
I fyougetAitsamountis4,ifit’sB itsamountis3andifC,itwill
be2andifitisDitwillbe1,andfinally,ifitisFitwillbe0.Ifit
letterisnotmentionedaboveitwillbeaninvalidgrade.
Credit-https://www.zenflowchart.com/
Thisisallaboutflowchartsatabasiclevel.
16
Datastructureandalgorithm101 Bywondmenehfekadu
1.9Algorithmcomplexityanalysis
heremightbeadifferentapproachtosolvingagivenproblem.
T
Andwewillhavetousetheefficientone.Thismeansitisbothtime
andspace-efficient.
ranalgorithmthatexecutesfasterandoccupiesasmallspacein
O
acomputerbecausebothtimeandspacearepreciousina
computerworld.
paceandtimecomplexityaretwocorethingstoconsiderwhile
S
designinganalgorithm.
pace-complexity
S
Itishowmuchmemoryisneededforexecutinganalgorithm.So
thesimpleralgorithmthesmallerspacewillbeneeded.
MemoryreferstoRAM(Randomaccessmemory).
ase10Appsonyourphone
C
Let’slookatyourphoneandIamsureyouplaymobilegames.
ThereareseveralgameswhichquitepopularlikePUBG,popstar,
ClashofClans,CandyCrush,etc…
ornow,let’spickPopstarandPUBG.lemmeaskyouaquestion,
F
fromtwoofthemwhichoneofthemrequireshighRAMtorun? The
answerisobviousPUBG.becauseithasHDgraphics,animation,
sound,etc…soalltheseresourcestakeupquitealotofspaceona
phonethatiswhysmallqualityphonesgetbusywhileyoutryto
suchgamesusingthem.
17
Datastructureandalgorithm101 Bywondmenehfekadu
FactorsaffectingSpace-complexity
hoiceofdatatypes-inbasicC++wesaweachsizeofmemoryof
C
eachdatatype.Sochoosingthemreallyplaysa crucialrolein
Space-complexityefficiency.
singrecursivefunctioninaprogram.Theyareafunctionthatis
U
definedintermsofitself.Andthemoredeeptherecursivefunction
goesthemorespacewillbeused.
izeofinput-moreinputenterstoprogrammorespaceisusedto
S
processit.
ThisiswhyyourPCwillbebusywhenyoutrytorunabig
applicationonyourcomputer.
ariables-thenumberandsizeofvariablesusedinaprogram
V
playsignificantproblems.
ime-complexity
T
Inthecomputerworld,thetime-complexityisthetimewhichisthe
amountoftimethattakestorunanalgorithm.Itislikefindingthe
fastestwaytosolveaproblem.
Case11Andnet’sjourneytohishome
ndnetcomesfromDireDawatoAdamatolearnatASTU.after
A
finishingthefinalexam,hedecidedtotakeabreakandrelax,sohe
planstogotoDireDawa.Nowasyouknowthereareabunchof
waystogettherefromAdama.
18
Datastructureandalgorithm101 Bywondmenehfekadu
hefirstwayistogotoAddisAbabaandget Selam,Golden,or…
T
whatevertransportationservice.Hecanalsogettherebyusingthe
routetoGonderCityandgoingtoDireDawafromGondar.And
finallyusingtrainservicefromAdamacity.
eebyusingalltheabovewayshecanreachhisfinaldestination.
S
Butwhichisefficient(wediscussedefficiencyinpropertiesofthe
algorithm)?Sowhichoneheshouldtake togotohishometown?
Ofcoursetogetatrainfromhere.Right?Becauseitistheshortest
andmostreliableway.
hoosinganalgorithmthattakesashorttimetobeexecutedis
C
crucialinefficientprogramming.
Factorsaffectingtime-complexity
Wewillusetheabovecasetodemonstratethem.
Analgorithm-thepaththatistaken.
nputsize-themoredatatakenintotheprogrammoretimetakes
I
toprocessthem.Forthislet’sMillHouse(ወፍጮቤት)asanexample,
themoreteffyouenterthemachinethemoretimeittakestogrind
it.
esource-Ifthecomputer’sCPUspeedandRAMsizearevery
R
efficientthealgorithmwillbeexecutedfast.
Ifthetrainworksbycharcoalorelectrictheyhavedifferences.
hoiceofprogramminglanguage-wehavelearnedin
C
fundamentalsofprogrammingthatithasanimpactonprogram
executiontimewhetherwechooseMachinelanguage,Compile
19
Datastructureandalgorithm101 Bywondmenehfekadu
language,orInterpretedlanguage.Machinelanguagehasthe
fastestexecutiontime.
xample
E
ItislikechoosingfromaBicycle,motorbike,Car,Train,orairplane.
Allofthemgetthejobdonebutwehavetochoosethefastest.In
ourcasewhichistheairplane.
Wecanmeasurethetimetakestobeexecuted.
Operationcount-itreferstocountingthebasicprocessesand
stepsinanalgorithmtoprocessagivendataorsolveaproblem.
Case12-Revisingstudies
upposeyouarerevisingwhatyoustudyforupcoming
S
exams.Thestepsyoutakearelistedasfollows.
.
1 kimreading
S
2. Readmoreindetailaboutwhereyoufellconfused
3. Workontheworksheetthatisgiventoyou
4. Finally,workonlastyear'sexams
eethereare4basicstepsyoutaketoreviseonesubject(one
S
course).Butwhatifyouplantorevisensubjects,itmeansyouhave
torepeatthisprocessntimes.Itmeansyouhavetoperform4n
steps.
hegoalhereistoprepareanalgorithmwithaloweroperation
T
countforbetterefficiency.
20
Datastructureandalgorithm101 Bywondmenehfekadu
Growth-RateFunction
I tisaconceptoftenassociatedwithtimecomplexity.Itdescribes
howthetimethattakestorunaprogramwillincreaseastheinput
sizeincreases.Asdiscussedabovetopicifoperationcountsishigh
timetakentorunisalsohigh.
I tisoftenexpressedusingBig-Onotation,whichdescribesthe
upperboundofanalgorithm’sgrowthrate.
Thereareseveralwaysofexpressingthegrowthratefunction.
1. O
(1)-ConstantTime:Thetimerequiredisconstant,it
doesnotdependonthesizeoftheinputoroperation
count.
ase13: Salingattheshop
C
Let’ssayyouareasalespersonattheshop,andsomeonecomes
andasksyoutogivehimapacketofchewinggum.Yougivehimin
notime.Andalsoanotherdudecomesandasksyoutogivehim
candy,seemslikeitdoesn’ttakeanyadditionaltime.
I tmeansthetimetakestoserveacustomeristhesameandit
doesn'tdependonhowbigyourshopisorhowmanycustomers
youhave.
2. O
(logn)-LogarithmicTime:Thetimerequiredwill
increaseslowlyastheproblemsizeincreases.
Binarysearchisagoodexample.(wewillseeitinChapter2in
detail).
21
Datastructureandalgorithm101 Bywondmenehfekadu
3. L
inearTime(O(n)):Thetimerequiredwillincreaseas
theproblemsizeincreases.Theygohandinhand.
ase14:FindingaLostPersonPublicHoliday
C
Weallhavebeeninasituationwherewesomehowgotlostfrom
ourfamilywhenwewerecelebratingpublicholidays.LikeMeskel,
Timket,andEidal-feter….I(waslostduringTimketandmyfamily
😄
almostkilledmewhentheyfoundme ).Nowanswerthequestion
that,fromthefollowingsituationwhichonewillbeeasierforour
parentstofindus?
I fpeople'ssizeissmallorquitebiglarge.Ofcourse,ifitissmall.
Seeifinputsizeincreasestimeittakesincreasestoo.
4. LinearithmicTime(O(nlogn)):Thetimeittakes
increasesmorerapidlythanlineartime.
Case15:Abraham'sfriend'sbirthday.
brahamhasalotoffriendsuntilhestrugglestoremember
A
arrangingtheorderoftheirbirthdays.Tryingtoarrangeinonetry
ishard,soheusesasmartway.
eseparatesthemintosmallgroupsandarrangeseachgroup.He
H
repeatsthesameprocessforallgroupsandfinallyarrangesallof
hisfriend'sbirthdays.
ItisusedinMergeSortorHeapSort.wewillseetheminthefuture.
5. O
(n^2)-QuadraticTime:Thetimerequiredincreases
rapidlywiththesizeofaproblem.
ubblesortisagoodexample.
B
22
Datastructureandalgorithm101 Bywondmenehfekadu
ase16: Biniyamlostphone.
C
Biniyamlosthisphoneinalibrary.Sohesuspectsitisstolenby
someone.Sohetellsthelibrariantoneverletstudentsouthestarts
tofindhisphonebytryingtocheckeachstudent'spocket.Heis
alsocheckingifitmatcheshisphone.Luckyforhim,hefindsitin
onestudent’spocket.
eetimerequiredtodothistaskincreasesrapidlywiththesizeof
S
theproblem.
6. O
(n3)-Cubictime:Thetimerequirementforacubic
algorithmincreasesmorerapidlywiththesizeofthe
problemthanthetimerequirementforaquadratic
algorithm.
7. E
xponentialTime(O(2^n)):Asthesizeoftheproblem
increases,thetimerequirementforanexponential
algorithmincreasestoorapidlytobepractical.
Bestcaseanalysis
I tisapossibleminimumtimeorresourcesthattakesanalgorithm
tocompleteatask.Indicatesthealgorithmisbest.
Worstcaseanalysis
I tisapossiblemaximumtimeorresourcesthattakesanalgorithm
tocompleteatask.Indicatesthealgorithmisbad.
Averagecaseanalysis
I tisa normaltimeorresourcesthattakesanalgorithmto
completeatask.Indicatesthealgorithmisnotbadorbest.
23
Datastructureandalgorithm101 Bywondmenehfekadu
Case17Ahmed’sjourneytoschool
hmedisgoingtohisschool,sothereare3waystogoeven
A
regardingarrivingatschool.
. G
1 ousingataxi-veryfast(Bestcaseanalysis)
2. Gobywalking-Normaltime(Averagecaseanalysis)
3. Gowithhisfriendsplayingandtalking-Veryslowly
andhemightbelateforschool(Worstcaseanalysis)
24
Datastructureandalgorithm101 Bywondmenehfekadu
Unit
2
Simple
searching
and
sorting
algorithms
ehaveseenindetailwhatalgorithmsmeanwiththeir
W
properties.Wehavealsodiscussedalgorithmcomplexityanalysis.
owitistimewhenweseehowanalgorithmcanbe usedfor
N
solvingcomputationalproblems. Wewilldevelopasimple
algorithmforsearchingandsortingpurposesandwewillwritea
C++programtoimplementthealgorithm.
Searchingalgorithms
tisquiteusefulforlocatingspecificdatafromacollectionof
I
datalikearrays.Theyaredesignedtoretrievetargetdatafrom
whereitisstored.
Case1Searchingcontactfromyourphone.
magineifwecan’tsearchforaspecificnameandweare
I
supposedtocheckeverycontactofourphonetocalltoourfriend.
Howboringitwouldbe?Butthankstomodernapplicationsweare
notsupposedtodothistiresometask.Andhereishowthose
applicationsmakethesearchingtasksoeasy.
Wewillseealgorithmsasfollows.
25
Datastructureandalgorithm101 Bywondmenehfekadu
Linearsearch
tisanalgorithmthatlocatesthetargetbycheckingtheentire
I
array(data).Soitchecksandmatchestheentiredataonebyone
ifitismatchedwiththetargetandwillreturnthetargetdata.
twillstopsearchingfortwocaseseitherifitfindsatargetorif
I
thetargetelementisnotinthegivendataset.
Case2Policechasingthetheft
omecrimeiscommittedinyourvillagebutsecuritycameras
S
havecapturedcriminals'faces.Sothepolicearesearchingfor
thesecriminalsbycheckingeveryone'sfacewholivesinahood
withsecurityfootage.
fteranintensive2-hoursearchtheyfoundthepersonthatwas
A
exactlymatchedwithaphotothatwascapturedbythesecurity
cameraandthepolicetookhimtoprison.Seethisisexactlyhow
linearsearchalgorithmsworktosearchspecificdatafroma
collectionofdata.
tseemsagoodsearchalgorithmbecauseeachitemofdatais
I
😄
accessed.Itislikehowwefindourlostmoney .Wetendto
checkitineveryclothespocketevenifwedidn'twearthose
clothesonthedayourmoneywaslost.
talsohasahorribledisadvantagewhichistime.Asinputsizeor
I
datasizeisincreasedthetimetofindthatdatagrowsfastertoo.
Soittakesmoretimetosearch.
26
Datastructureandalgorithm101 Bywondmenehfekadu
ntheabovecaseifalotofpeopleliveinagivenvillageittakes
I
quitealotoftimetoidentifyandcatchthetheft.
Linearsearchalgorithm
nt
i data[
7
]
=
{
1
,
4
,
2
,
5
,
6
,
7
,
5
}
//
the
below
code
used
to
get
the
size
of
array
int
n=
sizeof
(arr)/
sizeof
(arr[
0
]);
int
found
=
0
;
//
this
sets
found
state
to
false
int
target
=
4
;
//
this
is
our
target
//
the
below
for
loop
will
help
to
loop
through
all
elements
for
(
int
i=
1
;
i<n;i++)
{
if
(arr[i]==key)
//
if
target
element
is
found
found=
1
;
//
found
status
will
be
found
}
return
found;
It’scomplexityanalysis
estcaseisifthetargetelementisatthestart.
B (1)
O
Averagecaseifthetargetelementisinthemiddle. O(n)
Worstcaseisifthetargetelementisattheend. O(n)
Anditstimecomplexityisbig-Oofn.O(n)
27
Datastructureandalgorithm101 Bywondmenehfekadu
BinarySearch
I tworksinabitdifferentwaywhenitiscomparedwithalinear
searchalgorithm.Itworksonlyforsorteddata.
Pleasecarefullyreadthis.
I tworksbydividingagivenlistinthemiddleandwegetthemiddle
element.Anditmeanswehavetwolistsnow.
. L
1 istwhichconsistsoflowerthanmiddleelement
2. Listwhichconsistsofgreaterthanthemiddleelement
Nowitwillcheck3things.
. I fthetargetdataisinthemiddle.Ifitis,itwillreturnit.
1
2. Ifthetargetdataisinthelistitislowerthanthemiddle
element. Itwillcompletelyignorethelistwhichconsists
ofgreaterthanmiddleelement.Becauseitwillbe
wasteoftime.ThereisalsoanAmharicquote that
goeslikethis.(ላምባልዋለበትኩበትለቀማ)
3. Itwillalsodothesamethingasnumber2ifthetarget
dataisgreaterthanamiddleelement.
I twillperformtheaboveoperationoverandover.Dividing
anddividingalistuntilitgetsthetargetdata.
Forthisreason,wecanonlyapplybinarysearch.
28
Datastructureandalgorithm101 Bywondmenehfekadu
Binarysearchalgorithm
int
binarySearch
(arr:
array
of
int
,
target:
int
)
left
=
0
right
=
length(arr)
-
1
while
left
<=
right
mid
=
(left
+
right)
/
2
if
arr[mid]
==
target
return
mid
else
if
arr[mid]
<
target
left
=
mid
+
1
else
right
=
mid
-
1
return
-1
//
Target
not
found
29
Datastructureandalgorithm101 Bywondmenehfekadu
It’scomplexityanalysis
estcase-ifthetargetisatthemiddle.O(1)
B
Worstcase-ifthetargetisatthefirstoratthelast.O(logn)
Averagecase-itissomewhereinthelist.O(logn)
veralltimecomplexityisO(logn).
O
Itwasallthebasicsaboutsimplesearchingalgorithms
Simplesortingalgorithms
ortingalgorithmisanalgorithmwhichusedtosortalist.Doesit
S
soundcircular?Yesitis!Ithelpustoorderlistsinincreasingor
decreasingform.
herearetwotypesofsorting.Pre-sorted&Post-sortedsorting.
T
Pre-sortedisinascendingorder,andpost-sortedisdesending
order.
Thereare3typesofsortingalgorithms.
1. Bubblesort
2. Selectionsort
3. Insertionsort
1.Bubblesort
I tbasicallycomparestwoadjacentlist’selementsanditwillswap
thesmallerelementtotheleftandthehighertoright.Itwilldothe
samethinguntiltheendofalist.Oncedone.Itwilldoitagainand
againuntilallitemsaresorted.
30
Datastructureandalgorithm101 Bywondmenehfekadu
I tisveryusefulforalistthatisalreadysortedorjustneedsalittle
modification.
Itisveryslow.ItstimecomplexityisO(n^2).
2.Selectionsort
syouknowifwearesortingwehavetoprepareanarrayof
A
elements.Andhereiswhatthisalgorithmwilldotothearrayin
ordertosortit.
I tsplitsthegivenarrayintotwoparts.Sortedandunsortedparts.
Itfindsthesmallestelementfromunsortedandputstotheendof
thesortedlist.Thisprocesswillberepeatedoverandoveruntilall
elementsaresorted.
ItisalsoslowwiththetimecomplexityofO(n^2).
3.Insertionsort
hisalgorithmiskindofbestalgorithmbecauseitsortsthegiven
T
arrayoneatatime.Thismeansitpicksoneelementandputsitin
itsproperplaceasasortedlist.
Casesortingdeckofcards
et’ssayyouhaveunsortedpiecesofcardinyourhandandyou
L
wanttosortthem.Butalsosinceitissmallyoudon’ttowasteyour
timebyswappingeachelementorbyputtingordereditemsatone
place.Soyousorteditinyourhandbyseeingthe numberand
puttingitinitsproperplace.
31
Datastructureandalgorithm101 Bywondmenehfekadu
heaboveishowyouuseinsertionsortpractically.Anditworks
T
efficientlyforsmall-sizedata.Ifdatagetslargeritconfuses.
hisisallaboutthebasicsofsimplealgorithmsandpleasereferto
T
othermaterialstootogetthebestoutofthisbook.
GoodLuck
32
Datastructureandalgorithm101 Bywondmenehfekadu
AboutMe
I amWondmenehFekadu,adedicated2nd-yearstudentatAdama
ScienceandTechnologyUniversity.Myacademicjourneyisnotjusta
pursuitofknowledge;itisapassionateexplorationoftheintricate
worldoftechnology.
I 'mallupforconnecting.Whetheryouneedhelpwithtechstuff,want
toteamuponprojects,orjustfancyachat,hitmeup.
I'dlovetohearyourfeedback,sodon'thesitatetoreachoutwith
thoughtsorsuggestions.Connectingwithyouwouldbringasmileto
myface!
ContactInformation:
📞
Phone:0984559233
📧
Email:[email protected]
🌐
Telegram:t.me/wondifekadu
33