Test-1 Answer Key
Test-1 Answer Key
Test-1 Answer Key
Answer:
Q2. Arrange the following sequence of records in ascending order using external sorting (2-
way merge sort) 17, 3, 29, 56, 24, 18, 4, 9, 10, 23.
Q3. Given an array of integers 45, -33, 28, 19, 11, -77, 20, -52, arrange them in ascending
order using shell sort and display the order of elements and number of comparisons after
every iteration?
Answer:
Pass 1 : Interval = 4 No.of Sublist = 4 and No .of Camparison =4
Q4. Neha is interested in playing card game. She wants to arrange the cards in ascending
order with minimum number of comparisons and swaps. Help her by implementing a c
program with insertion sorting technique?
Answer:
#include<stdio.h>
void ShellSort(int a[], int n)
{
int i, j, increment, tmp;
for(increment = n/2; increment > 0; increment /= 2)
{
for(i = increment; i < n; i++)
{
tmp = a[i];
for(j = i; j >= increment; j -= increment)
{
if(tmp < a[j-increment])
a[j] = a[j-increment];
else
break;
}
a[j] = tmp;
}
}
}
int main()
{
int i, n, a[10];
printf("Enter the number of elements :: ");
scanf("%d",&n);
printf("Enter the elements :: ");
for(i = 0; i < n; i++)
{
scanf("%d",&a[i]);
}
ShellSort(a,n);
printf("The sorted elements are :: ");
for(i = 0; i < n; i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
Q5. Mr. Darvin is working as a full-time scholar in IIT Bombay. He is doing research on
quick sort with given numbers and completed the first iteration successfully by placing first
pivot element in correct position. Suddenly, he went for shopping along with his wife and
left the sequence as it is [ 212, -25, 221, 122, 512, 978, 521, 876, 768]. His daughter Alexa
seen the sequence and she is trying to arrange them in sorted order. Help Alexa to find the
first pivot element which is placed by Darwin and trace the sequence to arrange the
numbers in ascending order accordingly.
Answer:
In the above problem the input is [ 212, -25, 221, 122, 512, 978, 521, 876, 768],
the middle element is being chosen as pivot i.e. 512. After first iteration, all elements lesser than
it are moved to left and greater than it are moved to it's right.
Answer:
void merge(int a[], int beg, int mid, int end)
{
int i, j, k;
int n1 = mid - beg + 1;
int n2 = end - mid;
int LeftArray[n1], RightArray[n2]; //temporary arrays
for (int i = 0; i < n1; i++)
LeftArray[i] = a[beg + i]; //copy data to temp arrays
for (int j = 0; j < n2; j++)
RightArray[j] = a[mid + 1 + j]; // copy data to temp arrays
i = 0;
j = 0;
k = beg;
while (i < n1 && j < n2)
{
if(LeftArray[i] <= RightArray[j])
{
a[k] = LeftArray[i];
i++;
}
else
{
a[k] = RightArray[j];
j++;
}
k++;
}
while (i<n1)
{
a[k] = LeftArray[i];
i++;
k++;
}
while (j<n2)
{
a[k] = RightArray[j];
j++;
k++;
}
}
void mergeSort(int a[], int beg, int end)
{
if (beg < end)
{
int mid = (beg + end) / 2;
mergeSort(a, beg, mid);
mergeSort(a, mid + 1, end);
merge(a, beg, mid, end);
}
}
void Result(int a[], int n) // Display the result
{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}
int main()
{
int a[100] ,i,n;
printf(“ Enter the size of corps “);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“ \n Enter the height of corps member \n“);
scanf(“%d”,&a[i]);
}
printf(" Before Arranging \n");
Result(a, n);
mergeSort(a, 0, (0+n-1)/2);
printf ("After Arranging \n");
Result(a, n);
}
Time Complexity:
void dequeue()
{
struct Queue *ptr;
if(front==NULL) // queue is empty
{
printf("Queue is empty\n");
}
else if(front==rear) // queue is with single node
{
ptr = front;
front = NULL;
rear = NULL;
printf("Dequeued node is %d\n", ptr->data);
free(ptr);
}
else // queue with multiple nodes
{
ptr = front;
front = front->next;
ptr->next = NULL;
printf("Dequeued node is %d\n", ptr->data);
free(ptr);
}
display();
}
void enqueue()
{
int key;
printf("\nEnter element\n");
scanf("%d", &key);
if(r==SIZE-1&&f==0 || r==f-1)
{
printf("\nQueue overflow\n");
}
else if(f==-1 && r==-1)
{
f = f+1;
r = r+1;
queue[r] = key;
}
else if(r==SIZE-1 && f>0)
{
r = 0;
queue[r] = key;
}
else
{
r = r+1;
queue[r] = key;
}
display();
}
Q9. Display the final popped out element in given scenario, If the seven elements A, B, C,
D, E, F and G are pushed into a stack in reverse order, i.e., starting from G. The stack is
popped five times and each element is inserted into a queue. Then two elements are
dequeued from the queue and pushed back into the stack. Now, one element is popped
from the stack.
Answer:
The final popped out element is B.
Q10. Write a C program to implement the following operations on doubly linked list?
a. Give the node structure
b. Delete an element at the given position
c. Insert an element at the given position
Answer:
Q11. Wrote a C program to convert the given infix expression into postfix expression. And
trace that program by converting the given infix expression (a +b) * (c – ((d ^ e) ^ f)) into
postfix expression. Assume that the operators +, -, × are left associative and ^ is
right associative. The order of precedence (from highest to lowest) is ^, x, +, -.
Answer:
Program:
#include<stdio.h>
#include<ctype.h>
char stack[100];
int top=-1;
top++;
stack[top]=ch;
char pop()
if(top == -1)
return -1;
else
return stack[top--];
if(ch == '^')
return 3;
return 2;
return 1;
else
return 0;
for(int i=0;ch[i]!='\0';i++)
if(isalnum(ch[i]))
printf("%c",ch[i]);
push(ch[i]);
{
char x;
printf("%c",x);
else
printf("%c",pop());
push(ch[i]);
printf("%c",pop());
int main()
char inputEx[100];
scanf("%s",inputEx);
convertPost(inputEx);
return 0;
Q12. Mr. Rajesh is CEO of an organization ‘Software Solutions’. He wants to maintain the
personal information of his employee’s like Ename (Employee Name), Eno (Employee Number),
and salary using singly linked list. So, help him to store and organize the data with following
operations.
a. Write a function to read and maintain employee data
b. Write a function that reads Eno (Employee Number) and then increase 10% salary.
c. Write a function that reads Ename (Employee Name), search and display the employee
details.
Answer:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void create();
void per();
void search();
void Display();
void per();
char name[100];
int salary;
}s;
s *head=NULL,*tail=NULL,*p,*q,*k;
void main()
int opt;
do{
scanf("%d",&opt);
switch(opt)
{
case 1:
create();
break;
case 2:
per();
break;
case 3:
search();
break;
case 4:
Display();
break;
}while(opt>=1&&opt<=4);
void create()
int x,n,i;
char N[100];
int sal;
printf("enter no of nodes\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
p=(s*)malloc(sizeof(s));
fflush(stdin);
printf("enter name\n");
scanf("%s",N);
strcpy(p->name,N);
printf("enter id no\n");
scanf("%ld",&Eno);
p->num=Eno;
printf("enter salary\n");
scanf("%d",&sal);
p->salary=sal;
p->next=NULL;
if(i==1)
head=tail=p;
else
tail->next=p;
tail=p;
void Display()
if(head==NULL)
printf("list is empty\n");
else
for(p=head;p!=NULL;p=p->next)
printf("%s\n",p->name);
printf("%ld\n",p->num);
printf("%d\n",p->salary);
void per()
int z;
printf("enter idno\n");
scanf("%ld",&ino);
p=head;
while((p->num)!=ino)
p=p->next;
z=(p->salary)+(p->salary*10)/100;
printf("%d",z);
}
void search()
int flag=0;
char search[100];
scanf("%s",search);
for(p=head;p!=NULL;p=p->next)
if(strcmp(p->name,search)==0)
flag=1;
break;
if(flag==1)
printf("name found");
else