Вы находитесь на странице: 1из 43

DATA STRUCTURES

P.A.Bharathi PGT ComputerScience Jain International Residential School

What is a data structure?


A logical method of representing data in memory using the simple and comple data types pro!ided "y a programming language. Simple data structures are "uilt#in data types. Comple data structures are made up of simple data types.

Types of Data Structures


$inear %ata Structures
& 'lements in se(uence & ' amples are stac)s* (ueues* lin)ed list

+on#linear %ata Structures


& 'lements not linear in fashion. & ' amples are trees and graphs

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.

Address Calculation in Ro!ma"or #ethod


$et i* F denote the ro, and column inde of array A3$B;1-B;* $B=1-B=5* ,here $B; 89 i 89 -B; and $B= 89 F 89 -B=. Address of an element 9 B 0 >no. of elts "efore it? @ S ,here B is "ase address and S is si7e of each element. +um"er of ro,s m 9 >-B; & $B; 0 ;? +um"er of columns n 9 >-B= & $B= 0;? -sing ro, maFor order* the address of a3i53F5 is gi!en "y1 Address of a3i53F5 9 B 0 >+um"er of elts "efore a3i53F5? @ S 9 B 0 >+um"er of elts in >i & $B;? ro,s 0 +um"er of elts in ith ro, "efore col F? @S 9 B 0 3>i & $B;?@+ 0 >F#$B=?5 @ S

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

Column ma"or representation


This lineari7ation techni(ue stores first the first column* then the second column and so on. The formula for address calculation of >I*J?th element in array I3$r1-r* $c1-c5 ,ith "ase address B and element si7e , is 1 Address>I*J? 9 B 0 ,3r>J#$c? 0 >I#$r?5 Jhere r is the num"er of ro,s.

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.

& Binary Search


Searches the gi!en item in minimum possi"le comparisons. This techni(ue re(uires that the array "e sorted. The item is searched for in smaller segment after e!ery stage.

%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

%imitations of %inear Search


Time consuming +um"er of comparisons needed is more. The e ecution time is proportional to the si7e of the array.

&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

Tra'ersal in a (-D array


It means !isiting each element from start to end one after the other. Algorithm1 $et A "e a collection of data elements stored in the memory of the computer. Suppose ,e ,ant to print the contents of each element of A . This can "e accomplished "y tra!ersing A. The follo,ing algorithm tra!erses a linear array $A. Lere $A is a linear array ,ith lo,er "ound $B and upper "ound -B. This algorithm tra!erses $A applying an operation PR.C'SS to each element of $A. ;. >Initiali7e counter? Set P 9 $B =. Repeat steps E and D ,hile P 89 -B. E. 3Gisit element5 Apply PR.C'SS to $A3P5. D. 3Increase counter5 Set P 9 P 0 ;. 3'nd of Step = loop5 <. ' it. Program ' e

)nsertion into an Array


$et A "e an array of si7e + ha!ing / elements >/ 8 +? %ATA is the element to "e inserted at position P.S. Oor insertion* elements from positions /* /#;* /#= Q.. P.S are shifted do,n,ard "y one position and the element %ATA is inserted at position P.S. +um"er of elements is /0;.

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

Deletion from an array


%'$'T'>$A* +* P* IT'/? Lere $A is a linear array ,ith + elements ,ith + elements and P is a positi!e integer such that P 89 +. this algorithm deletes the Pth element from $A. ;. Set IT'/ 9 $A3P5. =. Repeat for J 9 P to + #; E. 3/o!e J0;st element up,ard5 Set $A3J5 9 $A3J0;5 3'nd of $oop5 D. 3Reset the num"er of elements in $A5 Set + 9 + #; <. ' 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

&u**le Sort Example


=3<<les#rt =3<<les#rt #mpares #mparesthe then3m<ers n3m<ersin inpairs pairs!r#m !r#mle!t le!tt# t#ri$ht ri$ht e8 han$in$ when ne essary& Here the !irst n3m<er is #mpared e8 han$in$ when ne essary& Here the !irst n3m<er is #mpared t# t#the these se #nd #ndand andas asit itis islar$er lar$erthey theyare aree8 e8 han$ed& han$ed& N#w the ne8t pair #! n3m<ers are #mpared& N#w the ne8t pair #! n3m<ers are #mpared& A$ain A$ainthe the::is isthe the lar$er and s# this pair is als# e8 han$ed& lar$er and s# this pair is als# e8 han$ed&

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,

12, 12, 12, 12, 11, 11, 11, 11,

11, 9, 3, 7 11, 9, 3, 7 11, 9, 3, 7 11, 9, 3, 7 12, 9, 3, 7 9, 12, 3, 7 9, 3, 12, 7 9, 3, 7, 12

&u**le Sort Example

6, 2, 9, 11, 9, 3, 7, 12 Second Pass 6, 6, 2, 2, 9, 9, 11,3, 11, 9,7, 11, 3,11, 7, 12


N#ti N#ti eethat thatthis thistime timewe wed# d#n#t n#tha%e ha%et# t# #mpare #mparethe thelast lasttw# tw# n3m<ers as we >n#w the 1/ is in p#siti#n& This pass there!#re n3m<ers as we >n#w the 1/ is in p#siti#n& This pass there!#re#nly #nly re?3ires 6 #mparis#ns& re?3ires 6 #mparis#ns&

First Pass

&u**le Sort Example

6, 2, 9, 11, 9, 3, 7, 12 Second Pass 2, 6, 9, 9, 3, 7, 11, 12 Third Pass 2, 6, 9, 3, 9, 7, 3, 9, 9, 7, 11, 12


This Thistime timethe the11 11and and1/ 1/are arein inp#siti#n& p#siti#n& This Thispass passthere!#re there!#re#nly #nly re?3ires re?3ires55 #mparis#ns& #mparis#ns&

First Pass

&u**le Sort Example

6, Second Pass 2, Third Pass 2, Fourth Pass 2,

First Pass

2, 6, 6, 6,

9, 9, 9, 9, 3,

11, 9, 3, 7, 9, 3, 7, 11, 3, 7, 9, 11, 3, 9, 9, 7, 7, 9, 11,

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&

&u**le Sort Example

6, Second Pass 2, Third Pass 2, Fourth Pass 2, Fifth Pass 2,

First Pass

2, 6, 6, 6, 6, 3,

9, 9, 9, 3, 3, 6,

11, 9, 3, 7, 9, 3, 7, 11, 3, 7, 9, 11, 7, 9, 9, 11, 7, 9, 9, 11,

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&

&u**le Sort Example

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,

11, 9, 3, 7, 9, 3, 7, 11, 3, 7, 9, 11, 7, 9, 9, 11, 7, 9, 9, 11, 7, 9, 9, 11,

12 12 12 12 12 12

Sorting Insertion Sort


- Assume

that the elements are numbered !si"e-1 the correct

- #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

Sorting Insertion Sort


+ 6 6 2 2 2 2 6 8 8 6 6 6 6 3, 3, 34 8 8 8 8 2 2 2 34 34 32 21 -1 -1 -1 -1 51 34 32 32 32 32 32 32 51 34 21 21 21 21 21 21 51 ori.inal $ass 1 $ass 2 $ass 3 $ass , $ass $ass 6

Sorting Selection Sort


-

Assume the elements are numbered from !si"e1

- 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

Sorting Selection Sort


+ 2 2 2 2 2 2 6 6 6 6 6 6 6 3, 3, 3, 8 8 8 8 2 + + 3, 21 21 21 -1 -1 -1 -1 -1 32 32 32 32 32 32 32 -1 34 21 21 21 21 3, 3, -1 ori.inal $ass 1 $ass 2 $ass 3 $ass , $ass $ass 6

Sorting Selection Sort


#ach loo$ executes at most 1 2or si"e3 times, therefore this sort is 421 23% 'f the arra5 is arra5 is alread5 sorted, then this al.orithm is still 421 23%

#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

Вам также может понравиться