Você está na página 1de 6

LOVELY PROFESSIONAL UNIVERSITY

HOME WORK: #1
Course No: CSE205 Course Title: DATA
STRUCTURES
School: LITCA
Department: CSE/IT
Name of the faculty member: Balwant Ram
Class: K1902 Term: Jan-May Section:
K1902 Batch: 2nd
Max. Marks: 15 DOA: 22-01-11 DOS: 1-02-11

Name-Naveen Kumar Mahaur


Class- B.tech
Section-K1902
Roll no –RK1902A06

Part-A

Q.1. How do we improve the time and space efficiency of a program?


Explain with some suitable example

ANS.
Accordingly, we may not always be able to use the most efficient
algorithm, since the choice of data structure depends on many things,
including the type of data and the frequency with which various data
operations are applied. Sometimes the choice of data structure involves a
time-space trade off: by increasing the amount of space for storing the
data, one may be able to reduce the time needed for processing the data,
or vice versa.
Suppose a file of records contains names, social security numbers and
much additional information among its fields. Sorting the file
alphabetically and rising a binary search is a very efficient way to find the
record for a given name. On the other hand, suppose we are given only
the social security number of the person. Then we would have to do a
linear search for the record, which is extremely time-consuming for a
very large number of records. One way is to solve that is to have another
file which is sorted numerically according to social security number. This
however, would double the space required for sorting the data. Another
way, is to have the main file sorted numerically by social security number
and to have an auxiliary array with two columns, the first column
containing an alphabetized list of the names and the second column
containing pointers which give the problem that is used frequently, since
the additional space, containing only two columns, is minimal for the
amount of extra information it provides.

Q.2. Two different procedures are written for a given problem. One
has a computing time given by 2n and that for the other is n2. Specify
the range of n for each would be suitable.

ANS.
Two different procedures are written for a given problem. One has a
computing time given by 2n and here the exponential function grows
most rapidly. Other is n2 , the complexity of certain well-known
searching and sorting algorithms , Bubble sort : O(n2)
The complexity of an algorithm M is the function f(n) which gives the
running time and/or storage space requirement of the algorithm in
terms of the size n of the input data. The storage space required by an
algorithm is simply a multiple of the data size n. According, unless
otherwise stated or implied, the term “complexity” shall refer to the
running time of the algorithm.
1) Worst case: the maximum value of f(n) for any possible input
2) Average case: the expected value of f(n)
Sometimes the minimum possible value of f(n), called the best
case.
n2 2n
25 32
100 1000
10000 10^30
1000000 10^300

Q.3. Analyze and find out the basic steps for the generation of a new
data structure.

ANS.
Data may be organized in many different ways, the logical or
mathematical model of a particular organization of data is called data
structure. The choice of a particular data model depends on two
considerations. First, it must be rich enough in structure to mirror
relationships of the in the real world. On the other hand, the structure
should be simple enough that one can effectively process the data when
necessary.
Collections of data are frequently organized into a hierarchy of fields,
records and files. Organization of data into fields, records and files may
not be complex enough to maintain and efficiently process certain
collections of data. For this reason, data are also organized into more
complex types of structure. The study of such data structure , includes the
following three steps:
1. Logical or mathematical description of the structure
2. Implementation of the structure on a computer
3. Quantitative analysis of the structure, which includes determining
the amount of memory needed to store the structure and the time
required to process the structure.

Q.4. Which function grows faster?


nn or log nn
ANS.

nn is grows faster than log nn

Part-B

Q.5. Generate an algorithm to find out the maximum and minimum


from an array of integers.

ANS.
Given a nonempty array DATA with N numerical values, this
algorithm the location LOC and the value MAX of the largest element
of DATA and the value MIN of the minimum element of DATA.
1. [Initialize] Set K:=1, LOC:=1 MAX:=DATA[1] and
MIN:=DATA[1].
2. Repeat steps 3 & 4 while K<=N:
3. If MAX<DATA[K], then:
Set LOC:=K and MAX:=DATA[K],
[end of if structure]
4. Set K:=K+1.
[end of step 2 loop]
5. Write: LOC,MAX.
6. Repeat steps 7 & 8 while K>=N:
7. If MIN>DATA[K], then:
Set LOC:=K and MIN:=DATA[K],
[end of if structure]
8. Set K:=K+1.
[end of step 6 loop]
9. Write: LOC,MIN.

Q.6. There are two arrays A and B and C. A contains 20 elements


whereas B contains 30 and C contains 10 elements. Write a procedure
to create an array D that contains only those elements that are
common to A and B and C.

ANS.

First of all we declare three array named A,B and C as given in the
question . than by applying for loop we traverse every element of array
A,B and C and compare them by if statement .than put them in seprate
array named D.

Q.7. Consider the following multidimensional arrays:


A=(2:22, -9:6,10:10) B=(6:20, 2:5, 15:20, 20:10)
a) Find the length of each dimension and number of elements in A
and B.
ANS.

The length of the three dimension of A are,


L1 = 22 – 2 + 1 = 21

L2 = 6 – (-9) + 1 =16

L3 = 10 – 10 + 1 =1

Accordingly, A contains
L1*L2*L3 = 21*16*1 = 336 elements

The length of the four dimension of B are,

L1 = 20 – 6 + 1 = 15

L2 = 5 – 2 + 1 = 4

L3 = 20 – 15 + 1 = 6

Accordingly, B contains
L1*L2*L3*L4 = 15*4*6 = 360 elements

b) Suppose Base(B)=6010 and there are w=4 words per memory


location. Find the effective indices E1, E2, E3 and the address
of B[8, 4, 17].

ANS.
The effective indices of the subscripts are,
E1 = 8 – 6 =2,
E2 = 4 – 2 = 2,
E3 = 17 – 15 = 2
Using
Base(C) + w[(…((E1L2 + E2)L3 + E3)L4 +…+ EN-1)LN + EN]

E1L2 = 2*4 =8
E1L2 + E2 = 8 + 2 = 10
(E1L2 + E2) L3 =10*6 =60
(E1L2 + E2) L3 + E3 = 60 + 2 = 62

Therefore ,

LOC(B[8,4,17]) =6010 + 4(62) =200 + 248 = 448

Q.8. Compare and contrast pointers and array pointers.

ANS.

POINTERS ARRAY PONITERS


1. A pointer is a memory 1. Array pointer means a
variable that stores a pointer is made for an
memory address. array.
2. Pointers save the memory 2. Pointer arrays offer a
space. particularly convenient
method for storing strings.
3. Execution time with pointer 3. In this situation, each array
is faster because data is element is a character-type
manipulated with the Pointer that indicates the
address i.e. direct access to beginning of a separate
memory location. string.
4. The memory is accessed
efficiently with the pointers. 4. Each individual string can
The pointer assigns the be accessed by referring to
memory space and it also its corresponding pointer.
releases. Dynamically
memory is allocated. 5. Pointer contains the
5. Pointers are used with data locations of the different
structure. They are useful groups or, more especially,
for representing two- the locations of the first
dimensional and multi- elements in the different
dimensional arrays. groups.