GNIOT - Lab Manual Format
GNIOT - Lab Manual Format
Prepared by
Mr. Arjun Singh
➢ Syllabus
➢ Practical Objectives
➢ Mapping of Program Outcomes with Practical Objectives
➢ Mapping of Program Educational Objectives with Program Outcomes
➢ Session Plan
➢ Laboratory policies & Report format
➢ Evaluation sheet
➢ Each experiment should be with post lab questions with answer key
Syllabus:
2. Problem analysis: Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics, natural sciences,
and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems and design
system components or processes that meet the specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and environmental considerations.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
engineering and IT tools including prediction and modeling to complex engineering activities with an
understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to assess
societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering solutions
in societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of
the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or leader in
diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write effective reports and
design documentation, make effective presentations, and give and receive clear instructions.
12. Life-long learning: Recognize the need for, and have the preparation and ability to engage in
independent and life-long learning in the broadest context of technological change.
CREDIT: 1
LOCATION: LAB-13
2. The lab reports will be written individually. Please use the following format for your lab
reports.
a. Cover Page Include your name, Subject Code, Subject title, Name of the
College
3. Your work must be original and prepared independently. However, if you need any guidance or
have any questions or problems, please do not hesitate to approach your staff in charge. The
students should follow the dress code in the Lab session.
5. Reports Due Dates: Reports should be submitted immediately after next week of the
experiment. A late lab report will have 20% of the points deducted for being one day late. If a
report is 3 days late, a grade/marks of D/0 will be assigned.
Brief Theory:-A binary search will start by examining the middle item. If that item is the one we
are searching for, we are done. If it is not the correct item, we can use the ordered nature of the
list to eliminate half of the remaining items. If the item we are searching for is greater than
the middle item, we know that the entire lower half of the list as well as the middle item can be
eliminated from further consideration. The item, if it is in the list, must be in the upper half.
We can then repeat the process with the upper half. Start at the middle item and compare
it against what we are looking for. Again, we either find it or split the list in half, therefore
eliminating another large part of our possible search space.
Procedure:-Given an array A of n elements with values or records A0 ... An−1 and target
value T, the following subroutine uses binary search to find the index of T in A.
1. Set L to 0 and R to n − 1.
2. If L > R, the search terminates as unsuccessful. Set m (the position of the middle element) to the
floor of (L + R) / 2.
3. If Am < T, set L to m + 1 and go to step 2.
4. If Am > T, set R to m − 1 and go to step 2.
5. If Am = T, the search is done; return m.
Program:-
#include<stdio.h>
int main(){
int a[10],i,n,m,c,l,u;
return 0;
}
int mid,c=0;
if(l<=u){ mid=(l+u)/2;
if(m==a[mid]){
c=1;
}
else if(m<a[mid]){
return binary(a,n,m,l,mid-1);
}
else
return binary(a,n,m,mid+1,u);
}
else
return c;
}
Result:-
Unsuccessful searches
Θ(1) Θ (logn) Θ (Logn)
best average worst
Θ (logn)
Brief Theory:- Linear search or sequential search is a method for finding a target value
within a list. It sequentially checks each element of the list for the target value until a
match is found or until all the elements have been searched. Linear search runs in at worst linear
time and makes at most n comparisons, where n is the length of the list
Procedure:-
1. LinearSearch(value, list)
2. if the list is empty, return item_not_found;
3. else
4. if the first item of the list has the desired value, return its location;
5. else return LinearSearch(value, remainder of the list)
Program:-
#include<stdio.h>
int main(){
int a[10],i,n,m,c=0;
printf("Enter the size of an array: ");
scanf("%d",&n);
printf("Enter the elements of the array: ");
for(i=0;i<=n-1;i++){
scanf("%d",&a[i]);
}
printf("Enter the number to be search: ");
scanf("%d",&m);
for(i=0;i<=n-1;i++){
if(a[i]==m){
c=1;
break;
}
}
if(c==0)
printf("The number is not in the list");
else
printf("The number is found");
return 0;
Results:
Enter the size of an array: 5
Enter the elements of the array: 4 6 8 0 3
Enter the number to be search: 0
The number is found
Viva-voce Questions
Brief Theory:- Insertion sort is a simple sorting algorithm that builds the final sorted array (or
list) one item at a time. It is much less efficient on large lists than more advanced
algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several
advantages:
Efficient for (quite) small data sets, much like other quadratic sorting algorithms
More efficient in practice than most other simple quadratic (i.e., O(n2)) algorithms such
as selection sort or bubble sort
Adaptive, i.e., efficient for data sets that are already substantially sorted: the time complexity is
O(nk) when each element in the input is no more than k places away from its sorted position
Stable; i.e., does not change the relative order of elements with equal keys
Procedure:-
1. for i ← 1 to length(A)-1
2. j←i
3. while j > 0 and A[j-1] > A[j]
4. swap A[j] and A[j-1]
5. j←j–1
6. end while
7. end for
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,t,a[100],d,n;
printf("Enter The No. Of Elements\n");
scanf("%d",&n);
printf("Enter %d Integers\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
Department of Information Technology
for(i=1;i<=n-1;i++)
{
d=i;
while(d>0&&a[d]<a[d-1])
{ t=a[d];
a[d]=a[d-1];
a[d-1]=t;
d--;
}
}
printf("Sorted List in Ascending Order is:\n");
for(i=0;i<=n-1;i++)
{
printf("%d\t",a[i]);
}
getch();
}
Results:
Enter the No. of Elements: 5
Enter 5 integers: 11, 1, 14, 9, 4
Sorted List in Ascending Order Using Insertion Sort is: 1, 4, 9, 11, 14
Viva-voce Questions
Brief Theory:- Merge sort is based on the divide-and-conquer paradigm. Its worst- case
running time has a lower order of growth than insertion sort. Since we are dealing with sub
problems, we state each sub problem as sorting a sub array A[p .. r]. Initially, p = 1 and r = n, but
these values change as we recurs through sub problems. To sort A[p .. r]:
1. Divide Step
If a given array A has zero or one element, simply return; it is already sorted. Otherwise, split A[p
.. r] into two subarrays A[p .. q] and A[q + 1 .. r], each containing about half of the elements of A[p
.. r]. That is, q is the halfway point of A[p .. r].
2. Conquer Step
Conquer by recursively sorting the two subarrays A[p .. q] and A[q + 1 .. r].
3. Combine Step
Combine the elements back in A[p .. r] by merging the two sorted subarrays A[p .. q] and A[q + 1
.. r] into a sorted sequence. To accomplish this step, we will define a procedure MERGE (A, p, q,
r).
Procedure:-
To sort the entire sequence A[1 .. n], make the initial call to the procedure MERGE-SORT (A, 1,
n).
MERGE-SORT (A, p, r)
Program:-
Results:
Enter the No. of Elements to be sorted: 7
Enter the elements to be sorted: 11, 1, 14, 4, 18, 16, 9.
Sorted List in Ascending Order Using Merge Sort is: 1, 4, 9, 11, 14, 16, 18.
The algorithm divides the input list into two parts: the sublist of items already sorted, which is built
up from left to right at the front (left) of the list, and the sublist of items remaining to be sorted that
occupy the rest of the list. Initially, the sorted sublist is empty and the unsorted sublist is the entire
input list. The algorithm proceeds by finding the smallest (or largest, depending on sorting order)
element in the unsorted sublist, exchanging (swapping) it with the leftmost unsorted element
(putting it in sorted order), and moving the sublist boundaries one element to the right.
Procedure:-
SELECTION_SORT (A)
for i ← 1 to n-1 do
min j ← i;
min x ← A[i]
for j ← i + 1 to n do
If A[j] < min x then
min j ← j
min x ← A[j]
A[min j] ← A [i]
A[i] ← min x
Program:-
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,position,swap,a[100],d,n;
printf("Enter The No. Of Elements\n");
Results:
Enter the No. of Elements to be sorted: 5
Enter 5 integers: 70, 12, 50, 20, 10
Sorted List in Ascending Order Using Selection Sort is: 10, 12, 20, 50, 70
Brief Theory:- Quicksort is a divide and conquer algorithm. Quicksort first divides a large array
into two smaller sub-arrays: the low elements and the high elements. Quicksort can then recursively
sort the sub-arrays.
Procedure:-
#include<stdio.h
Results:
Enter the No. of Elements to be sorted: 5
Enter 5 integers: 70, 12, 50, 20, 10
Sorted List in Ascending Order Using Quick Sort is: 10, 12, 20, 50, 70
Viva-voce Questions
Procedure:-
1. Call the buildMaxHeap() function on the list. Also referred to as heapify(), this builds a heap
from a list in O(n) operations.
2. Swap the first element of the list with the final element. Decrease the considered range of the
list by one.
3. Call the Adjust() function on the list to sift the new first element to its appropriate index in
the heap.
4. Go to step (2) unless the considered range of the list is one element.
Program:-
#include<stdio.h>
#include<conio.h>
void heapsort(int[], int);
void heapify(int[], int);
void adjust(int[], int);
int main()
{
int array[50],n,i;
clrscr();
printf("Enter the no. of elements to be sorted:\n ");
scanf("%d",&n);
printf("Enter %d elements: \n",n);
for(i=0 ; i<n ; i++)
{
scanf("%d",&array[i]);
Department of Information Technology
heapsort(array,n);
printf("Sorted list in ascending order using heap sort is:\n");
for(i = 0; i < n; i++)
{
printf("%d\t", array[i]);
}
printf("\n");
getch();
return 0;
}
void heapsort(int array[], int n)
{
int i,t;
heapify(array,n);
for(i=n-1 ; i>0 ; i--)
{
t = array[0];
array[0] = array[i];
array[i] = t;
adjust(array,i);
}
}
void heapify(int array[], int n)
{
int item,i,j,k;
for(k=1 ; k<n ; k++)
{
item = array[k];
i = k;
j = (i-1)/2;
while( (i>0) && (item>array[j]) )
{
array[i] = array[j];
i = j;
j = (i-1)/2;
}
array[i] = item;
}
}
void adjust(int array[], int n)
{
int item,i,j;
Results:
Enter the No. of Elements to be sorted: 5
Enter 5 integers: 70, 12, 50, 20, 15
Sorted List in Ascending Order Using Heap Sort is: 12, 15, 20, 50, 70
Viva-voce Questions
Procedure:-
1. // Input:
2. // Values (stored in array v)
3. // Weights (stored in array w)
4. // Number of distinct items (n)
5. // Knapsack capacity (W)
6. for j from 0 to W do:
7. m[0, j] := 0
8. for i from 1 to n do:
9. for j from 0 to W do:
10. if w[i-1] > j then:
11. m[i, j] := m[i-1, j]
12. else:
13. m[i, j] := max(m[i-1, j], m[i-1, j-w[i-1]] + v[i-1])
Program:-
#include<stdio.h>
#include<conio.h>
void knapsack(int n, float weight[], float profit[], float capacity)
{
float x[20], tp = 0;
int i, j, u;
u = capacity;
for (i = 0; i < n; i++)
x[i] = 0.0;
for (i = 0; i < n; i++)
{
if (weight[i] > u)
Results:
3, 5
5, 15
7, 7
1, 6
4, 18
1, 3
Enter the capacity of knapsack: - 15
The result vector is: - 1.000000 1.000000 1.000000 1.000000 1.000000 0.666667 0.000000
Maximum profit is: - 55.333332
Objective: - Program to find the minimum spanning tree using Kruskal’s algorithm.
Procedure:-
KRUSKAL(G):
1A=∅
2 foreach v ∈ G.V:
3 MAKE-SET(v)
4 foreach (u, v) in G.E ordered by weight(u, v), increasing:
5 if FIND-SET(u) ≠ FIND-SET(v):
6 A = A ∪ {(u, v)}
7 UNION(u, v)
8 return A
Program:-
#include<stdio.h>
#include<conio.h>
#define INF 0
char vertex[10];
int wght[10][10];
int span_wght[10][10];
int source;
struct Sort
{
int v1,v2;
int weight;
}que[20];
int n,ed,f,r;
int cycle(int s,int d)
{
int j,k;
if(source==d)
Department of Information Technology
return 1; for(j=0;j<n;j++)
if(span_wght[d][j]!=INF && s!=j)
{
if(cycle(d,j))
return 1;
}
return 0;
}
void build_tree()
{
int i,j,w,k,count=0;
for(count=0;count<n;f++)
{ i=que[f].v1;
j=que[f].v2;
w=que[f].weight;
span_wght[i][j]=span_wght[j][i]=w;
source=i;
k=cycle(i,j);
if(k)
span_wght[i][j]=span_wght[j][i]=INF;
else
count++;
}
}
void swap(int *i,int *j)
{
int t;
t=*i;
*i=*j;
*j=t;
}
void main()
{
int i,j,k=0,temp;
int sum=0;
clrscr();
printf("\n\tEnter the No. of Nodes : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
Results:
Original Graph Weight Matrix
Weight Matrix
0 4 8 6 7
4 0 2 9 0
8 2 0 8 5
6 9 8 0 3
7 0 5 3 0
Total weight = 14
Viva-voce Questions
1. What is graph?
A graph is a pictorial representation of a set of objects where some pairs of objects are
connected by links. The interconnected objects are represented by points termed as vertices,
and the links that connect the vertices are called edges.
Brief Theory:-The Travelling Salesman Problem describes a salesman who must travel between
N cities. The order in which he does so is something he does not care about, as long as he visits
each one during his trip, and finishes where he was at first. Each city is connected to other close by
cities, or nodes, by airplanes, or by road or railway. Each of those links between the cities has one
or more weights (or the cost) attached. The cost describes how "difficult" it is to traverse this edge
on the graph, and may be given, for example, by the cost of an airplane ticket or train
ticket, or perhaps by the length of the edge, or time required to complete the traversal. The
salesman wants to keep both the travel costs, as well as the distance he travels as low as possible.
Procedure:-
Program:-
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int a[10][10],visited[10],n,cost=0;
void get()
{
int i,j;
printf("\n\nEnter Number of Cities: ");
scanf("%d",&n);
printf("\nEnter Cost Matrix: \n");
for( i=0;i<n;i++)
{
printf("\n Enter Elements of Row # : %d\n",i+1);
for( j=0;j<n;j++)
Results:
Viva-voce Questions
Travelling Salesman Problem describes a salesman who must travel between N cities. The order
in which he does so is something he does not care about, as long as he visits each one during
his trip, and finishes where he was at first. Each city is connected to other close by
3. Let a complete graph G has n vertices and e edges. What will be the value of e in terms
of vertices n?
e = n (n-1)/2
4. Define forest.
Collection of sub trees that are obtained when root node is eliminated is known as forest
Brief Theory:-In this problem or puzzle, N queens will be given and the challenge is to place all
of them in N x N chessboard so that no two queens are in attacking position. What that means is
we need to find the configuration of N queens where any of the two queens do not share a row,
column and diagonal paths. One of the better and elegant solutions to this puzzle is
‘backtracking algorithm’.
Procedure:-
1. Place the queens column wise, start from the left most column
2. If all queens are placed.
1. return true and print the solution matrix.
3. Else
1. Try all the rows in the current column.
2. Check if queen can be placed here safely if yes mark the current cell in solution
matrix as 1 and try to solve the rest of the problem recursively.
3. If placing the queen in above step leads to the solution return true.
4. If placing the queen in above step does not lead to the solution , BACKTRACK,
mark the current cell in solution matrix as 0 and return false.
4. If all the rows are tried and nothing worked, return false and print NO SOLUTION.
Program:-
#include<stdio.h>
#include<conio.h>
#include<math.h>
char a[10][10];
int n;
void printmatrix()
{
int i, j;
printf("\n");
for (i = 0; i < n; i++)
{
Results:
Enter the no. of queens: - 4
The solution is: -
. Q . .
. . . Q
Q . . .
. . Q .
The solution is: -
. . Q .
Q . . .
. . . Q
. Q . .
Viva-voce Questions
Experiment -12
2. #include <stdio.h>
3. void shellsort(int arr[], int num)
4. {
5. int i, j, k, tmp;
6. for (i = num / 2; i > 0; i = i / 2)
7. {
8. for (j = i; j < num; j++)
9. {
10. for(k = j - i; k >= 0; k = k - i)
11. {
12. if (arr[k+i] >= arr[k])
13. break;
14. else
15. {
16. tmp = arr[k];
17. arr[k] = arr[k+i];
18. arr[k+i] = tmp;
19. }
20. }
21. }
Viva-voce Questions
Q-1 What is shell sort
Shell sort, also known as Shell sort or Shell’s method, is an in-place comparison sort. It can either
be seen as a generalization of sorting by exchange (bubble sort) or sorting by insertion (insertion
sort).
Worst case time complexity is O(n2) and best-case complexity is O(n log(n)).
Bubble sort is also known as sinking sort. This algorithm compares each pair of adjacent items and
swaps them if they are in the wrong order, and this same process goes on until no swaps are
needed. In the following program we are implementing bubble sort in C language. In this program
user would be asked to enter the number of elements along with the element values and then the
program would sort them in ascending order by using bubble sorting algorithm logic.
#include<stdio.h>
int main(){
for(i=0;i<count;i++)
scanf("%d",&number[i]);
return 0;
}
Output:
Viva-voce Questions
Bubble sort is also known as sinking sort. This algorithm compares each pair of adjacent items and
swaps them if they are in the wrong order, and this same process goes on until no swaps are
needed. In the following program we are implementing bubble sort in C language
Counting sort algorithm is a sorting algorithm which do not involve comparison between elements
of an array1.
First of all I am reading n elements in array a[]. While reading the array elements I have also
calculated the maximum element.
2. Now the actual sorting is done in counting_sort() function. Here I am counting the occurrence of
each element in the array a[] and then storing it at the index equal to the value of that element. For
example occurrence of element 5 will be stored at index 5, occurrence of element 9 will be stored
at index 9 and so on.
3. As the value at each index in count[] array is the occurrence of that index or element, so the
elements are printed in ascending order by printing each index number of times equal to its
corresponding value.
1 #include<stdio.h>
2
3 void counting_sort(int a[],int n,int max)
4 {
5 int count[50]={0},i,j;
6
7 for(i=0;i<n;++i)
8 count[a[i]]=count[a[i]]+1;
9
10 printf("\nSorted elements are:");
11
12 for(i=0;i<=max;++i)
13 for(j=1;j<=count[i];++j)
14 printf("%d ",i);
15 }
16
17 int main()
18 {
19 int a[50],n,i,max=0;
20 printf("Enter number of elements:");
21 scanf("%d",&n);
22 printf("\nEnter elements:")
for(i=0;i<n;++i)
{
scanf("%d",&a[i]);
24 if(a[i]>max)
25 max=a[i];
26 }
27
28 counting_sort(a,n,max);
29 return 0;
30 }
Output
Viva-voce Questions
Counting Sort. Counting sort is a sorting technique based on keys between a specific range. It
works by counting the number of objects having distinct key values (kind of hashing). Then doing
some arithmetic to calculate the position of each object in the output sequence.