0% found this document useful (0 votes)
85 views36 pages

Dsa 101

Uploaded by

Wondmeneh Fekadu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views36 pages

Dsa 101

Uploaded by

Wondmeneh Fekadu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

‭Concept‬‭understanding‬‭approach‬

‭DSA‬‭101‬
‭A‬‭Concise‬‭Introduction‬‭to‬‭Data‬
‭Structure‬‭and‬‭Algorithm‬

‭Prepared‬‭by‬‭Wondmeneh‬‭Fekadu‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭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‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭Words‬‭from‬‭the‬‭Me‬

‭ o‬‭make‬‭the‬‭most‬‭of‬‭this‬‭book,‬‭here‬‭are‬‭a‬‭few‬‭things‬‭to‬‭keep‬
T
‭in‬ ‭mind.‬ ‭The‬ ‭book‬ ‭is‬ ‭designed‬ ‭to‬ ‭help‬ ‭you‬ ‭understand‬ ‭the‬
‭basic‬ ‭concepts‬ ‭of‬ ‭data‬‭structures‬‭and‬‭algorithms.‬‭I‬‭suggest‬
‭reading‬ ‭it‬ ‭before‬ ‭your‬ ‭DSA‬ ‭lecture‬‭class‬‭or‬‭before‬‭you‬‭start‬
‭using‬‭the‬‭study‬‭materials‬‭from‬‭your‬‭school.‬

‭ ost‬‭importantly‬‭don't‬‭forget‬‭to‬‭share‬‭what‬‭you‬‭know‬‭with‬
M
‭your‬‭classmates‬‭or‬‭someone‬‭who‬‭needs‬‭your‬‭help‬‭for‬‭his‬‭/‬
‭her‬‭educational‬‭success.‬

‭Happy‬‭reading‬‭!!!‬

‭Good‬‭Luck‬
Unit‬‭
‭ 1‬
Introduction‬‭
‭ to‬‭
Data‬‭
Structure‬‭
and‬‭
Algorithm‬

‭ ata‬‭structure‬‭and‬‭algorithms‬‭serve‬‭as‬‭the‬‭main‬‭pillars‬‭of‬‭the‬
D
‭computer‬‭realm.‬‭The‬‭data‬‭structure‬‭is‬‭the‬‭storage‬‭and‬‭organization‬
‭of‬‭data,‬‭whereas‬‭algorithms‬‭are‬‭step-by-step‬‭instructions‬‭for‬
‭solving‬‭a‬‭computing‬‭problem.‬

‭ ata‬‭structure‬‭and‬‭algorithm‬‭are‬‭also‬‭the‬‭core‬‭concepts‬‭of‬
D
‭computer‬‭science,‬‭software‬‭engineering,‬‭and‬‭related‬‭fields‬‭since‬
‭they‬‭play‬‭a‬‭significant‬‭role‬‭when‬‭we‬‭are‬‭working‬‭with‬‭time‬‭and‬
‭space‬‭complexity.‬

‭ on’t‬‭worry‬‭if‬‭you‬‭don’t‬‭know‬‭the‬‭terms‬‭above‬‭as‬‭we‬‭will‬‭go‬‭through‬
D
‭each‬‭of‬‭them.‬

‭1.1‬‭Data‬‭structure‬

‭ he‬‭data‬‭structure‬‭mainly‬‭focuses‬‭on‬‭a‬‭way‬‭of‬‭organizing,‬
T
‭collecting,‬‭arranging,‬‭and‬‭ordering‬‭data‬‭that‬‭comes‬‭from‬‭various‬
‭sources‬‭(The‬‭data‬‭may‬‭be‬‭generated‬‭from‬‭applications‬‭we‬‭use‬‭day‬‭to‬
‭day‬‭to‬‭make‬‭our‬‭life‬‭easier.)‬‭.‬ ‭So‬‭that‬‭time‬‭and‬‭memory‬‭complexity‬
‭will‬‭be‬‭efficient.‬

I‭ n‬‭other‬‭words,‬‭if‬‭data‬‭is‬‭put‬‭in‬‭an‬‭organized‬‭way‬‭space‬‭it‬‭takes‬‭to‬
‭store‬‭it‬‭will‬‭be‬‭very‬‭small‬‭and‬‭we‬‭can‬‭store‬‭a‬‭lot‬‭of‬‭data‬‭using‬‭a‬
‭small‬‭space.‬‭Also,‬‭the‬‭time‬‭it‬‭takes‬‭to‬‭access‬‭data‬‭will‬‭be‬‭short‬
‭because‬‭we‬‭have‬‭to‬‭make‬‭it‬‭very‬‭easy‬‭to‬‭access‬‭and‬‭our‬‭program‬
‭will‬‭not‬‭spend‬‭unnecessary‬‭time‬‭searching‬‭it‬‭from‬‭large‬‭and‬‭messed‬
‭up‬‭data‬‭sets.‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭The‬‭following‬‭case‬‭will‬‭make‬‭the‬‭explanation‬‭clearer.‬

‭Case‬‭1‬‭Solomon's‬‭clothes‬

‭ olomon‬‭is‬‭too‬‭lazy‬‭to‬‭organize‬‭his‬‭clothes‬‭as‬‭most‬‭of‬‭us‬‭do.‬‭His‬
S
‭clothes‬‭are‬‭here‬‭and‬‭there‬‭all‬‭over‬‭the‬‭place.‬‭Some‬‭are‬‭on‬‭the‬‭chair,‬
‭others‬‭are‬‭on‬‭the‬‭bed,‬‭a‬‭few‬‭are‬‭on‬‭the‬‭sofa‬‭and‬‭even‬‭some‬‭are‬‭lost.‬

‭ eing‬‭in‬‭this‬‭situation‬‭look‬‭how‬‭much‬‭space‬‭it‬‭took‬‭to‬‭put‬‭his‬
B
‭clothes,‬‭if‬‭it‬‭was‬‭organized‬‭I‬‭bet‬‭it‬‭wouldn’t‬‭be‬‭more‬‭than‬‭one‬
‭clothes‬‭bag.‬‭The‬‭same‬‭analogy‬‭works‬‭for‬‭data‬‭if‬‭it‬‭is‬‭not‬‭organized‬
‭it‬‭will‬‭take‬‭more‬‭space‬‭than‬‭it‬‭should‬‭take.‬

‭ emme‬‭add‬‭one‬‭question‬‭for‬‭you‬‭too.‬‭How‬‭fast‬‭would‬‭Solomon‬‭take‬
L
‭to‬‭find‬‭his‬‭red‬‭T-shirt,‬‭grey‬‭trousers,‬‭and‬‭yellow‬‭jacket?‬‭I‬‭am‬‭sure‬‭if‬
‭it‬‭is‬‭not‬‭at‬‭the‬‭front‬‭by‬‭luck‬‭it‬‭takes‬‭him‬‭a‬‭lot‬‭of‬‭time‬‭and‬‭tiring‬‭that‬
‭makes‬‭him‬‭decide‬‭to‬‭wear‬‭random‬‭clothes‬‭that‬‭appear‬‭at‬‭the‬‭front.‬
‭The‬‭same‬‭analogy‬‭goes‬‭for‬‭time‬‭data‬‭if‬‭it‬‭is‬‭not‬‭organized‬‭properly‬
‭it‬‭tries‬‭to‬‭check‬‭the‬‭entire‬‭files‬‭one‬‭by‬‭one.‬

I‭ ‬ ‭don’t‬ ‭know‬ ‭if‬ ‭you‬ ‭recognize‬‭but‬‭when‬‭you‬‭search‬


‭specific‬ ‭files‬ ‭from‬ ‭the‬ ‭entire‬ ‭window‬ ‭it‬ ‭takes‬ ‭a‬ ‭bit‬
‭longer.‬‭But‬‭if‬‭it‬‭is‬‭well‬‭organized‬‭you‬‭just‬‭click‬‭a‬‭few‬
‭buttons,‬‭and‬‭you‬‭get‬‭what‬‭you‬‭want‬‭in‬‭no‬‭time.‬

‭ ut‬‭imagine‬‭if‬‭his‬‭clothes‬‭were‬‭organized‬‭well‬‭by‬‭categories,‬‭all‬
B
‭T-shirts‬‭on‬‭one‬‭side,‬‭all‬‭trousers‬‭on‬‭one‬‭side,‬‭all‬‭shorts‬‭on‬‭one‬‭side,‬
‭all‬‭jackets‬‭on‬‭one‬‭side,‬‭it‬‭would‬‭take‬‭a‬‭small‬‭space‬‭and‬‭Solomon‬
‭would‬‭never‬‭be‬‭tired‬‭to‬‭find‬‭his‬‭favorite‬‭clothes‬‭and‬‭never‬‭be‬
‭disappointed.‬

‭2‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭1.2‬‭Abstract‬‭Data‬‭type‬

‭ o‬‭understand‬‭the‬‭concepts‬‭of‬‭ADT‬‭let’s‬‭expand‬‭the‬‭terms‬‭one‬‭by‬
T
‭one.‬

‭ bstract‬‭:‬‭in‬‭programming,‬‭the‬‭abstract‬‭hides‬‭how‬‭the‬‭system‬‭is‬
A
‭working,‬‭and‬‭just‬‭it‬‭gets‬‭the‬‭work‬‭done.‬‭It‬‭means‬‭we‬‭don’t‬‭know‬
‭how‬‭it‬‭does‬‭it‬‭but‬‭we‬‭see‬‭what‬‭it‬‭does.‬

‭Case‬‭2:‬‭The‬‭Coca-Cola‬‭drink‬

‭ lmost‬‭everyone‬‭on‬‭this‬‭planet‬‭has‬‭drunk‬‭or‬‭at‬‭least‬‭heard‬‭the‬
A
‭name‬‭Coca-Cola‬‭soft‬‭drink.‬‭But‬‭no‬‭one‬‭knows‬‭the‬‭formula‬‭behind‬
‭Coca-Cola's‬‭soft‬‭drink.‬‭That‬‭is‬‭what‬‭abstract‬‭is.‬

‭Data‬‭:‬‭it‬‭is‬‭a‬‭piece‬‭of‬‭information‬‭we‬‭use‬‭in‬‭programming.‬

‭Type‬‭:‬‭They‬‭are‬‭strings,‬‭lists,‬‭integers,‬‭stacks,‬‭queues…‬

‭ o‬‭ADT‬‭is‬‭a‬‭set‬‭of‬‭rules‬‭for‬‭organizing‬‭data‬‭and‬‭working‬‭with‬‭data‬
S
‭in‬‭programming.‬‭So‬‭by‬‭saying‬‭this,‬‭we‬‭understand‬‭that‬‭it‬‭is‬
‭independent‬‭of‬‭programming‬‭language‬‭it‬‭is‬‭just‬‭a‬‭formal‬
‭description‬‭not‬‭code.‬

‭ e‬‭can‬‭do‬‭programming‬‭operations‬‭within‬‭data‬‭in‬‭ADT.‬‭like‬‭adding,‬
W
‭and‬‭subtracting‬‭….‬

‭ e‬‭need‬‭to‬‭decide‬‭which‬‭data‬‭structure‬‭to‬‭use‬‭when‬‭we‬‭create‬‭ADT.‬
W
‭We‬‭can‬‭use‬‭Linked‬‭lists,‬‭arrays,‬‭or‬‭stacks‬‭…..‬‭To‬‭create‬‭ADT.‬‭This‬
‭decision‬‭is‬‭up‬‭to‬‭you‬‭which‬‭you‬‭think‬‭is‬‭the‬‭best‬‭choice‬‭for‬‭your‬
‭project‬‭or‬‭problem.‬

‭3‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭1.3‬‭Classification‬‭of‬‭data‬‭structure‬

I‭ n‬‭the‬‭data‬‭structure,‬‭we‬‭use‬‭more‬‭complex‬‭and‬‭sophisticated‬‭data‬
‭types‬‭rather‬‭than‬‭simple‬‭data‬‭types‬‭like‬‭string‬‭and‬‭int‬‭...‬‭Now‬
‭stacks,‬‭linked‬‭lists‬‭…‬‭are‬‭added.‬‭We‬‭will‬‭discuss‬‭them‬‭in‬‭detail‬‭as‬‭we‬
‭move‬‭on.‬

‭ hey‬‭are‬‭mainly‬‭divided‬‭into‬‭two‬‭broad‬‭categories.‬‭Primitive‬‭and‬
T
‭Non‬‭primitives.‬

‭ rimitives‬‭:‬‭those‬‭are‬‭types‬‭in‬‭which‬‭we‬‭can‬‭directly‬‭interact‬‭and‬
P
‭manipulate‬‭the‬‭data‬‭using‬‭programming.‬‭No‬‭need‬‭to‬‭use‬‭the‬
‭indexing‬‭method‬‭or‬‭other‬‭complex‬‭ways‬‭to‬‭access‬‭it.‬‭They‬‭are‬
‭integers,‬‭floating‬‭point‬‭numbers,‬‭characters,‬‭string‬‭constants,‬‭and‬
‭pointers‬‭…‬‭and‬‭they‬‭are‬‭basic‬‭components‬‭of‬‭non-primitives.‬
‭Meaning‬‭non-primitives‬‭are‬‭a‬‭collection‬‭of‬‭primitives.‬‭Our‬‭focus‬‭is‬
‭not‬‭on‬‭primitives.‬

‭ on-primitives:‬‭They‬‭can‬‭be‬‭a‬‭group‬‭of‬‭the‬‭same‬‭data‬‭types‬‭like‬
N
‭arrays‬‭or‬‭different‬‭data‬‭types‬‭like‬‭structures.‬‭Simply‬‭data‬‭type‬
‭which‬‭is‬‭created‬‭from‬‭another‬‭data‬‭type.‬

‭Array,‬‭lists,‬‭files,‬‭linked‬‭lists,‬‭trees,‬‭and‬‭graphs‬‭are‬‭good‬‭examples.‬

‭ on-primitives‬‭are‬‭again‬‭divided‬‭into‬‭two‬‭Linear‬‭lists‬‭and‬
N
‭Non-linear‬‭lists.‬‭We‬‭will‬‭discuss‬‭each‬‭data‬‭structure‬‭one‬‭by‬‭one‬
‭since‬‭they‬‭have‬‭their‬‭chapter.‬

‭4‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭Case‬‭3‬‭The‬‭foods‬

‭ s‬‭we‬‭know‬‭we‬‭can‬‭eat‬‭food‬‭mainly‬‭in‬‭two‬‭forms‬‭one‬‭on‬‭its‬‭own‬
A
‭without‬‭banana,‬‭orange,‬‭and‬‭mango‬‭we‬‭just‬‭peel‬‭and‬‭eat‬‭them.‬
‭The‬‭same‬‭analogy‬‭to‬‭primitives‬‭we‬‭just‬‭use‬‭them‬‭on‬‭their‬‭own.‬

‭ ther‬‭forms‬‭of‬‭food‬‭are‬‭those‬‭which‬‭require‬‭cooking,‬‭as‬‭you‬‭know‬‭if‬
O
‭we‬‭are‬‭cooking‬‭we‬‭mostly‬‭mix‬‭some‬‭ingredients.‬‭So‬‭we‬‭have‬‭to‬‭use‬
‭a‬‭collection‬‭of‬‭foods‬‭to‬‭create‬‭a‬‭food.‬

‭ he‬‭same‬‭thing‬‭goes‬‭for‬‭non-primitives‬‭we‬‭use‬‭a‬‭group‬‭of‬‭data‬
T
‭types‬‭to‬‭create‬‭data‬‭type.‬

‭ ut‬‭here‬‭is‬‭a‬‭catch‬‭both‬‭of‬‭them‬‭are‬‭useful,‬‭but‬‭we‬‭need‬‭to‬‭be‬
B
‭careful‬‭while‬‭choosing‬‭them.‬‭We‬‭can‬‭eat‬‭bananas‬‭or‬‭‘‭D ‬ oro‬‭wot’‬ ‭our‬
‭entire‬‭life‬‭and‬‭manage‬‭to‬‭live,‬‭but‬‭since‬‭their‬‭nutrient‬‭content‬‭is‬
‭different‬‭it‬‭affects‬‭us‬‭in‬‭the‬‭long‬‭run.‬

‭ e‬‭can‬‭use‬‭primitives‬‭for‬‭a‬‭complex‬‭project‬‭and‬‭it‬‭still‬‭gets‬‭the‬
W
‭work‬‭done‬‭but‬‭in‬‭the‬‭long‬‭run,‬‭it‬‭will‬‭affect‬‭our‬‭program‬‭time‬‭and‬
‭space‬‭complexity.‬

I‭ n‬‭addition,‬‭if‬‭there‬‭is‬‭a‬‭holiday‬‭what‬‭do‬‭we‬‭prepare‬‭a‬‭kilos‬‭of‬
‭mangoes‬‭or‬‭a‬‭delicious‬‭‘Doro‬‭wot’‬‭which‬‭is‬‭a‬‭mixing‬‭of‬‭different‬
‭ingredients?‬‭The‬‭answer‬‭is‬‭obvious‬‭‘Doro‬‭wot’‬‭.‬

‭ o‬‭if‬‭a‬‭program‬‭is‬‭simple‬‭and‬‭doesn’t‬‭need‬‭a‬‭complexity‬‭use‬
S
‭primitive.‬‭But‬‭if‬‭it‬‭is‬‭quite‬‭large,‬‭complex,‬‭and‬‭sophisticated‬‭go‬‭for‬
‭non-primitives‬‭as‬‭it‬‭makes‬‭our‬‭life‬‭easier‬‭(‭O‬ nly‬‭for‬‭programming‬
😄 ‭ ‬‭).‬

‭5‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭ e‬‭will‬‭discuss‬‭more‬‭in‬‭detail‬‭how‬‭non-primitives‬‭make‬
W
‭programming‬‭efficient‬‭later‬‭in‬‭this‬‭book.‬

‭1.4‬‭Array‬‭revision‬

‭ n‬‭array‬‭is‬‭a‬‭data‬‭type‬‭that‬‭is‬‭used‬‭to‬‭store‬‭the‬‭same‬‭data‬‭types‬‭in‬
A
‭one‬‭variable.‬
‭We‬‭declare‬‭the‬‭array‬‭in‬‭the‬‭following‬‭form;‬

‭datatype‬‭name[size];‬

‭nt‬‭
i price[‬ 5‭
‭]
‬‬‭=‬‭
{‬1‬
‭ ,‬
‭2‭
‭,
‬‬ 3‭
‭,
‬‬4‭
‭,
‬‬
5‭
‭}
‬‬
‭In‬‭C++‬‭we‬‭have‬‭set‬‭the‬‭size‬‭of‬‭an‬‭array‬‭at‬‭declaration,‬‭we‬‭limit‬‭the‬
‭size‬‭only‬‭to‬‭a‬‭specified‬‭size‬‭to‬‭save‬‭unnecessary‬‭memory‬‭usage,‬
‭even‬‭though‬‭we‬‭can‬‭set‬‭it‬‭dynamic‬‭and‬‭its‬‭size‬‭will‬‭increase‬‭as‬‭the‬
‭items‬‭inside‬‭increase.‬

‭Case‬‭4‬‭The‬‭wedding‬‭attendant‬

‭ ikiyas’s‬‭wedding‬‭is‬‭soon,‬‭and‬‭he‬‭is‬‭planning‬‭to‬‭invite‬‭his‬‭friends,‬
M
‭relatives,‬‭co-workers,‬ ‭and‬‭people‬‭he‬‭knows.‬‭He‬‭thinks‬‭of‬‭two‬‭ways‬
‭to‬‭do‬‭it.‬

‭1.‬ T ‭ o‬‭name‬‭people‬‭he‬‭knows,‬‭count‬‭them‬‭and‬‭the‬‭resource‬
‭will‬‭be‬‭prepared‬‭based‬‭on‬‭the‬‭number‬‭of‬‭people‬‭to‬‭be‬
‭invited.‬
‭2.‬ ‭To‬‭prepare‬‭the‬‭resource‬‭at‬‭the‬‭excess‬‭amount‬‭people‬
‭will‬‭use‬‭as‬‭much‬‭as‬‭they‬‭want‬‭and‬‭the‬‭door‬‭will‬‭be‬‭open‬
‭to‬‭anyone.‬
‭Which‬‭one‬‭of‬‭the‬‭above‬‭you‬‭would‬‭do‬‭if‬‭you‬‭were‬‭Mikiyas?‬
😄
‭(Be‬‭honest‬‭don’t‬‭try‬‭to‬‭be‬‭a‬‭nice‬‭person‬‭ )‬

‭6‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭ f‬‭course‬‭the‬‭first‬‭choice.‬‭And‬‭here‬‭is‬‭why‬‭you‬‭do‬‭this.‬
O
‭Because‬‭if‬‭you‬‭don’t‬‭limit‬‭the‬‭number‬‭of‬‭people‬‭you‬‭invite,‬
‭there‬‭will‬‭be‬‭extravagancy‬‭so‬‭you‬‭might‬‭go‬‭bankrupt.‬

‭ or‬‭the‬‭same‬‭reason‬‭to‬‭use‬‭memory‬‭efficiently‬
F
‭limiting‬‭the‬‭size‬‭of‬‭the‬‭array‬‭is‬‭quite‬‭important.‬

‭1.5‬‭Pointer‬‭revision‬

‭ ‬‭pointer‬‭is‬‭a‬‭variable‬‭that‬‭stores‬‭the‬‭address‬‭of‬‭another‬‭variable.‬
A
‭We‬‭declare‬‭it‬‭the‬‭following‬‭way.‬

‭nt‬‭
i ptr‬
,‬‭
‭ var‬
;‬

var‬‭
‭ =‬‭
value;‬
ptr‬‭
‭ =‬‭
&‬‭
var‬
;‬

‭Case‬‭5‬‭where‬‭is‬‭the‬‭police‬‭station?‬

‭ omeone‬‭stranger‬‭who‬‭is‬‭new‬‭to‬‭your‬‭village‬‭suddenly‬‭appears‬‭and‬
S
‭asks‬‭you‬‭where‬‭is‬‭the‬‭police‬‭station.‬‭And‬‭you‬‭tell‬‭him‬‭it‬‭is‬‭100m‬‭in‬
‭the‬‭east‬‭direction.‬

‭ ou‬‭know‬‭what‬‭just‬‭happened‬‭here?‬‭The‬‭police‬‭station‬‭address‬‭is‬
Y
‭stored‬‭in‬‭you‬‭and‬‭you‬‭just‬‭point‬‭at‬‭it.‬‭That‬‭is‬‭how‬‭a‬‭pointer‬‭works.‬

‭1.6‬‭Algorithm‬

‭ he‬‭algorithm‬‭is‬‭a‬‭step-by-step‬‭guide‬‭to‬‭follow‬‭to‬‭solve‬‭a‬‭given‬
T
‭problem.‬‭It‬‭is‬‭a‬‭guide‬‭a‬‭programmer‬‭has‬‭to‬‭follow‬‭to‬‭build‬‭a‬‭project‬
‭or‬‭solve‬‭a‬‭problem.‬

‭7‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭Case‬‭6‬‭Beso‬‭‘በሶ'‬‭making‬‭process‬

‭ s‬‭university‬‭students‬‭we‬‭know‬‭that‬‭‘በሶ’‬‭plays‬‭a‬‭crucial‬‭role‬‭in‬‭our‬
A
‭campus‬‭life,‬‭and‬‭we‬‭are‬‭familiar‬‭with‬‭the‬‭making‬‭process‬‭of‬‭it.‬‭But‬
‭lemme‬‭ask‬‭you‬‭a‬‭question‬‭before‬‭you‬‭know‬‭it.‬ ‭Someone‬‭has‬‭taught‬
‭you‬‭or‬‭you‬‭saw‬‭someone‬‭doing‬‭it.‬‭Right?‬ ‭What‬‭did‬‭you‬‭get‬‭taught‬
‭or‬‭what‬‭did‬‭you‬‭see‬‭when‬‭someone‬‭makes‬‭a‬‭'በሶ'?‬‭Here‬‭are‬‭the‬
‭things‬‭you‬‭see.‬

‭ .‬
1 ‭ tart‬‭the‬‭process‬
S
‭2.‬ ‭Prepare‬‭a‬‭plastic‬‭cup,‬‭flour,‬‭water,‬‭and‬‭sugar.‬
‭3.‬ ‭Put‬‭flour,‬‭water,‬‭and‬‭sugar‬‭in‬‭a‬‭plastic‬‭cup.‬
‭4.‬ ‭Shake‬‭it‬‭until‬‭they‬‭are‬‭completely‬‭mixed.‬
‭5.‬ ‭Test‬‭it.‬
‭6.‬ ‭If‬‭it's‬‭okay,‬‭finish.‬‭if‬‭not‬‭shake‬‭again.‬
‭7.‬ ‭End‬‭the‬‭process‬

‭ ou‬‭see‬‭the‬‭above‬‭instructions‬‭for‬‭making‬‭‘በሶ'.‬‭We‬‭call‬‭the‬‭same‬
Y
‭mechanism‬‭an‬‭algorithm‬‭for‬‭computational‬‭problems.‬

‭ n‬‭algorithm‬‭is‬‭not‬‭a‬‭code‬‭or‬‭it‬‭doesn’t‬‭solve‬‭a‬‭problem‬‭on‬‭its‬‭own.‬
A
‭It‬‭is‬‭just‬‭a‬‭logic‬‭of‬‭how‬‭the‬‭solution‬‭works.‬ ‭So‬‭programmers‬‭like‬‭you‬
‭will‬‭use‬‭it‬‭as‬‭a‬‭guide‬‭for‬‭coding‬‭solutions.‬

‭1.7‬‭Properties‬‭of‬‭the‬‭algorithm‬

‭ n‬‭algorithm‬‭has‬‭the‬‭properties‬‭that‬‭make‬‭it‬‭a‬‭good‬‭algorithm.‬‭Not‬
A
‭every‬‭procedure‬‭is‬‭an‬‭algorithm.‬‭An‬‭algorithm‬‭should‬‭have‬‭the‬
‭following‬‭properties.‬

‭We‬‭will‬‭use‬‭case‬‭6‬‭to‬‭demonstrate‬‭properties.‬

‭8‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭ .‬ ‭Input‬‭-‬‭it‬‭should‬‭take‬‭input.‬
1
‭We‬‭prepare‬‭flour,‬‭water,‬‭and‬‭sugar‬‭in‬‭a‬‭plastic‬‭cup.‬‭An‬
‭algorithm‬‭should‬‭have‬‭something‬‭to‬‭take‬‭from‬‭the‬‭user‬‭or‬
‭sensor.‬

‭2.‬ O
‭ utput‬‭-‬‭it‬‭should‬‭give‬‭output‬‭at‬‭the‬‭end‬‭of‬‭the‬
‭program.‬
‭The‬‭whole‬‭process‬‭is‬‭to‬‭drink‬‭a‬‭delicious‬‭‘በሶ'‬‭at‬‭the‬‭end.‬‭And‬
‭we‬‭managed‬‭to‬‭drink.‬

‭3.‬ I
‭ t‬‭Should‬‭be‬‭clear‬‭-‬‭it‬‭shouldn’t‬‭be‬‭ambiguous.‬‭The‬
‭instruction‬‭should‬‭be‬‭clear‬‭and‬‭simple‬‭to‬‭understand‬‭by‬
‭a‬‭programmer.‬
‭See‬‭the‬‭instructions‬‭they‬‭are‬‭easy,‬‭and‬‭even‬‭someone‬‭with‬‭no‬
‭experience‬‭can‬‭read‬‭it‬‭and‬‭make‬‭it‬‭by‬‭himself.‬

‭4.‬ T
‭ here‬‭must‬‭be‬‭an‬‭end‬‭-‬ ‭the‬‭algorithm‬‭must‬‭have‬‭a‬
‭point‬‭where‬‭it‬‭gets‬‭terminated.‬
‭If‬‭you‬‭recognize‬‭we‬‭stopped‬‭once‬‭we‬‭got‬‭our‬‭‘በሶ'.‬‭There‬‭is‬
‭nothing‬‭left‬‭to‬‭do‬‭after‬‭that.‬

‭5.‬ S
‭ hould‬‭be‬‭effective‬‭-‬‭an‬‭algorithm‬‭should‬‭consider‬‭the‬
‭available‬‭resources‬‭for‬‭making‬‭a‬‭solution.‬
‭Imagine‬‭on‬‭the‬‭instruction‬‭that‬‭it‬‭says‬‭to‬‭add‬‭2‬‭spoons‬‭of‬
‭cheese,‬‭100ml‬‭of‬‭goat’s‬‭milk,‬‭and‬‭3‬‭spoons‬‭of‬‭butter.‬‭As‬‭a‬
‭student,‬‭you‬‭couldn’t‬‭get‬‭those‬‭things.‬‭So‬‭you‬‭can’t‬‭make‬
‭‘በሶ'.so‬‭instruction‬‭should‬‭consider‬‭what‬‭you‬‭can‬‭access‬‭in‬‭your‬
‭dorm.‬

‭9‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭6.‬ I
‭ t‬‭should‬‭be‬‭language-independent‬‭-‬‭an‬‭algorithm‬
‭should‬‭be‬‭able‬‭to‬‭be‬‭implemented‬‭in‬‭any‬‭language‬‭of‬
‭the‬‭programmer's‬‭choice.‬
‭1.8‬‭Expressing‬‭algorithms‬

‭ n‬‭algorithm‬‭can‬‭be‬‭expressed‬‭in‬‭different‬‭ways.‬‭Based‬‭on‬‭the‬
A
‭complexity‬‭of‬‭a‬‭problem‬‭or‬‭the‬‭preference‬‭of‬‭a‬‭person‬‭who‬
‭prepares‬‭it.‬

‭The‬‭main‬‭ways‬‭of‬‭expressing‬‭an‬‭algorithm‬‭are‬‭the‬‭following.‬
‭1.‬ ‭Natural‬‭language‬
‭2.‬ ‭Pseudocode‬
‭3.‬ ‭Flowchart‬

‭Let’s‬‭see‬‭them‬‭in‬‭detail.‬

‭Natural‬‭language‬

I‭ t‬‭is‬‭when‬‭you‬‭prepare‬‭an‬‭instruction‬‭using‬‭just‬‭plain‬‭language‬
‭using‬‭a‬‭sequence‬‭of‬‭actions.‬‭This‬‭means‬‭you‬‭just‬‭guide‬‭a‬
‭programmer‬‭to‬‭solve‬‭a‬‭problem‬‭like‬‭‘do‬‭this‬‭then‬‭do‬‭that‬‭after‬‭it‬
‭comes‬‭to‬‭do‬‭that‬‭and‬‭goes‬‭on‬‭…’‬

‭In‬‭the‬‭following‬‭example,‬‭we‬‭will‬‭illustrate‬‭the‬‭situation‬‭.‬

‭Case‬‭7‬‭The‬‭entrance‬‭answer‬‭sheet‬

‭ n‬‭the‬‭orientation‬‭university‬‭entrance‬‭national‬‭examination,‬‭we‬
O
‭were‬‭given‬‭the‬‭following‬‭instructions‬‭on‬‭answering‬‭the‬‭question.‬
‭Prepare‬‭a‬‭pencil,‬‭sharpener,‬‭and‬‭rubber.‬

‭10‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭ harpen‬‭your‬‭pencil,‬‭calm‬‭down,‬‭and‬‭blacken‬‭the‬‭answer‬‭on‬‭the‬
S
‭given‬‭answer‬‭sheet.‬‭If‬‭you‬‭think‬‭you‬‭made‬‭a‬‭mistake‬‭erase‬‭it‬‭by‬
‭your‬‭rubber‬‭and‬‭re-blacken.‬‭And‬‭give‬‭it‬‭to‬‭the‬‭examiner,‬‭when‬‭you‬
‭are‬‭done.‬‭This‬‭is‬‭when‬‭defined‬‭by‬‭natural‬‭language.‬

‭Flowchart‬

I‭ t‬‭is‬‭a‬‭way‬‭of‬‭expressing‬‭algorithms‬‭using‬‭diagrams.‬ ‭The‬‭main‬
‭components‬‭are:‬
‭1.‬ ‭Terminal‬‭-‬‭start‬‭and‬‭end.‬
‭2.‬ ‭Decision‬
‭3.‬ ‭Process‬
‭4.‬ ‭Input‬‭and‬‭output‬
‭Let’s‬‭see‬‭them‬‭in‬‭detail.‬

‭ erminal‬
T
‭It‬‭is‬‭an‬‭indication‬‭of‬‭where‬‭the‬‭execution‬‭starts‬‭and‬‭ends.‬

I‭ f‬‭you‬‭this‬‭symbol‬‭at‬‭the‬‭beginning‬‭the‬‭execution‬‭starts‬‭there‬‭and‬‭if‬
‭you‬‭see‬‭it‬‭at‬‭the‬‭end‬‭it‬‭means‬‭the‬‭execution‬‭ends‬‭there.‬

‭ ecision‬
D
‭It‬‭is‬‭an‬‭indication‬‭of‬‭a‬‭conditional‬‭statement‬‭that‬‭executes‬‭certain‬
‭statements‬‭based‬‭on‬‭certain‬‭conditions.‬‭Simply‬‭we‬‭can‬‭refer‬‭to‬‭it‬‭as‬
‭if‬‭statement.‬

‭11‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

I‭ f‬‭you‬‭see‬‭this‬‭symbol‬‭you‬‭can‬‭be‬‭sure‬‭that‬‭there‬‭is‬‭an‬‭execution‬
‭that‬‭is‬‭based‬‭on‬‭some‬‭conditions.‬

‭ rocess‬
P
‭It‬‭indicates‬‭a‬‭process‬‭like‬‭addition,‬‭subtraction,‬‭or‬‭fetching‬‭data‬
‭from‬‭databases…‬‭but‬‭just‬‭know‬‭that‬‭there‬‭is‬‭some‬‭process‬‭being‬
‭carried‬‭on.‬

‭ nput-output‬
I
‭It‬‭indicates‬‭there‬‭is‬‭something‬‭that‬‭the‬‭program‬‭takes‬‭from‬‭the‬
‭user‬‭or‬‭sensor‬‭and‬‭there‬‭is‬‭something‬‭we‬‭get‬‭from‬‭the‬‭program‬‭as‬
‭final‬‭output.‬

‭Feel‬‭confused?‬‭Let’s‬‭see‬‭them‬‭in‬‭use.‬‭In‬‭the‬‭next‬‭section.‬

‭12‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭Flowchart‬‭structures‬

‭There‬‭are‬‭4‬‭types‬‭of‬‭flowchart‬‭structures.‬‭And‬‭here‬‭they‬‭go.‬

‭ .‬
1 ‭ equence‬
S
‭2.‬ ‭Decision‬
‭3.‬ ‭Repetition‬
‭4.‬ ‭Case‬

‭ et’s‬‭see‬‭them‬‭in‬‭detail.‬
L
‭Sequence‬‭-‬‭the‬‭series‬‭of‬‭actions‬‭guided‬‭by‬‭instructions.‬
‭Example‬‭-‬‭Go‬‭to‬‭bookshelf‬‭→‬‭Locate‬‭the‬‭book→‬‭Pickup‬‭the‬‭book‬

‭Credit‬‭-‬‭https://www.yaclass.in/‬

‭ ecision‬‭-‬‭depending‬‭on‬‭the‬‭condition‬‭one‬‭of‬‭statements‬‭will‬‭be‬
D
‭executed.‬

‭ xample:‬ ‭If‬‭the‬‭decision‬‭is‬‭Yes,‬‭step1‬‭will‬‭be‬‭executed‬‭and‬‭if‬‭it‬‭is‬
E
‭No‬‭step2‬‭then‬‭step3‬‭will‬‭be‬‭executed.‬

‭13‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭Credit‬‭-‬‭https://www.gleek.io/‬

‭ epetition‬‭-‬‭the‬‭execution‬‭will‬‭be‬‭repeated‬‭over‬‭and‬‭over‬‭based‬‭on‬
R
‭some‬‭condition.‬‭There‬‭are‬‭two‬‭types‬‭of‬‭repetition.‬‭Pre-Test‬‭and‬
‭Post-Test.‬

‭ re-test‬‭-‬‭Condition‬‭must‬‭be‬‭satisfied‬‭to‬‭be‬‭executed.‬‭Examples‬‭are‬
P
‭for-loop‬‭and‬‭while-loop.‬

‭Credit‬‭-‬‭https://www.edrawmax.com/‬

‭14‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭ ost-test‬‭repetition‬ ‭-‬‭is‬‭a‬‭kind‬‭of‬‭repetition‬‭that‬‭checks‬‭conditions‬
P
‭after‬‭the‬‭first‬‭iteration.‬‭For‬‭iteration‬‭that‬‭comes‬‭after‬‭condition‬
‭assessment‬‭will‬‭be‬‭carried.‬

‭Case‬‭8‬‭Soccer‬‭tournament‬

I‭ f‬‭we‬‭say‬‭soccer‬‭league‬‭a‬‭number‬‭of‬‭teams‬‭participate‬‭in‬‭the‬‭game‬
‭but‬‭only‬‭one‬‭team‬‭lifts‬‭the‬‭cup.‬‭If‬‭we‬‭see‬‭it‬‭deeply‬‭the‬‭tournament‬
‭organizer‬‭uses‬‭the‬‭same‬‭technique‬‭as‬‭post-test.‬‭They‬‭let‬‭teams‬‭play‬
‭at‬‭least‬‭one‬‭time‬‭and‬‭their‬‭next‬‭match‬‭will‬‭depend‬‭on‬‭their‬
‭performance.‬‭It‬‭means‬‭they‬‭may‬‭play‬‭or‬‭not‬‭next‬‭time.‬

‭ he‬‭bottom‬‭flowchart‬‭represents‬‭that‬‭it‬‭will‬‭print‬‭a‬‭as‬‭long‬‭as‬‭a‬‭is‬
T
‭less‬‭than‬‭a‬‭is‬‭less‬‭than‬‭or‬‭equal‬‭to‬‭5.‬

‭ redit‬‭-‬‭https://www.authorcode.com/‬
C
‭Case‬‭-‬‭for‬‭every‬‭condition‬‭that‬‭is‬‭considered‬‭to‬‭happen‬‭there‬‭is‬‭a‬
‭statement‬‭to‬‭be‬‭executed.‬‭It‬‭has‬‭also‬‭something‬‭called‬‭default‬
‭case‬‭which‬‭will‬‭be‬‭executed‬‭if‬‭the‬‭input‬‭is‬‭out‬‭of‬‭alternatives.‬

‭15‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭Case‬‭9‬‭Grade‬‭and‬‭its‬‭amount‬

I‭ f‬‭you‬‭get‬‭A‬‭its‬‭amount‬‭is‬‭4,‬‭if‬‭it’s‬‭B‬ ‭its‬‭amount‬‭is‬‭3‬‭and‬‭if‬‭C,‬‭it‬‭will‬
‭be‬‭2‬‭and‬‭if‬‭it‬‭is‬‭D‬‭it‬‭will‬‭be‬‭1,‬‭and‬‭finally,‬‭if‬‭it‬‭is‬‭F‬‭it‬‭will‬‭be‬‭0.‬‭If‬‭it‬
‭letter‬‭is‬‭not‬‭mentioned‬‭above‬‭it‬‭will‬‭be‬‭an‬‭invalid‬‭grade.‬

‭Credit‬‭-‬‭https://www.zenflowchart.com/‬

‭This‬‭is‬‭all‬‭about‬‭flowcharts‬‭at‬‭a‬‭basic‬‭level.‬

‭16‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭1.9‬‭Algorithm‬‭complexity‬‭analysis‬

‭ here‬‭might‬‭be‬‭a‬‭different‬‭approach‬‭to‬‭solving‬‭a‬‭given‬‭problem.‬
T
‭And‬‭we‬‭will‬‭have‬‭to‬‭use‬‭the‬‭efficient‬‭one.‬‭This‬‭means‬‭it‬‭is‬‭both‬‭time‬
‭and‬‭space-efficient.‬

‭ r‬‭an‬‭algorithm‬‭that‬‭executes‬‭faster‬‭and‬‭occupies‬‭a‬‭small‬‭space‬‭in‬
O
‭a‬‭computer‬‭because‬‭both‬‭time‬‭and‬‭space‬‭are‬‭precious‬‭in‬‭a‬
‭computer‬‭world.‬

‭ pace‬‭and‬‭time‬‭complexity‬‭are‬‭two‬‭core‬‭things‬‭to‬‭consider‬‭while‬
S
‭designing‬‭an‬‭algorithm.‬

‭ pace-complexity‬
S
‭It‬‭is‬‭how‬‭much‬‭memory‬‭is‬‭needed‬‭for‬‭executing‬‭an‬‭algorithm.‬‭So‬
‭the‬‭simpler‬‭algorithm‬‭the‬‭smaller‬‭space‬‭will‬‭be‬‭needed.‬
‭Memory‬‭refers‬‭to‬‭RAM‬‭(Random‬‭access‬‭memory).‬

‭ ase‬‭10‬‭Apps‬‭on‬‭your‬‭phone‬
C
‭Let’s‬‭look‬‭at‬‭your‬‭phone‬‭and‬‭I‬‭am‬‭sure‬‭you‬‭play‬‭mobile‬‭games.‬
‭There‬‭are‬‭several‬‭games‬‭which‬‭quite‬‭popular‬‭like‬‭PUBG,‬‭popstar,‬
‭Clash‬‭of‬‭Clans,‬‭Candy‬‭Crush,‬‭etc…‬

‭ or‬‭now,‬‭let’s‬‭pick‬‭Popstar‬‭and‬‭PUBG.‬‭lemme‬‭ask‬‭you‬‭a‬‭question,‬
F
‭from‬‭two‬‭of‬‭them‬‭which‬‭one‬‭of‬‭them‬‭requires‬‭high‬‭RAM‬‭to‬‭run?‬ ‭The‬
‭answer‬‭is‬‭obvious‬‭PUBG.‬‭because‬‭it‬‭has‬‭HD‬‭graphics,‬‭animation,‬
‭sound,‬‭etc…‬‭so‬‭all‬‭these‬‭resources‬‭take‬‭up‬‭quite‬‭a‬‭lot‬‭of‬‭space‬‭on‬‭a‬
‭phone‬‭that‬‭is‬‭why‬‭small‬‭quality‬‭phones‬‭get‬‭busy‬‭while‬‭you‬‭try‬‭to‬
‭such‬‭games‬‭using‬‭them.‬

‭17‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭Factors‬‭affecting‬‭Space-complexity‬

‭ hoice‬‭of‬‭data‬‭types‬‭-‬‭in‬‭basic‬‭C++‬‭we‬‭saw‬‭each‬‭size‬‭of‬‭memory‬‭of‬
C
‭each‬‭data‬‭type.‬‭So‬‭choosing‬‭them‬‭really‬‭plays‬‭a‬ ‭crucial‬‭role‬‭in‬
‭Space-complexity‬‭efficiency.‬

‭ sing‬‭recursive‬‭function‬‭in‬‭a‬‭program‬‭.‬‭They‬‭are‬‭a‬‭function‬‭that‬‭is‬
U
‭defined‬‭in‬‭terms‬‭of‬‭itself.‬‭And‬‭the‬‭more‬‭deep‬‭the‬‭recursive‬‭function‬
‭goes‬‭the‬‭more‬‭space‬‭will‬‭be‬‭used.‬

‭ ize‬‭of‬‭input‬‭-‬‭more‬‭input‬‭enters‬‭to‬‭program‬‭more‬‭space‬‭is‬‭used‬‭to‬
S
‭process‬‭it.‬
‭This‬‭is‬‭why‬‭your‬‭PC‬‭will‬‭be‬‭busy‬‭when‬‭you‬‭try‬‭to‬‭run‬‭a‬‭big‬
‭application‬‭on‬‭your‬‭computer.‬

‭ ariables‬‭-‬‭the‬‭number‬‭and‬‭size‬‭of‬‭variables‬‭used‬‭in‬‭a‬‭program‬
V
‭play‬‭significant‬‭problems.‬

‭ ime-complexity‬
T
‭In‬‭the‬‭computer‬‭world,‬‭the‬‭time-complexity‬‭is‬‭the‬‭time‬‭which‬‭is‬‭the‬
‭amount‬‭of‬‭time‬‭that‬‭takes‬‭to‬‭run‬‭an‬‭algorithm.‬‭It‬‭is‬‭like‬‭finding‬‭the‬
‭fastest‬‭way‬‭to‬‭solve‬‭a‬‭problem.‬

‭Case‬‭11‬‭Andnet’s‬‭journey‬‭to‬‭his‬‭home‬

‭ ndnet‬‭comes‬‭from‬‭Dire‬‭Dawa‬‭to‬‭Adama‬‭to‬‭learn‬‭at‬‭ASTU.‬‭after‬
A
‭finishing‬‭the‬‭final‬‭exam,‬‭he‬‭decided‬‭to‬‭take‬‭a‬‭break‬‭and‬‭relax,‬‭so‬‭he‬
‭plans‬‭to‬‭go‬‭to‬‭Dire‬‭Dawa.‬‭Now‬‭as‬‭you‬‭know‬‭there‬‭are‬‭a‬‭bunch‬‭of‬
‭ways‬‭to‬‭get‬‭there‬‭from‬‭Adama.‬

‭18‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭ he‬‭first‬‭way‬‭is‬‭to‬‭go‬‭to‬‭Addis‬‭Ababa‬‭and‬‭get‬ ‭Selam,‬‭Golden,‬‭or‬‭…‬
T
‭whatever‬‭transportation‬‭service.‬‭He‬‭can‬‭also‬‭get‬‭there‬‭by‬‭using‬‭the‬
‭route‬‭to‬‭Gonder‬‭City‬‭and‬‭going‬‭to‬‭Dire‬‭Dawa‬‭from‬‭Gondar.‬‭And‬
‭finally‬‭using‬‭train‬‭service‬‭from‬‭Adama‬‭city.‬

‭ ee‬‭by‬‭using‬‭all‬‭the‬‭above‬‭ways‬‭he‬‭can‬‭reach‬‭his‬‭final‬‭destination.‬
S
‭But‬‭which‬‭is‬‭efficient‬‭(we‬‭discussed‬‭efficiency‬‭in‬‭properties‬‭of‬‭the‬
‭algorithm)?‬‭So‬‭which‬‭one‬‭he‬‭should‬‭take‬ ‭to‬‭go‬‭to‬‭his‬‭home‬‭town?‬
‭Of‬‭course‬‭to‬‭get‬‭a‬‭train‬‭from‬‭here.‬‭Right?‬‭Because‬‭it‬‭is‬‭the‬‭shortest‬
‭and‬‭most‬‭reliable‬‭way.‬

‭ hoosing‬‭an‬‭algorithm‬‭that‬‭takes‬‭a‬‭short‬‭time‬‭to‬‭be‬‭executed‬‭is‬
C
‭crucial‬‭in‬‭efficient‬‭programming.‬

‭Factors‬‭affecting‬‭time-complexity‬

‭We‬‭will‬‭use‬‭the‬‭above‬‭case‬‭to‬‭demonstrate‬‭them.‬

‭An‬‭algorithm‬‭-‬‭the‬‭path‬‭that‬‭is‬‭taken.‬

‭ nput‬‭size‬‭-‬‭the‬‭more‬‭data‬‭taken‬‭into‬‭the‬‭program‬‭more‬‭time‬‭takes‬
I
‭to‬‭process‬‭them.‬‭For‬‭this‬‭let’s‬‭Mill‬‭House‬‭(ወፍጮ‬‭ቤት)‬‭as‬‭an‬‭example,‬
‭the‬‭more‬‭teff‬‭you‬‭enter‬‭the‬‭machine‬‭the‬‭more‬‭time‬‭it‬‭takes‬‭to‬‭grind‬
‭it.‬

‭ esource‬‭-‬‭If‬‭the‬‭computer’s‬‭CPU‬‭speed‬‭and‬‭RAM‬‭size‬‭are‬‭very‬
R
‭efficient‬‭the‬‭algorithm‬‭will‬‭be‬‭executed‬‭fast.‬
‭If‬‭the‬‭train‬‭works‬‭by‬‭charcoal‬‭or‬‭electric‬‭they‬‭have‬‭differences.‬

‭ hoice‬‭of‬‭programming‬‭language‬‭-‬‭we‬‭have‬‭learned‬‭in‬
C
‭fundamentals‬‭of‬‭programming‬‭that‬‭it‬‭has‬‭an‬‭impact‬‭on‬‭program‬
‭execution‬‭time‬‭whether‬‭we‬‭choose‬‭Machine‬‭language,‬‭Compile‬

‭19‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

l‭anguage,‬‭or‬‭Interpreted‬‭language.‬‭Machine‬‭language‬‭has‬‭the‬
‭fastest‬‭execution‬‭time.‬

‭ xample‬
E
‭It‬‭is‬‭like‬‭choosing‬‭from‬‭a‬‭Bicycle,‬‭motorbike,‬‭Car,‬‭Train,‬‭or‬‭airplane.‬
‭All‬‭of‬‭them‬‭get‬‭the‬‭job‬‭done‬‭but‬‭we‬‭have‬‭to‬‭choose‬‭the‬‭fastest.‬‭In‬
‭our‬‭case‬‭which‬‭is‬‭the‬‭airplane.‬

‭We‬‭can‬‭measure‬‭the‬‭time‬‭takes‬‭to‬‭be‬‭executed.‬

‭Operation‬‭count‬‭-‬‭it‬‭refers‬‭to‬‭counting‬‭the‬‭basic‬‭processes‬‭and‬
‭steps‬‭in‬‭an‬‭algorithm‬‭to‬‭process‬‭a‬‭given‬‭data‬‭or‬‭solve‬‭a‬‭problem.‬

‭Case‬‭12‬‭-‬‭Revising‬‭studies‬

‭ uppose‬‭you‬‭are‬‭revising‬‭what‬‭you‬‭study‬‭for‬‭upcoming‬
S
‭exams‬‭.‬‭The‬‭steps‬‭you‬‭take‬‭are‬‭listed‬‭as‬‭follows.‬

‭ .‬
1 ‭ kim‬‭reading‬
S
‭2.‬ ‭Read‬‭more‬‭in‬‭detail‬‭about‬‭where‬‭you‬‭fell‬‭confused‬
‭3.‬ ‭Work‬‭on‬‭the‬‭worksheet‬‭that‬‭is‬‭given‬‭to‬‭you‬
‭4.‬ ‭Finally,‬‭work‬‭on‬‭last‬‭year's‬‭exams‬

‭ ee‬‭there‬‭are‬‭4‬‭basic‬‭steps‬‭you‬‭take‬‭to‬‭revise‬‭one‬‭subject‬‭(one‬
S
‭course).‬‭But‬‭what‬‭if‬‭you‬‭plan‬‭to‬‭revise‬‭n‬‭subjects,‬‭it‬‭means‬‭you‬‭have‬
‭to‬‭repeat‬‭this‬‭process‬‭n‬‭times.‬‭It‬‭means‬‭you‬‭have‬‭to‬‭perform‬‭4n‬
‭steps.‬

‭ he‬‭goal‬‭here‬‭is‬‭to‬‭prepare‬‭an‬‭algorithm‬‭with‬‭a‬‭lower‬‭operation‬
T
‭count‬‭for‬‭better‬‭efficiency.‬

‭20‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭Growth-Rate‬‭Function‬

I‭ t‬‭is‬‭a‬‭concept‬‭often‬‭associated‬‭with‬‭time‬‭complexity.‬‭It‬‭describes‬
‭how‬‭the‬‭time‬‭that‬‭takes‬‭to‬‭run‬‭a‬‭program‬‭will‬‭increase‬‭as‬‭the‬‭input‬
‭size‬‭increases.‬‭As‬‭discussed‬‭above‬‭topic‬‭if‬‭operation‬‭counts‬‭is‬‭high‬
‭time‬‭taken‬‭to‬‭run‬‭is‬‭also‬‭high.‬

I‭ t‬‭is‬‭often‬‭expressed‬‭using‬‭Big-O‬‭notation‬‭,‬‭which‬‭describes‬‭the‬
‭upper‬‭bound‬‭of‬‭an‬‭algorithm’s‬‭growth‬‭rate.‬

‭There‬‭are‬‭several‬‭ways‬‭of‬‭expressing‬‭the‬‭growth‬‭rate‬‭function.‬

‭1.‬ O
‭ (1)‬‭-‬‭Constant‬‭Time‬‭:‬‭The‬‭time‬‭required‬‭is‬‭constant,‬‭it‬
‭does‬‭not‬‭depend‬‭on‬‭the‬‭size‬‭of‬‭the‬‭input‬‭or‬‭operation‬
‭count.‬

‭ ase‬‭13:‬ ‭Saling‬‭at‬‭the‬‭shop‬
C
‭Let’s‬‭say‬‭you‬‭are‬‭a‬‭salesperson‬‭at‬‭the‬‭shop,‬‭and‬‭someone‬‭comes‬
‭and‬‭asks‬‭you‬‭to‬‭give‬‭him‬‭a‬‭packet‬‭of‬‭chewing‬‭gum.‬‭You‬‭give‬‭him‬‭in‬
‭no‬‭time.‬‭And‬‭also‬‭another‬‭dude‬‭comes‬‭and‬‭asks‬‭you‬‭to‬‭give‬‭him‬
‭candy,‬‭seems‬‭like‬‭it‬‭doesn’t‬‭take‬‭any‬‭additional‬‭time.‬

I‭ t‬‭means‬‭the‬‭time‬‭takes‬‭to‬‭serve‬‭a‬‭customer‬‭is‬‭the‬‭same‬‭and‬‭it‬
‭doesn't‬‭depend‬‭on‬‭how‬‭big‬‭your‬‭shop‬‭is‬‭or‬‭how‬‭many‬‭customers‬
‭you‬‭have.‬

‭2.‬ O
‭ (log‬‭n)‬‭-‬‭Logarithmic‬‭Time‬‭:‬‭The‬‭time‬‭required‬‭will‬
‭increase‬‭slowly‬‭as‬‭the‬‭problem‬‭size‬‭increases.‬
‭Binary‬‭search‬‭is‬‭a‬‭good‬‭example.‬‭(we‬‭will‬‭see‬‭it‬‭in‬‭Chapter‬‭2‬‭in‬
‭detail).‬

‭21‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭3.‬ L
‭ inear‬‭Time‬‭(O(n))‬‭:‬‭The‬‭time‬‭required‬‭will‬‭increase‬‭as‬
‭the‬‭problem‬‭size‬‭increases.‬‭They‬‭go‬‭hand‬‭in‬‭hand.‬

‭ ase‬‭14:‬‭Finding‬‭a‬‭Lost‬‭Person‬‭Public‬‭Holiday‬
C
‭We‬‭all‬‭have‬‭been‬‭in‬‭a‬‭situation‬‭where‬‭we‬‭somehow‬‭got‬‭lost‬‭from‬
‭our‬‭family‬‭when‬‭we‬‭were‬‭celebrating‬‭public‬‭holidays.‬‭Like‬‭Meskel‬‭,‬
‭Timket‬‭,‬‭and‬‭Eid‬‭al-feter….‬‭I(‬‭was‬‭lost‬‭during‬‭Timket‬‭and‬‭my‬‭family‬
😄
‭almost‬‭killed‬‭me‬‭when‬‭they‬‭found‬‭me‬‭ ).‬‭Now‬‭answer‬‭the‬‭question‬
‭that,‬‭from‬‭the‬‭following‬‭situation‬‭which‬‭one‬‭will‬‭be‬‭easier‬‭for‬‭our‬
‭parents‬‭to‬‭find‬‭us?‬

I‭ f‬‭people's‬‭size‬‭is‬‭small‬‭or‬‭quite‬‭big‬‭large.‬‭Of‬‭course,‬‭if‬‭it‬‭is‬‭small.‬
‭See‬‭if‬‭input‬‭size‬‭increases‬‭time‬‭it‬‭takes‬‭increases‬‭too.‬
‭4.‬ ‭Linearithmic‬‭Time‬‭(O(n‬‭log‬‭n))‬‭:‬‭The‬‭time‬‭it‬‭takes‬
‭increases‬‭more‬‭rapidly‬‭than‬‭linear‬‭time.‬
‭Case‬‭15:‬‭Abraham's‬‭friend's‬‭birthday.‬

‭ braham‬‭has‬‭a‬‭lot‬‭of‬‭friends‬‭until‬‭he‬‭struggles‬‭to‬‭remember‬
A
‭arranging‬‭the‬‭order‬‭of‬‭their‬‭birthdays.‬‭Trying‬‭to‬‭arrange‬‭in‬‭one‬‭try‬
‭is‬‭hard,‬‭so‬‭he‬‭uses‬‭a‬‭smart‬‭way.‬

‭ e‬‭separates‬‭them‬‭into‬‭small‬‭groups‬‭and‬‭arranges‬‭each‬‭group.‬‭He‬
H
‭repeats‬‭the‬‭same‬‭process‬‭for‬‭all‬‭groups‬‭and‬‭finally‬‭arranges‬‭all‬‭of‬
‭his‬‭friend's‬‭birthdays.‬

‭It‬‭is‬‭used‬‭in‬‭Merge‬‭Sort‬‭or‬‭Heap‬‭Sort.‬‭we‬‭will‬‭see‬‭them‬‭in‬‭the‬‭future.‬

‭5.‬ O
‭ (n^2)‬‭-‬‭Quadratic‬‭Time‬‭:‬‭The‬‭time‬‭required‬‭increases‬
‭rapidly‬‭with‬‭the‬‭size‬‭of‬‭a‬‭problem.‬
‭ ubble‬‭sort‬‭is‬‭a‬‭good‬‭example.‬
B

‭22‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭ ase‬‭16:‬ ‭Biniyam‬‭lost‬‭phone.‬
C
‭Biniyam‬‭lost‬‭his‬‭phone‬‭in‬‭a‬‭library.‬‭So‬‭he‬‭suspects‬‭it‬‭is‬‭stolen‬‭by‬
‭someone.‬‭So‬‭he‬‭tells‬‭the‬‭librarian‬‭to‬‭never‬‭let‬‭students‬‭out‬‭he‬‭starts‬
‭to‬‭find‬‭his‬‭phone‬‭by‬‭trying‬‭to‬‭check‬‭each‬‭student's‬‭pocket.‬‭He‬‭is‬
‭also‬‭checking‬‭if‬‭it‬‭matches‬‭his‬‭phone.‬‭Lucky‬‭for‬‭him,‬‭he‬‭finds‬‭it‬‭in‬
‭one‬‭student’s‬‭pocket.‬

‭ ee‬‭time‬‭required‬‭to‬‭do‬‭this‬‭task‬‭increases‬‭rapidly‬‭with‬‭the‬‭size‬‭of‬
S
‭the‬‭problem.‬

‭6.‬ O
‭ (n3)‬‭-‬‭Cubic‬‭time:‬‭The‬‭time‬‭requirement‬‭for‬‭a‬‭cubic‬
‭algorithm‬‭increases‬‭more‬‭rapidly‬‭with‬‭the‬‭size‬‭of‬‭the‬
‭problem‬‭than‬‭the‬‭time‬‭requirement‬‭for‬‭a‬‭quadratic‬
‭algorithm.‬

‭7.‬ E
‭ xponential‬‭Time‬‭(O(2^n)):‬‭As‬‭the‬‭size‬‭of‬‭the‬‭problem‬
‭increases,‬‭the‬‭time‬‭requirement‬‭for‬‭an‬‭exponential‬
‭algorithm‬‭increases‬‭too‬‭rapidly‬‭to‬‭be‬‭practical.‬

‭Best‬‭case‬‭analysis‬
I‭ t‬‭is‬‭a‬‭possible‬‭minimum‬‭time‬‭or‬‭resources‬‭that‬‭takes‬‭an‬‭algorithm‬
‭to‬‭complete‬‭a‬‭task.‬‭Indicates‬‭the‬‭algorithm‬‭is‬‭best.‬

‭Worst‬‭case‬‭analysis‬
I‭ t‬‭is‬‭a‬‭possible‬‭maximum‬‭time‬‭or‬‭resources‬‭that‬‭takes‬‭an‬‭algorithm‬
‭to‬‭complete‬‭a‬‭task.‬‭Indicates‬‭the‬‭algorithm‬‭is‬‭bad.‬

‭Average‬‭case‬‭analysis‬
I‭ t‬‭is‬‭a‬ ‭normal‬‭time‬‭or‬‭resources‬‭that‬‭takes‬‭an‬‭algorithm‬‭to‬
‭complete‬‭a‬‭task.‬‭Indicates‬‭the‬‭algorithm‬‭is‬‭not‬‭bad‬‭or‬‭best.‬

‭23‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭Case‬‭17‬‭Ahmed’s‬‭journey‬‭to‬‭school‬

‭ hmed‬‭is‬‭going‬‭to‬‭his‬‭school,‬‭so‬‭there‬‭are‬‭3‬‭ways‬‭to‬‭go‬‭even‬
A
‭regarding‬‭arriving‬‭at‬‭school.‬

‭ .‬ G
1 ‭ o‬‭using‬‭a‬‭taxi‬‭-‬‭very‬‭fast‬‭(Best‬‭case‬‭analysis)‬
‭2.‬ ‭Go‬‭by‬‭walking‬‭-‬‭Normal‬‭time‬‭(Average‬‭case‬‭analysis)‬
‭3.‬ ‭Go‬‭with‬‭his‬‭friends‬‭playing‬‭and‬‭talking‬‭-‬‭Very‬‭slowly‬
‭and‬‭he‬‭might‬‭be‬‭late‬‭for‬‭school‬‭(Worst‬‭case‬‭analysis)‬

‭24‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

Unit‬‭
‭ 2‬
Simple‬‭
‭ searching‬‭
and‬‭
sorting‬‭
algorithms‬

‭ e‬‭have‬‭seen‬‭in‬‭detail‬‭what‬‭algorithms‬‭mean‬‭with‬‭their‬
W
‭properties.‬‭We‬‭have‬‭also‬‭discussed‬‭algorithm‬‭complexity‬‭analysis.‬

‭ ow‬‭it‬‭is‬‭time‬‭when‬‭we‬‭see‬‭how‬‭an‬‭algorithm‬‭can‬‭be‬ ‭used‬‭for‬
N
‭solving‬‭computational‬‭problems.‬ ‭We‬‭will‬‭develop‬‭a‬‭simple‬
‭algorithm‬‭for‬‭searching‬‭and‬‭sorting‬‭purposes‬‭and‬‭we‬‭will‬‭write‬‭a‬
‭C++‬‭program‬‭to‬‭implement‬‭the‬‭algorithm.‬

‭Searching‬‭algorithms‬

‭ t‬‭is‬‭quite‬‭useful‬‭for‬‭locating‬‭specific‬‭data‬‭from‬‭a‬‭collection‬‭of‬
I
‭data‬‭like‬‭arrays.‬‭They‬‭are‬‭designed‬‭to‬‭retrieve‬‭target‬‭data‬‭from‬
‭where‬‭it‬‭is‬‭stored.‬

‭Case‬‭1‬‭Searching‬‭contact‬‭from‬‭your‬‭phone.‬

‭ magine‬‭if‬‭we‬‭can’t‬‭search‬‭for‬‭a‬‭specific‬‭name‬‭and‬‭we‬‭are‬
I
‭supposed‬‭to‬‭check‬‭every‬‭contact‬‭of‬‭our‬‭phone‬‭to‬‭call‬‭to‬‭our‬‭friend.‬
‭How‬‭boring‬‭it‬‭would‬‭be?‬‭But‬‭thanks‬‭to‬‭modern‬‭applications‬‭we‬‭are‬
‭not‬‭supposed‬‭to‬‭do‬‭this‬‭tiresome‬‭task.‬‭And‬‭here‬‭is‬‭how‬‭those‬
‭applications‬‭make‬‭the‬‭searching‬‭task‬‭so‬‭easy.‬

‭We‬‭will‬‭see‬‭algorithms‬‭as‬‭follows.‬

‭25‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭Linear‬‭search‬

‭ t‬‭is‬‭an‬‭algorithm‬‭that‬‭locates‬‭the‬‭target‬‭by‬‭checking‬‭the‬‭entire‬
I
‭array‬‭(data).‬‭So‬‭it‬‭checks‬‭and‬‭matches‬‭the‬‭entire‬‭data‬‭one‬‭by‬‭one‬
‭if‬‭it‬‭is‬‭matched‬‭with‬‭the‬‭target‬‭and‬‭will‬‭return‬‭the‬‭target‬‭data.‬

‭ t‬‭will‬‭stop‬‭searching‬‭for‬‭two‬‭cases‬‭either‬‭if‬‭it‬‭finds‬‭a‬‭target‬‭or‬‭if‬
I
‭the‬‭target‬‭element‬‭is‬‭not‬‭in‬‭the‬‭given‬‭dataset.‬

‭Case‬‭2‬‭Police‬‭chasing‬‭the‬‭theft‬

‭ ome‬‭crime‬‭is‬‭committed‬‭in‬‭your‬‭village‬‭but‬‭security‬‭cameras‬
S
‭have‬‭captured‬‭criminals'‬‭faces.‬‭So‬‭the‬‭police‬‭are‬‭searching‬‭for‬
‭these‬‭criminals‬‭by‬‭checking‬‭everyone's‬‭face‬‭who‬‭lives‬‭in‬‭a‬‭hood‬
‭with‬‭security‬‭footage.‬

‭ fter‬‭an‬‭intensive‬‭2-hour‬‭search‬‭they‬‭found‬‭the‬‭person‬‭that‬‭was‬
A
‭exactly‬‭matched‬‭with‬‭a‬‭photo‬‭that‬‭was‬‭captured‬‭by‬‭the‬‭security‬
‭camera‬‭and‬‭the‬‭police‬‭took‬‭him‬‭to‬‭prison.‬‭See‬‭this‬‭is‬‭exactly‬‭how‬
‭linear‬‭search‬‭algorithms‬‭work‬‭to‬‭search‬‭specific‬‭data‬‭from‬‭a‬
‭collection‬‭of‬‭data.‬

‭ t‬‭seems‬‭a‬‭good‬‭search‬‭algorithm‬‭because‬‭each‬‭item‬‭of‬‭data‬‭is‬
I
😄
‭accessed.‬‭It‬‭is‬‭like‬‭how‬‭we‬‭find‬‭our‬‭lost‬‭money‬‭ .‬‭We‬‭tend‬‭to‬
‭check‬‭it‬‭in‬‭every‬‭clothes‬‭pocket‬‭even‬‭if‬‭we‬‭didn't‬‭wear‬‭those‬
‭clothes‬‭on‬‭the‬‭day‬‭our‬‭money‬‭was‬‭lost.‬

‭ t‬‭also‬‭has‬‭a‬‭horrible‬‭disadvantage‬‭which‬‭is‬‭time.‬‭As‬‭input‬‭size‬‭or‬
I
‭data‬‭size‬‭is‬‭increased‬‭the‬‭time‬‭to‬‭find‬‭that‬‭data‬‭grows‬‭faster‬‭too.‬
‭So‬‭it‬‭takes‬‭more‬‭time‬‭to‬‭search.‬

‭26‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭ n‬‭the‬‭above‬‭case‬‭if‬‭a‬‭lot‬‭of‬‭people‬‭live‬‭in‬‭a‬‭given‬‭village‬‭it‬‭takes‬
I
‭quite‬‭a‬‭lot‬‭of‬‭time‬‭to‬‭identify‬‭and‬‭catch‬‭the‬‭theft.‬

‭Linear‬‭search‬‭algorithm‬

‭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’s‬‭complexity‬‭analysis‬

‭ est‬‭case‬‭is‬‭if‬‭the‬‭target‬‭element‬‭is‬‭at‬‭the‬‭start.‬
B ‭ (1)‬
O
‭Average‬‭case‬‭if‬‭the‬‭target‬‭element‬‭is‬‭in‬‭the‬‭middle.‬ ‭O(n)‬
‭Worst‬‭case‬‭is‬‭if‬‭the‬‭target‬‭element‬‭is‬‭at‬‭the‬‭end.‬ ‭O(n)‬

‭And‬‭its‬‭time‬‭complexity‬‭is‬‭big-O‬‭of‬‭n.‬‭O(n)‬

‭27‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭Binary‬‭Search‬

I‭ t‬‭works‬‭in‬‭a‬‭bit‬‭different‬‭way‬‭when‬‭it‬‭is‬‭compared‬‭with‬‭a‬‭linear‬
‭search‬‭algorithm.‬‭It‬‭works‬‭only‬‭for‬‭sorted‬‭data.‬

‭Please‬‭carefully‬‭read‬‭this.‬

I‭ t‬‭works‬‭by‬‭dividing‬‭a‬‭given‬‭list‬‭in‬‭the‬‭middle‬‭and‬‭we‬‭get‬‭the‬‭middle‬
‭element.‬‭And‬‭it‬‭means‬‭we‬‭have‬‭two‬‭lists‬‭now.‬

‭ .‬ L
1 ‭ ist‬‭which‬‭consists‬‭of‬‭lower‬‭than‬‭middle‬‭element‬
‭2.‬ ‭List‬‭which‬‭consists‬‭of‬‭greater‬‭than‬‭the‬‭middle‬‭element‬

‭Now‬‭it‬‭will‬‭check‬‭3‬‭things.‬

‭ .‬ I‭ f‬‭the‬‭target‬‭data‬‭is‬‭in‬‭the‬‭middle.‬‭If‬‭it‬‭is,‬‭it‬‭will‬‭return‬‭it.‬
1
‭2.‬ ‭If‬‭the‬‭target‬‭data‬‭is‬‭in‬‭the‬‭list‬‭it‬‭is‬‭lower‬‭than‬‭the‬‭middle‬
‭element.‬ ‭It‬‭will‬‭completely‬‭ignore‬‭the‬‭list‬‭which‬‭consists‬
‭of‬‭greater‬‭than‬‭middle‬‭element.‬‭Because‬‭it‬‭will‬‭be‬
‭waste‬‭of‬‭time.‬‭There‬‭is‬‭also‬‭an‬‭Amharic‬‭quote‬ ‭that‬
‭goes‬‭like‬‭this.‬‭(ላም‬‭ባልዋለበት‬‭ኩበት‬‭ለቀማ)‬
‭3.‬ ‭It‬‭will‬‭also‬‭do‬‭the‬‭same‬‭thing‬‭as‬‭number‬‭2‬‭if‬‭the‬‭target‬
‭data‬‭is‬‭greater‬‭than‬‭a‬‭middle‬‭element.‬

I‭ t‬‭will‬‭perform‬‭the‬‭above‬‭operation‬‭over‬‭and‬‭over.‬‭Dividing‬
‭and‬‭dividing‬‭a‬‭list‬‭until‬‭it‬‭gets‬‭the‬‭target‬‭data.‬

‭For‬‭this‬‭reason,‬‭we‬‭can‬‭only‬‭apply‬‭binary‬‭search.‬

‭28‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭Binary‬‭search‬‭algorithm‬

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‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭It’s‬‭complexity‬‭analysis‬

‭ est‬‭case‬‭-‬‭if‬‭the‬‭target‬‭is‬‭at‬‭the‬‭middle.‬‭O(1)‬
B
‭Worst‬‭case‬‭-‬‭if‬‭the‬‭target‬‭is‬‭at‬‭the‬‭first‬‭or‬‭at‬‭the‬‭last.‬‭O(log‬‭n)‬
‭Average‬‭case‬‭-‬‭it‬‭is‬‭somewhere‬‭in‬‭the‬‭list.‬‭O(log‬‭n)‬

‭ verall‬‭time‬‭complexity‬‭is‬‭O(log‬‭n).‬
O
‭It‬‭was‬‭all‬‭the‬‭basics‬‭about‬‭simple‬‭searching‬‭algorithms‬

‭Simple‬‭sorting‬‭algorithms‬

‭ orting‬‭algorithm‬‭is‬‭an‬‭algorithm‬‭which‬‭used‬‭to‬‭sort‬‭a‬‭list.‬‭Does‬‭it‬
S
‭sound‬‭circular?‬‭Yes‬‭it‬‭is!‬‭It‬‭help‬‭us‬‭to‬‭order‬‭lists‬‭in‬‭increasing‬‭or‬
‭decreasing‬‭form.‬

‭ here‬‭are‬‭two‬‭types‬‭of‬‭sorting.‬‭Pre-sorted‬‭&‬‭Post-sorted‬‭sorting.‬
T
‭Pre-sorted‬‭is‬‭in‬‭ascending‬‭order,‬‭and‬‭post-sorted‬‭is‬‭desending‬
‭order.‬

‭There‬‭are‬‭3‬‭types‬‭of‬‭sorting‬‭algorithms.‬
‭1.‬ ‭Bubble‬‭sort‬
‭2.‬ ‭Selection‬‭sort‬
‭3.‬ ‭Insertion‬‭sort‬

‭1.‬‭Bubble‬‭sort‬

I‭ t‬‭basically‬‭compares‬‭two‬‭adjacent‬‭list’s‬‭elements‬‭and‬‭it‬‭will‬‭swap‬
‭the‬‭smaller‬‭element‬‭to‬‭the‬‭left‬‭and‬‭the‬‭higher‬‭to‬‭right.‬‭It‬‭will‬‭do‬‭the‬
‭same‬‭thing‬‭until‬‭the‬‭end‬‭of‬‭a‬‭list.‬‭Once‬‭done.‬‭It‬‭will‬‭do‬‭it‬‭again‬‭and‬
‭again‬‭until‬‭all‬‭items‬‭are‬‭sorted.‬

‭30‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

I‭ t‬‭is‬‭very‬‭useful‬‭for‬‭a‬‭list‬‭that‬‭is‬‭already‬‭sorted‬‭or‬‭just‬‭needs‬‭a‬‭little‬
‭modification.‬

‭It‬‭is‬‭very‬‭slow.‬‭Its‬‭time‬‭complexity‬‭is‬‭O(n^2).‬

‭2.‬‭Selection‬‭sort‬

‭ s‬‭you‬‭know‬‭if‬‭we‬‭are‬‭sorting‬‭we‬‭have‬‭to‬‭prepare‬‭an‬‭array‬‭of‬
A
‭elements.‬‭And‬‭here‬‭is‬‭what‬‭this‬‭algorithm‬‭will‬‭do‬‭to‬‭the‬‭array‬‭in‬
‭order‬‭to‬‭sort‬‭it.‬

I‭ t‬‭splits‬‭the‬‭given‬‭array‬‭into‬‭two‬‭parts.‬‭Sorted‬‭and‬‭unsorted‬‭parts.‬
‭It‬‭finds‬‭the‬‭smallest‬‭element‬‭from‬‭unsorted‬‭and‬‭puts‬‭to‬‭the‬‭end‬‭of‬
‭the‬‭sorted‬‭list.‬‭This‬‭process‬‭will‬‭be‬‭repeated‬‭over‬‭and‬‭over‬‭until‬‭all‬
‭elements‬‭are‬‭sorted.‬

‭It‬‭is‬‭also‬‭slow‬‭with‬‭the‬‭time‬‭complexity‬‭of‬‭O(n^2).‬

‭3.‬‭Insertion‬‭sort‬

‭ his‬‭algorithm‬‭is‬‭kind‬‭of‬‭best‬‭algorithm‬‭because‬‭it‬‭sorts‬‭the‬‭given‬
T
‭array‬‭one‬‭at‬‭a‬‭time.‬‭This‬‭means‬‭it‬‭picks‬‭one‬‭element‬‭and‬‭puts‬‭it‬‭in‬
‭its‬‭proper‬‭place‬‭as‬‭a‬‭sorted‬‭list.‬

‭Case‬‭sorting‬‭deck‬‭of‬‭cards‬

‭ et’s‬‭say‬‭you‬‭have‬‭unsorted‬‭pieces‬‭of‬‭card‬‭in‬‭your‬‭hand‬‭and‬‭you‬
L
‭want‬‭to‬‭sort‬‭them.‬‭But‬‭also‬‭since‬‭it‬‭is‬‭small‬‭you‬‭don’t‬‭to‬‭waste‬‭your‬
‭time‬‭by‬‭swapping‬‭each‬‭element‬‭or‬‭by‬‭putting‬‭ordered‬‭items‬‭at‬‭one‬
‭place.‬‭So‬‭you‬‭sorted‬‭it‬‭in‬‭your‬‭hand‬‭by‬‭seeing‬‭the‬ ‭number‬‭and‬
‭putting‬‭it‬‭in‬‭its‬‭proper‬‭place.‬

‭31‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭ he‬‭above‬‭is‬‭how‬‭you‬‭use‬‭insertion‬‭sort‬‭practically.‬‭And‬‭it‬‭works‬
T
‭efficiently‬‭for‬‭small-size‬‭data.‬‭If‬‭data‬‭gets‬‭larger‬‭it‬‭confuses.‬

‭ his‬‭is‬‭all‬‭about‬‭the‬‭basics‬‭of‬‭simple‬‭algorithms‬‭and‬‭please‬‭refer‬‭to‬
T
‭other‬‭materials‬‭too‬‭to‬‭get‬‭the‬‭best‬‭out‬‭of‬‭this‬‭book.‬

‭Good‬‭Luck‬

‭32‬
‭Data‬‭structure‬‭and‬‭algorithm‬‭101‬ ‭By‬‭wondmeneh‬‭fekadu‬

‭About‬‭Me‬
I‭ ‬‭am‬‭Wondmeneh‬‭Fekadu,‬‭a‬‭dedicated‬‭2nd-year‬‭student‬‭at‬‭Adama‬
‭Science‬‭and‬‭Technology‬‭University.‬‭My‬‭academic‬‭journey‬‭is‬‭not‬‭just‬‭a‬
‭pursuit‬‭of‬‭knowledge;‬‭it‬‭is‬‭a‬‭passionate‬‭exploration‬‭of‬‭the‬‭intricate‬
‭world‬‭of‬‭technology.‬

I‭ 'm‬‭all‬‭up‬‭for‬‭connecting.‬‭Whether‬‭you‬‭need‬‭help‬‭with‬‭tech‬‭stuff,‬‭want‬
‭to‬‭team‬‭up‬‭on‬‭projects,‬‭or‬‭just‬‭fancy‬‭a‬‭chat,‬‭hit‬‭me‬‭up.‬
‭I'd‬‭love‬‭to‬‭hear‬‭your‬‭feedback,‬‭so‬‭don't‬‭hesitate‬‭to‬‭reach‬‭out‬‭with‬
‭thoughts‬‭or‬‭suggestions.‬‭Connecting‬‭with‬‭you‬‭would‬‭bring‬‭a‬‭smile‬‭to‬
‭my‬‭face!‬

‭Contact‬‭Information:‬
📞
‭ ‬‭Phone:‬‭09‬‭84‬‭55‬‭92‬‭33‬
📧
‭ ‬‭Email:‬‭[email protected]
🌐
‭ ‬‭Telegram:‬‭t.me/wondifekadu‬

‭33‬

You might also like