Data Structures
Data Structures
Data Structures
Arrays
Collection of homogenous data elements. 'lements are stored in computer memory in a linear fashion. Referred ,ith the same name. -se su"scripts or inde es to refer to indi!idual elements.
& .ne#dimensional arrays & /ulti#dimensional arrays
One-Dimensional Arrays
Synta for declaring an array in C001
type !aria"le2name3SI4'56
/ust "e e plicitly defined so that the compiler can allocate memory.
Address Calculation
Array elements are stored in contiguous memory locations "y se(uential allocation techni(ues. Starting address or address of the first element is called Base Address* B Si7e of the element is denoted "y S. Consider the array arr in ,hich $B 89 -B* ,here $B denotes the lo,er "ound and -B denotes the upper "ound of the array. The address of the ith element is gi!en "y1 address of arr[i] = B + (I LB) * S
Example
Gi!en an array A3:1;<5. If B 9 ;::: and S 9 = then calculate the address of A3;:5. Sol1 B 9 ;::: S9= Address of A3i5 9 B 0 >i & $B? @ S Address of A3;:5 9 ;::: 0 >;: & :? @ = 9 ;::: 0 =: 9 ;:=:
Example
Gi!en an array A ,ith $B 9 :. If address of A3;:5 9 =:A< and address of A3;<5 9 =:B:* then calculate the address of A3C5. Sol1 $et B "e "ase address and S the num"er of ,ords re(uired to store an element in the array A. Then* Address of A3i5 9 B 0 >i & $B? @ S Address of A3;:5 9 B 0 >;: & :? @ S So* B 0 ;:S 9 =:A< Similarly* B 0 ;<S 9 =:B: So* sol!ing the a"o!e t,o e(uations gi!es us* S 9 D and B 9 =:E< So* address of A3C5 9 =:E< 0 >C & :? @ D 9 =:E< 0 =D 9 =:<B
T!o-Dimensional Arrays
Grid ha!ing ro,s and columns in ,hich each element is specified "y t,o su"scripts. The first su"script identifies ro, num"er and second su"script identifies column num"er of the element. Since the main memory of a computer is linear* =% array cannot "e stored as a grid. Array elements are stored linearly using one of the follo,ing methods1
& Ro,#maFor1
Array is stored ,ith all elts of first ro, in se(uence follo,ed "y elts of second ro, and so on.
& Column#maFor Array is stored ,ith all elts of first column in se(uence follo,ed "y elts of second column and so on.
Example
An array G3;<53E:5 is stored in the memory ,ith each element re(uiring H "ytes of storage. If the "ase address of G is <E::* find out the memory locations of G3H53;=5* if the array is stored along the ro,. Sol1 G3;<53E:5 means !alid ro, indices are : to ;D and !alid column indices are : to =B. B 9 <E:: S 9 H "ytes +um"er of ro,s / 9 ;D & : 0 ; 9 ;< +um"er of columns + 9 =B & : 0 ; 9 E: Address of G3i53F5 9 B 0 S@ 3>i & $B;? @ + 0 >F & $B=?5 Address of G3H53;=5 9 <E:: 0 H 3>H & :? @ E: 0 >;= & :?5 9 <E:: 0 H 3 H @ E: 0 ;=5 9 <E:: 0 H 3 =D: 0 ;=5 9 <E:: 0 H @ =<= 9 <E:: 0 =:;C 9 7316
Operations on Arrays
Searching Tra!ersal Sorting Insertion %eletion /erging
Searchin$
T,o common searching techni(ues are1
& $inear Search
'ach element of the array is compared ,ith the gi!en item to "e searched for* one "y one.
%inear Search
Searching is done "y comparing the gi!en item ,ith e!ery item in the array from the "eginning till a match is met. If a match is found* the location of the element is returned* other,ise +-$$ is returned.
ALGORITHM: A linear array DATA with N elements and a spe i!i IT"M #! in!#rmati#n are $i%en& This al$#rithm !inds the l# ati#n LO' #! IT"M in the array DATA #r sets LO' ( )& 1& *Initiali+e, -et .(1 and LO' ( ) /& Repeat -teps 3 and 0 while LO' ( ) and . 1( N& 3& I! IT"M ( DATA*., then: -et LO' ( . 0& -et . ( . 2 1 *In rements #3nter, *"nd #! step / l##p, *-3 ess!3l 4, 5& I! LO' ( ) then: 6rite : IT"M is n#t in the array DATA& "lse: 6rite: IT"M is in the l# ati#n LO'& *"nd #! I7 str3 t3re, 6& "8it Applet Program ' e
&inary Search
.!ercomes limitations of linear search. Re(uires less num"er of comparisons than linear search. Can "e used only on sorted lists. $ength of array is reduced "y half at each step.
Al$orithm
BI+ARK>%ATA* $B* -B* IT'/* $.C? Lere %ATA is a sorted array ,ith lo,er "ound $B and upper "ound -B* and IT'/ is a gi!en item of information. The !aria"les B'G* '+% and /I% denote the "eginning* end and middle locations of a segment of elements of %ATA. This algorithm finds the location $.C of IT'/ in %ATA or sets $.C 9 +-$$. ;. 3Initiali7e segment !aria"les5 Set B'G 9 $B* '+% 9 -B and /I% 9 I+T>>B'G 0 '+%? M =?. =. Repeat steps E ,hile B'G 89 '+% and %ATA3/I%5 N9 IT'/ E. If IT'/ 8 %ATA3/I%5 then1 Set '+% 9 /I% & ; 'lse1 Set B'G 9 /I% 0 ; 3'nd of IO structure5 Set /I% 9 I+T>>B'G0'+%?M=? 3'nd of step = loop5 D.If %ATA3/I%5 9 IT'/ then1 Set $.C 9 /I% 'lse1 Set $.C 9 +-$$ 3'nd of IO structure5 <. ' it Applet 9r#$ram "8e
Al$orithm
Algorithm for inserting1 I+S'RT>$A* +* P* IT'/? Lere $A is a linear array ,ith + elements and P is a positi!e integer such that P 89 +. This algorithm into the Pth position in $A. ;. 3Initiali7e counter5 Set J 9 +. =. Repeat steps E and D ,hile J R9 P. E. 3 /o!e Jth element do,n,ard5 Set $A3J0;5 9 $A3J5. D. 3%ecrease counter5 Set J 9 J & ;. 3'nd of Step = $oop5 <. 3Insert element5 Set $A3P5 9 IT'/ C. 3Reset +5 Set + 9 +0;. A. ' it. Program ' e
Sortin$
Sorting means arranging the elements in some specific order i.e.* either in ascending or descending order. Garious sorting techni(ues you ,ill learn are
& Bu""le Sort & Insertion Sort & Selection Sort & /erge Sort
&u**le Sort
Je pass through the array se(uentially many times. 'ach pass places the largest unsorted element in its proper position "y comparing each element in the array ,ith its successor element and s,apping the t,o elements if these are not in proper order.
Al$orithm
B-BB$'>%ATA* +? Lere %ATA is an array ,ith + elements. This algorithm sorts the elements in %ATA. ;. Repeat steps = and E for P 9 ; to + & ;. =. Set PTR 9 ; 3Initiali7es pass pointer to ;5 E. Repeat ,hile PTR 89 + & P 3' ecutes pass5 >a? If %ATA3PTR5 R %ATA3PTR 0 ;5 then1 Interchange %ATA3PTR5 and %ATA3PTR 0 ;5 3'nd of if statement5 >"? Set PTR 9 PTR 0 ; 3'nd of inner loop5 3'nd of step ; outer loop5 D. ' it Applet Program ' e
In Inthe thethird third #mparis#n; #mparis#n;the the::is isn#t n#tlar$er lar$erthan thanthe the1/ 1/s# s#n# n# e8 han$e is made& 6e m#%e #n t# #mpare the ne8t pair with#3t e8 han$e is made& 6e m#%e #n t# #mpare the ne8t pair with#3t any any han$e han$et# t#the thelist& list& The The1/ 1/is islar$er lar$erthan thanthe the11 11s# s#they theyare aree8 e8 han$ed& han$ed&
The Thetwel%e twel%eis is$reater $reaterthan thanthe the::s# s#they theyare aree8 e8 han$ed han$ed The Theend end#! #!the thelist listhas has<een <eenrea rea hed heds# s#this thisis isthe theend end#! #!the the!irst !irstpass& pass& The The twel%e twel%eat atthe theend end#! #!the thelist listm3st m3st<e <elar$est lar$estn3m<er n3m<erin inthe thelist listand ands# s#is isn#w n#win in The 1/ is $reater than the 3 s# they are e8 han$ed& the t tp#siti#n& 6e pass le!t The 1/ is $reater than the 3a s# they are!r#m e8 han$ed& the #rre #rre p#siti#n& 6en#w n#wstart start anew new pass !r#m le!tt# t#ri$ht& ri$ht& The The1/ 1/is is$reater $reaterthan thanthe the77s# s#they theyare aree8 e8 han$ed& han$ed&
9, 6, 6, 6, 6, 6, 6, 6,
6, 9, 2, 2, 2, 2, 2, 2,
2, 2, 9, 9, 9, 9, 9, 9,
First Pass
First Pass
First Pass
2, 6, 6, 6,
9, 9, 9, 9, 3,
12 12 12 12
"a "a h hpass passre?3ires re?3ires!ewer !ewer #mparis#ns& #mparis#ns& This Thistime time#nly #nly00are areneeded& needed&
First Pass
2, 6, 6, 6, 6, 3,
9, 9, 9, 3, 3, 6,
12 12 12 12 12
The Thelist listis isn#w n#ws#rted s#rted<3t <3tthe theal$#rithm al$#rithmd#es d#esn#t n#t>n#w >n#wthis this3ntil 3ntilit it #mpletes #mpletesaapass passwith withn# n#e8 e8 han$es& han$es&
6, Second Pass 2, Third Pass 2, Fourth Pass 2, Fifth Pass 2, Sixth Pass 2,
First Pass
This Thispass passn# n#e8 e8 han$es han$esare aremade mades# s#the theal$#rithm al$#rithm>n#ws >n#wsthe thelist listis is s#rted& s#rted& It It an anthere!#re there!#resa%e sa%etime time<y <yn#t n#td#in$ d#in$the the!inal !inalpass& pass& 6ith 6ith #ther #therlists liststhis this he he >> #3ld #3ldsa%e sa%em3 m3 h hm#re m#rew#r>& w#r>&
2, 6, 6, 6, 3, 3,
9, 9, 9, 3, 6, 6,
12 12 12 12 12 12
- #xamine element at $osition x% &'nsert( it in sorted order in the list from to x-1% - )e$eat until x * si"e-1
Al$orithm
I+S'RTI.+ >A* +? This algorithm sorts the array ,ith + elements. ;. Set A3:5 9 #S. 3Initiali7es sentinel element5. =. Repeat steps E to < for P 9 =* E* Q.. +1 E. Set T'/P 9 A3P5 and PTR 9 P#; D. Repeat ,hile T'/P 8 A3PTR51 >a? Set A3PTR0;5 9 A3PTR5 3/o!es element for,ard5 >"? Set PTR 9 PTR#; 3'nd of loop5 <. Set A3PTR0;5 9 T'/P. 3Inserts element in proper place5 3'nd of step = loop5 C. Return Program ' e
- Search the entire list and &select( the element /ith the smallest 0alue% S/a$ it /ith the element at the to$ of the list% - )e$eat until si"e-1 $asses ha0e been made throu.h the list
Al$orithm
/I+>A* P* +* $.C? An array A is in memory. This procedure finds the location $.C of the smallest element among A3P5* A3P0;5* QQQ.. A3+5 ;. Set /I+ 9 A3P5 and $.C 9 P 3Initiali7es pointers5. =. Repeat for J 9 P0;* P0=* QQ +1 If /I+ R A3J5* then set /I+ 9 A3J5 and $.C 9 J 3'nd of $oop5 E. Return. S'$'CTI.+>A* +? This algorithm sorts the array A ,ith + elements. ;. Repeat steps = and E for P 9 ;* =* QQ +#;1 =. Call /I+>A* P* +* $.C?. 3Interchange A3P5 and A3$.C55 E. Set T'/P 9 A3P5* A3P5 9 A3$.C5 and A3$.C5 9 T'/P 3'nd of Step ; $oop5 D. ' it. Applet Program ' e
#er$e Sort
/erging is Foining t,o sorted arrays as one "ig sorted array. Kou can merge t,o unsorted arrays as ,ell. Je ,ill "e dealing ,ith t,o sorted array merging.
Al$orithm
/'RGI+G>A* R* B* S* C? $et A and B "e sorted arrays ,ith R and S elements respecti!ely. This algorithm merges A and B into an array C ,ith + 9 R 0 S elements. ;. 3Initiali7e.5 Set +A 9 ;* +B 9 ; and PTR 9 ; =. 3Compare5 Repeat ,hile +A 89 R and +B 89 S1 If A3+A5 8 B3+B5 then1 >a? 3Assign element from A to C5 Set C3PTR5 9 A3+A5 >"? 3-pdate pointers5 Set PTR 9 PTR0; and +A 9 +A0; 'lse >a? 3Assign element from B to C5 Set C3PTR5 9 B3+B5 >"? 3-pdate pointers5 Set PTR 9 PTR0; and +B 9 +B0; 3'nd of IO structure5 3'nd of $oop5 ContdQ
E. 3Assign remaining elements to C5 If +A R R then1 Repeat for P 9 :* ;* =* Q. S#+B1 Set C3PTR0P5 9 B3+B0P5 3'nd of $oop5 'lse1 Repeat for P 9 :* ;* =* Q. R#+A1 Set C3PTR0P5 9 A3+A0P5 3'nd of $oop5 3'nd of IO structure5 D. ' it Comple ity of /erge Sort algorithm1 The input consists of the total num"er n = r + s of elements in A and B. 'ach comparison assigns an element to the array C* ,hich e!entually has n elements. Accordingly* the num"er f(n) of comparisons cannot e ceed n. In other ,ords* the merging algorithm can "e run in linear line. Program ' e
Wor+in$ on -D arrays
Tra!ersal Sum of diagonal elements -pper and $o,er Triangular /atri printing Addition T su"traction of t,o arrays Transpose of a matri