Sie sind auf Seite 1von 10

# Part-A

Q1:- 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.

Aa data structure is a particular way of storing and organizing data in a computer so that it can
be used efficiently.

Different kinds of data structures are suited to different kinds of applications, and some are
highly specialized to specific tasks. For example, B-trees are particularly well-suited for
implementation of databases, while compiler implementations usually use hash tables to look up
identifiers.
data structure is a group of data elements grouped together under one name. These data
elements, known as members, can have different types and different lengths.
Importance of data structure:
a. Data structure is important since it describes the types of operations we can perform on
the data and how efficiently they can be carried out.
b. It also describes how dynamic we can be in dealing with our data.
c. We determine which data structures to use to store our data only after we have carefully
analyzed the problem.

EX:-
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 CD’s in a CD cover 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.
d) We cannot be able to locate the position of the element stored in a memory.

Q2:- Elaborate the concept of “Algorithm complexity” and Complexity notations with
suitable examples.
ALGORITHM:-
Algorithm is a step by step processing to solve a particular problem.
Or
An algorithm is a well-defined list of steps for solving a particular problem. The algorithm
developed must be efficient so that the data must be processed in an efficient manner. The time
and space the algorithm uses are two major issues for the efficiency of algorithm. The
complexity of an algorithm is the function which gives the running time and space in terms of
the input size.
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.
To obtain the complexity of an algorithm , we generally causes

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

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

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, InsertionSort

O(n3) Multiplication of Square Matrix

## O(an) Recursion, Towers of Hanoi

Ques.3
Consider the array NAME
Mary

Jane

Diana

Susan

Karen

Edith

(i) Write the Algorithm to delete an element from an array and also delete “Jane”
from the array NAME

Ans: -
a.1):- Algorithm to delete an element from an array:-
Lets take 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

## a.2) :- Algorithm to delete “Jane” from an array

Let NAME is an array with N elements
1. Start
2. Input array
3. Set item = “Jane”
4. Repeat for I = K to N-1
a. If ( NAME[I] == item)
i. NAME[I] = NAME[I+1]
End if
End loop
5. Set N = N-1
6. Exit

## b.1):- Algorithm to insert an element in an array

Lets take 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-1
End loop
5. Set NAME[K] = ITEM
6. Set N = N+1
7. Exit

## b.2) :- 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
End loop
5. Set NAME[K] = ITEM
6. Set N = N+1
7. Exit

Q4:- Let an array A contains 30 positive integers. Write algorithm which finds all pair of
elements whose sum is 25.
1. Start
2. Input array[30]
3. Repeat steps for I = 1 to 30
4. While ( A[I] != 0)
a. P = A[I] % 10
b. Sum = sum + p
c. A[I] = A[I] / 10
d. If(sum == 25)
i. Write A[I]
Endif
Endwhile
Endfor
5. Exit
Q5. Write a module which finds the number of students who have failed that is
whose final grade is less than 60.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,n,m;
clrscr();
printf("\n Enter the NUMBER of students : ");
scanf("%d",&n);
printf("\n Enter the total Marks of students");
for(i=0;i<n;i++)
printf(“\n Enter marks of %d student : ”,++i);
scanf("%d",&n);
for(i=0;i<n;i++)
printf("\n the marks of %d student == %d",++i,a[i]);
t=0;
for(i=0;i<n;i++)
{
if(a[i]<60)
m++;
}

## printf("\n Total no of students failed : %d",m);

getch();
}
OUTPUT
Enter the total number of student :10
Enter the marks for the 1 student : 11
Enter the marks for the 2 student : 40
Enter the marks for the 3 student : 70
Enter the marks for the 4 student : 99
Enter the marks for the 5 student : 77
Enter the marks for the 6 student : 88
Enter the marks for the 7 student : 32
Enter the marks for the 8 student : 56
Enter the marks for the 9 student : 72
Enter the marks for the 10 student : 39

## Total no of students failed : 4

Q.6 Write a program to sort the data using Bubble sort and
Insertion Sort technique.

## ANSWERS : PROGRAM FOR USING BUBBLE SORT

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,a[20],n,temp,pass;
clrscr();
printf(“\n enter total no of elements : ”);
scnaf(“%d”,&n);
printf(“\n Enter the elements : \n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
printf(“\n The elements are : \n”);
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
printf(“\n The sorted data is : \n”);
for(i=n-1;i>=0;i--)
{
pass++;
for(j=0;j<=i;j++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
for(i=0;i<n;i++)
printf(“\n %d”,a[i]);
printf(“\n\n Toatal no of passes = %d ”,pass);
getch();
}
OUTPUT :-
Enter total number of elements : 5
Enter the elements :
2
5
7
9
0
The sorted data is :
0
2
5
7
9
Total no pass = 4
Ans b :- Algorithm for the Insertion sort…
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,i,j,temp,k;
clrscr();
printf(“\n enter the no of elements : ”);
scanf(“%d”,&n);
printf(“\n Enter the elements for the array :\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);

for(i=0;i<n;i++)
{
temp=a[i];
j=n-1;
while((temp < a[j]) && (j>=0))
{
a[i+1]=a[i];
j=j-1;
}
a[j+1]=temp;
}
Printf(“\n Elements of array after sorting are : \n”);
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);

getch();
}
Enter total number of elements : 5
Enter the elements :
2
5
7
9
0
The sorted data is :
0
2
5
7
9