CS PDF
CS PDF
where,
LOC (LA[K]) is address ofKh element of LA
Wis number of words per memory location for LA
and LBis lower bound ie. smallest index of LA.
iv) The memory representation of an array is shown in figure below :
Memory
address LA
Base (LA)’ LA ||| = 0199
LA [2] = 0200 Elements are
LA 31 = 0201 stored sequentially
0202
Q. 14 Consider the array AUTO, Which records number of automobiles from 1932 through
1984. Suppose Base (AUTO) = 200 and W = 4 words. Then,
LOC (AUTO [1932]) =200, LOC(AUTO [1933]) =204
LOC (AUTO[1934]) = 208
Calculate address at which 1965s record is stored.
Ans.:
Given :- K = 1965
Base address = 200
W = 4
LB = 1932
The address of the array element for the year 1965 can be obtained -
LOC (AUTO(1965]) =Base (AUTO) +W(1965 - LB)
=200 + 4(1965 1932) =332
Q. 15 What is traversing an array ? Give the algorithm for traversing a linear array.
(Oct. 2005,12,15; March 2006, March 2018; July 18
Ans.
Traversing an array means accessing with each element of array only at once, so that it
can be processed.
Algorithm:Traversing a linear array.
algorithm
Here LA is a linear array with lower bound LB and upper bound UB. Following
apply operation PROCESS to each element of LA.
Step 1: [Initialize counter
set K:= LB
Step 2: Repeat steps 3 and 4 while K<UB:
2-11 Data Str
TPS Computer Science - I
[ResetN]
Step6: Set N:=N+1
TPSComputer Science - I Data Structu
2-12
Q. 18 Suppose a company keeps a linear array year, such that year [K] contains number of
employees in year K. Write a module for each of thefollowing tasks :
(a) To print each of the years in which no emnployee was born.
(b) To find the number N of years in which no employee was born.
(c) To find the number NL of employees, who will be atleast L years old at the end
of year 1984.
Linear array year contain elements from 1920 to 1970.
Ans. :
(a) To print each of the years in which no employee was born.
1 Repeat for K:= 1920 to 1970:
if year [K]= 0, then :
write: K
[End of If structure]
[End of loop]
2. Exit
(b) To find numberN of years, in which noemployee was born.
1 Set N:=0
2 Repeat for K= 1920 To 1970:
If year [K]= 0, then:
N:=N +1
[End of Ifstructure]
[End of loop]
3. Write:N
4. Exit
2-13
TrS Computer Science - 1
Data Structures
(c) To find number of employees NL, who will be at least Lyears old at the end of
we want the number ofemployees bornin year 1984-L or earlier.
1 Set NL :=(0
year 1984
2 Set X:= 1984 - L
3 Repeat For K = 1920 ToX:
Set NL:= NL + year (K]
|End of loopl
4 IWrite:NL
5 Exit
Q. 19 Explain Bubble sort algorithm with
suitable example.
(March 2002,05, 08, 12, 17, 20;
Ans.: Algorithm: Oct.2005, 200:
Bubble Sort (DATA, N)
Here DATA is a linear array with N
ascending order. elements. This algorithm sorts elements of DATA in
Step 1: Repeat steps 2 and 3for K
Step 2: Set
:=1To N-1:
Ptr :=1
Step 3: Repeat While Ptr
<N-K:
(a) If DATA [Ptr] >
DATA (Ptr +
DATA Ptr]and DATA Ptr + 1] 1], then interchange
|End of If structure]
(b) [increment
pointer]
Set ptr := ptr + 1
(End of inner loop]
|End of outer loop]
Step 4: Exit
Explanation :
Suppose DATA is an array of N
means arranging the elements suchelements. Sorting these
DATA (2]<=... <= that: elements in ascending orae
In []<=DATA
Bubble sort, DATA(N]
DATA[ 1]>DATA| 2).compare DATA|1] with
Next
is DATA[2] is DATA[2] and
exchange
repeated till compare with
One makes N-DATA| DATA|3). with
N -1] is compared They are
them if
l
comparisons,
After the first pass the exchanged necessary.
this is called a DATAJN).
if This process
During the next pass,largest element is sink topass.
element the last position.
moves to the
compare.elements
After N-1 passes, all (N-1)" position.
elements are sorted.
upto the last but
one and seçond largest
tures TPS Computer Science -| 2-14
Data Structures
Pass 1:
2008 (a) Compare DATA[1]with DATA(2] since 55 > 43.. exchanged
(55) (43) 5 6 9
. New list is 43 55 5 6 9
A in
(b) Next compare DATA[2] with DATA(3] since 55>5.. exchanged
43 55) 5 6 9
.. New list is 43 5 55 6
.. New list is 43 5 6 55
cess At the end of second pass, the second largest element 43 has maked to its proper
position.
Pass 3:
5 9 43 55 Since 5 <6.. No exchange
gest 5 (6 9 43 55 Since 6 <9.. No exchange
.:. New list is 5 6 43 55
Science-I
ComputTPeS r
Pass 4:
example: For Step5: Step4: Step3: Step2: LINEAR(DATA, Ans. : Q.
21
SteAlgorithm:Linear
p 1: theHere Search Linear Ans. Q.
location 20
DATA search
Linear(1)
In searching or : follows:
order asIn
Suppose
ITEM
=33 LOC:=0
Exit Set Write Forsearching Searching
check this
loop)
If
|Endof Repeat item]
fo|Initialize
LOC::1
Set (Search r counter
Set [Insert
Set LOC algorithm,linear searching What
LOC is
11
LOC:=LOC DATA a an search 5] 55 DATA||) way
DATA(2]
Given of N, whether DATA|DATA|
do4] 43
=N+ While ITEM linear algorithm algorithmalgorithms:Searching DATA[3] 09 after
ITEM ITEM,
DATA [N refer algorithms you
22 array the
1, DATA + at 06 complete
05
then: + 1]:= the in LOC) to
given required understand
1 DATA with for : as
33
array Q. means
|LOC] ITEM end No. follows ?
linear element (2) Explain
with
of or N
(March 21.
element
elements
DATA] sets to by execution
search Binary
search : find
44
following ITEM: =
LOC is is the the
2003, present out of
and techniquecompared linear term
=0, particular this 2-15
55 5 ITEM 07,
elements if searching
search 09; or searching algorith
is Oct. with with not
given element
is each
in(March
? n,
unsuccessful. 2007,
suitable an the
element. algorithm. Which
10,21, element array. from
array
2004,
example. Tlhere a are
This July given Oct. gets
of the
algorithm 2016, list are 2004,different sorted
list
one two Structure
Data
Dec. of
2010,2021 in
by elemente
types ascending
finds types
2020 one.
of
of
Step :1 Step6: Step5: Step4: Step3: Step 2: Step1: 22
Q.Step4: Step3:Step 2: Step 1: TPS
Ans.
Computer
Suppose 60 Given
11e.g. of Here Binary
Algorithm
:Binary
search Binary :
NULL,element. Write
DATA.
66 22 Exit [End Else:
DATA[MID] If
=
[End [End Else : set while
If RepeatVariables]Set
[Initialize DATA
Initially
and Hence LOC:=
NULL set Set Set Hence Since Since LOC 1= 11Set
Here
LOC:=MID END
ITEM if (DATA, search an DATA Science
so ITEM MID:= BEG:= BEG:= search BEG
DATA of of of BEG This DATA
DATA[MID] MID BEG 77 30 If step If
<DATA[MID), := steps is algorithm ITEM DATA DATA
= structurel structure] MID =
denotes aLB, is 22 (6] I-
INT MID LB, algorithm is sorted used
= 40 be 2 3
END unsuccessful. (3]=
= (2] ([1] =
INT(1 1= the loop] and UB, 33 33,
(BEG +1 -1 END;= = =
and 80 33 ITEM,
then: to found 33 22 11
following AND 4 beginning,arrayITEM, search for List
= + binary = 33
DATA13)/2] END + finds 33 33 33 becomes
END)/2) then:
DATAMID] UB with at =
88 40 LOC) anposition, LOC=2
= and ITEM .. .:.
13 sorted the MIDlower element search LOC=3
(7] =7
44
= 99 44 MID:= location 2-16
55 denotedbound technique LOC
13 ITEM from
element
55 INT
LOC.of =
(BEG middle LB sorted 3.
55
and with
array
ITEM andupper array.
END)/2) + examplOct.
:
END 33
in bound
DATA (Mar.2013,
denotes
UB.
2002,06,11,12,13)
orITEM Structures
Data
sets end 14,
location is 15,19;
LOC
given
= 22
TPS Computer Science - I 2-17
Data Structure
Step 2 : Since 40<55, END hasits value changed by
END = MID - 1l=7-1 =6
Hence MID =INT |(1 + 6)/2] =3
and so DATA|MID]= DATA[3] = 30
Step 3: Since 40> 30, BEG has its value changed by
BEG = MID + 1=3+1=4
Hence MID= INT (4 + 6)/2] = 5
and so DATA|MID]= DATA[5]= 40
.. Found ITEM in location LOC =MID =5
Q 23 Explain the advantages of binary search algorithm with asuitable
Ans. :
any two disadvantages or limitations of binary search. ((March 07,example.
19; Oct. 03Stat
Advantages of binary search algorithm
(1) Binarv search algorithm is efficient as the search scope gets
the array, with each iteration. reduced to half the size of
(2) The number of comparisons required are
than linear search. approximately equal to log2n which are less
(3) For example :
Given array data with 7-sorted
11 22 30
elements:
33 40 44 55
Suppose ITEM =40
Step I: Initially BEG =1 and END=7
MID =(BEG +END) /2 =(1
DATA [MID] = DATA[4]=33+7)/2=4
Step II : Since 33< 40,
BEG is changed as BEG =
MID =(5 +7) /2 =6 MID + 1= 4 + 1=5
DATA
Step III: Since 44[MID]
= DATA [6]= 44
>40, END has
its value
MID =(5 +5) / 2 =5 changed by END= MID
DATA [MID] = DATA (5]=40 -1=6-1=5
ITEM found at location 5 in array.
In above example, only two
calculated only one half is checked.
comparisons are required
In the same
example, for linear search, 5 because at each iteration MID
Disadvantages : comparison are required.
1) The given list must be
2) The access of list sorted.
must be
3) At each
iteration, middle randomn
entry means the middle
calculation is required.element can be accessed.