Está en la página 1de 7

Data structure

Assignment =1

CourseCode:CAP205

Submitted by
Priyanka
Sharma
Roll no rtb901a04
10902167

PART A
1. Q)Why structuring of data is required .Take one example
problem and discuss various problems that can be faced
if wrong choice has been made in selecting data structure
for the problem.
Ans: - A data structure is a logical/mathematical model of organization of data. The choice of
data structure depends upon the various considerations such as:
1. The data structure should be simple so that it can be efficiently processed when
required.
2. Data structure must be capable to represent the relationship of the data in real world.
Example
The stack is a data structure that is used to perform the following operations: -
 PUSH: For inserting new element in Data Structure.
 POP: For deleting element from Data Structure.
 TRAVERSE: For List out the elements of Data Structure.
If we want to place plates in a rack in then we have to use the stack data structure. The stack is
used because we have to pick the last item placed first. If we choose the wrong data structure
then the following problems must be faced: -

a. The first problem we faced is the wastage of the memory space.


b. The time utilize to access that data must be wasted because the data is not stored in a
particular manner.
c. The accessing of data must be difficult because data is not stored at particular location.
We cannot be able to locate the position of the element stored in a memory

2. Q)Delineate the concept of abstract data type in data


structures.

Ans: - Abstract Data Type: -To understand the notion Abstract data type, it is necessary to
understand the concept of abstract and data type separately. Abstract in our context stands for
considering apart from the detailed specification or implementation. Abstraction refers to the act
of representing the essential features without including the details. Data type is the set of values
and set of operations that are permissible on those values.
Abstract data types uses the following principles:
Encapsulation : Providing the data and operations on the data.
Abstraction: hiding the details e.g. a class in c++ or java language exhibits what it does
through its method but the detail of how methods work is hidden from the user.
The various abstract data types are stack ,queue, tree etc. the stack is an abstract data type
as two operations push and pop are performed on the stack without knowing the detail of
whether stack is implemented using array and linked list.

3Q)List out Applications areas of various data structure.

Ans: - The various application areas of data structure are as follows: -


1.The data structure is used to design the compiler. The compiler is that which is used to
translate the high level language into low level language. When we design the compiler the data
structures are used by the designer to implement the compiler.
2.The databases maintained in organizations are made with the help of data structures. The
various data structures like arrays, linked lists are used to maintain the database.
3. The data structure is also used in the C language to implement various programs. We can be
able to store the elements using linked list, arrays, stacks and queues.
Part-B

3. Elaborate the concept of “Algorithm complexity” and


Complexity notations with suitable examples.
Ans: - : Complexity of an algorithm:
The complexity of an algorithm is a function f(n) which measures the time and space used by by
an algorithm in term of input size n.

To obtain the complexity of an algorithm , we generally causes

(i) Worst case: the maximum value of f(n) for any possible input.

(ii) Average Case: the expected value of f(n).

(iii) Best Case: the minimum value of f(n).

The various data structures are used to solve a problem. So, 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 tradeoff:
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.
Example with algorithm notations
O(1) Push and Pop Operations
O(n) Linear Search, Factorial
O(log2n) Binary Search, Insertion in a Tree
O(nlog2n) Quick Sort, Merge Sort, Heap Sort
O(n2) Bubble Sort, Selection Sort, Insertion Sort
O(n3) Multiplication of Square Matrix
O(an) Recursion, Towers of Hanoi
Complexity notations: a function f(n) is defined to be O (g(n)), that is

f(n)=O(g(n))

and is read as order of g(n)’,if there exists positive constants n0 and k such that

| f(n) |<= k * | g(n) | for all n>n0

4. Consider the array NAME

Mar
y
Jane (i)Write the Algorithm to delete an
element from an array and also delete
Dian
jane from the array NAME.
a
(ii) Write the algorithm to INSERT an
Susa
element in an algorithm and also add
n
‘ABC’ into the array NAME.
Kare
n
Edit
h
Ans: - Algorithm to delete an element from an array
Let NAME is an array with N elements and K is the position from where we want to
delete an element.
1. Start
2. Input array, item
3. Set item=NAME[K]
4. Repeat for I = K to N-1
a. NAME[I] = NAME[I+1]
[end of loop]
5. Set N = N-1
6. Exit

Algorithm to delete “Jane” from an array


Let A is an array with N elements
step1: start
step2:set item=A[k]
Step3: repeat for i=k to n-1
A[i]=A[j+1]
End of for loop
Step4 : n=n-1;
Step5: exit.

Algorithm to insert an element in an array


Let NAME is a linear array with N elements and K is a positive integer such that K<= N.
This algorithm inserts an element ITEM into the Kth position.
1. Start
2. Input array
3. Set J = N
4. Repeat Steps while J >= K
a. Set NAME[J+1] = NAME[J]
b. J = J-1ss
End loop
5. Set NAME[K] = ITEM
6. Set N = N+1
7. Exit

Algorithm to insert an element “ABC” in an array


Let NAME is a linear array with N elements and K is a positive integer such that K<= N.
This algorithm inserts an element ITEM into the Kth position.
1. Start
2. Input array
3. Set J = N, ITEM= “ABC”
4. Repeat Steps while J >= K
a. Set NAME[J+1] = NAME[J]
b. J = J-1
endloop
5. Set NAME[K] = ITEM
6. Set N = N+1
7. Exit

5. An array A contain 30 positive integers .Write algorithm


which find all pair of elements whose sum is 25.

Ans: - Algorithm to find all pair of elements whose sum is 25


1. Start
2. Input array[10]
3. Repeat steps for i = 1 to 30
4. While ( A[i] != 0)
a. j = A[i] % 10
b. Sum = sum + j
c. A[i] = A[i] / 10
d. If(sum == 25)
i. Write A[i]
Endif
Endwhile
Endfor
5. Exit

6. For Q5Write a module which finds the number of students


who have fail that is whose final grade is less than 60.

Ans)- Algorithm to the number of students who have failed whose grade is less than 40

1. Start

2. Input n, array
3. Repeat steps for i= 1 to n

4. If ( A[i] < 40)

count= count+1

Endif

Endfor

5. Write “ count”

6. Exit

=========================================================

También podría gustarte