SlideShare uma empresa Scribd logo
1 de 28
Baixar para ler offline
Department of Computer Science & Engineering
Lucknow Institute of Technology
Lucknow
Session (2019-20)
Practical lab
Files
“Design and Analysis of Algorithm Lab”
(RCS 552)
Submitted To:- Submitted By:-
Ms. Arti Singh Nitesh Kumar Dubey
(Asst. professor) B.Tech ( 5th semester)
Dept. of Computer Science Computer Science & Engg.
& Engineering 1836210903
Index
Sr .no Experiment Signatures Remarks
1
2
3
4
5
6
7
8
9
10
Program for Recursive Binary & Linear
Search.
Program for Heap sort
Program for Merge sort
Program for selection sort
Program for Insertion sort
Program for Quick sort
Knapsack Problem using Greedy Solution
Perform Travelling Salesman Problem
Find Minimum Spanning Tree Using
Kruskal’s algorithm
Implement N Queen Problem using
Backtracking
Experiment:- 1
Objective: - Program for Recursive Binary & Linear Search.
Program Binary Search
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
int main()
{
int c, first, last, middle, n, search, array[100];
printf("Enter number of elements:n");
scanf("%d",&n);
printf("Enter %d integers:n", n);
for (c = 0; c < n; c++)
scanf("%d",&array[c]);
printf("Enter the value to find:n");
scanf("%d", &search);
first = 0;
last = n - 1;
middle = (first+last)/2;
while (first <= last) {
if (array[middle] < search)
first = middle + 1;
else if (array[middle] == search) {
printf("%d is present at index %d.n", search, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
if (first > last)
printf("Not found! %d is not present in the list.n", search);
return 0;
}
Program Linear search :-
#include <stdio.h>
#include <conio.h>
int search(int [], int, int);
int main()
{
int size, index, key;
int list[20]; int count = 0; int i;
printf("Enter the size of the list: ");
scanf("%d", &size);
index = size;
printf("Printing the list:n");
for (i = 0; i < size; i++)
{
list[i] = rand() % size;
printf("%dt", list[i]);
}
printf("nEnter the key to search: ");
scanf("%d", &key);
while (index > 0)
{
index = search(list, index - 1, key);
/* In an array first position is indexed by 0 */
printf("Key found at position: %dn", index + 1);
count++;
}
if (!count)
printf("Key not found.n");
return 0;
}
int search(int array[], int size, int key)
{
int location;
if (array[size] == key)
{
return size;
}
else if (size == -1)
{
return -1;
}
else
{
return (location = search(array, size - 1, key));
}
}
Result:- Binary search
Linear search
Experiment: - 2
Objective: - Program for heap sort
Program
#include<stdio.h>
#include <conio.h>
void create(int []);
void down_adjust(int [],int);
void main()
{
int heap[30],n,i,last,temp;
printf("Enter no. of elements:");
scanf("%d",&n);
printf("nEnter elements:");
for(i=1;i<=n;i++)
scanf("%d",&heap[i]);
heap[0]=n;
create(heap);
while(heap[0] > 1)
{
//swap heap[1] and heap[last]
last=heap[0];
temp=heap[1];
heap[1]=heap[last];
heap[last]=temp;
heap[0]--;
down_adjust(heap,1);
}
printf("nArray after sorting:n");
for(i=1;i<=n;i++)
printf("%d ",heap[i]);
}
void create(int heap[])
{
int i,n;
n=heap[0];
for(i=n/2;i>=1;i--)
down_adjust(heap,i);
}
void down_adjust(int heap[],int i)
{
int j,temp,n,flag=1;
n=heap[0];
while(2*i<=n && flag==1)
{
j=2*i;
if(j+1<=n && heap[j+1] > heap[j])
j=j+1;
if(heap[i] > heap[j])
flag=0;
else
{
temp=heap[i];
heap[i]=heap[j];
heap[j]=temp;
i=j;
}
}
}
Result
Experiment:- 3
Program :-
#include<stdio.h>
#include <conio.h>
void mergesort(int a[],int i,int j);
void merge(int a[],int i1,int j1,int i2,int j2);
int main()
{
int a[30],n,i;
printf("Enter no of elements:");
scanf("%d",&n);
printf("Enter array elements:");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
mergesort(a,0,n-1);
printf("nSorted array is :");
for(i=0;i<n;i++)
printf("%d ",a[i]);
return 0;
}
void mergesort(int a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j);
merge(a,i,mid,mid+1,j);
}
}
void merge(int a[],int i1,int j1,int i2,int j2)
{
int temp[50];
int i,j,k;
i=i1; j=i2; k=0;
while(i<=j1 && j<=j2)
{
if(a[i]<a[j])
temp[k++]=a[i++];
else
temp[k++]=a[j++];
}
while(i<=j1)
temp[k++]=a[i++];
while(j<=j2)
temp[k++]=a[j++];
for(i=i1,j=0;i<=j2;i++,j++)
a[i]=temp[j];
}
Result:-
Experiment:- 4
Objective :- Program for selection Sort
#include <stdio.h>
#include <conio.h>
int main()
{
int array[100], n, c, d, position, swap;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0; c < (n - 1); c++)
{
position = c;
for (d = c + 1; d < n; d++)
{
if (array[position] > array[d])
position = d;
}
if (position != c)
{
swap = array[c];
array[c] = array[position];
array[position] = swap;
}
}
printf("Sorted list in ascending order:n");
for (c = 0; c < n; c++)
printf("%dn", array[c]);
return 0;
}
Result:-
Experiment:- 5
Objective: - Program for Insertion sort
Program
#include <stdio.h>
#include <conio.h>
int main()
{
int n, array[1000], c, d, t;
printf("Enter number of elementsn");
scanf("%d", &n);
printf("Enter %d integersn", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 1 ; c <= n - 1; c++) {
d = c;
while ( d > 0 && array[d-1] > array[d]) {
t = array[d];
array[d] = array[d-1];
array[d-1] = t;
d--;
}
}
printf("Sorted list in ascending order:n");
for (c = 0; c <= n - 1; c++) {
printf("%dn", array[c]);
}
return 0;
}
Result:-
Experiment:- 6
Objective:- Program for Quick Sort
Program
#include <stdio.h>
#include <conio.h>
void quicksort (int [], int, int);
int main()
{
int list[50]; int size, i;
printf("Enter the number of elements: ");
scanf("%d", &size);
printf("Enter the elements to be sorted:n");
for (i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
quicksort(list, 0, size - 1);
printf("After applying quick sortn");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
printf("n");
return 0;
}
void quicksort(int list[], int low, int high)
{
int pivot, i, j, temp;
if (low < high)
{
pivot = low;
i = low;
j = high;
while (i < j)
{
while (list[i] <= list[pivot] && i <= high)
{ i++;
}
while (list[j] > list[pivot] && j >= low)
{ j--;
}
if (i < j)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
temp = list[j];
list[j] = list[pivot];
list[pivot] = temp;
quicksort(list, low, j - 1);
quicksort(list, j + 1, high);
}
}
Result :-
Experiment:- 7
Objective: - Knapsack Problem using Greedy Solution
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)
break;
else {
x[i] = 1.0;
tp = tp + profit[i];
u = u - weight[i];
}
}
if (i < n)
x[i] = u / weight[i];
tp = tp + (x[i] * profit[i]);
printf("nThe result vector is:- ");
for (i = 0; i < n; i++)
printf("%ft", x[i]);
printf("nMaximum profit is:- %f", tp);
}
int main() {
float weight[20], profit[20], capacity;
int num, i, j;
float ratio[20], temp;
printf("nEnter the no. of objects:- ");
scanf("%d", &num);
printf("nEnter the wts and profits of each object:- ");
for (i = 0; i < num; i++) {
scanf("%f %f", &weight[i], &profit[i]);
}
printf("nEnter the capacityacity of knapsack:- ");
scanf("%f", &capacity);
for (i = 0; i < num; i++) {
ratio[i] = profit[i] / weight[i];
}
for (i = 0; i < num; i++) {
for (j = i + 1; j < num; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;
temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;
temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
knapsack(num, weight, profit, capacity);
return(0);
}
Result:-
Experiment: - 8
Objective: - Perform Travelling Salesman Problem
Program
#include<stdio.h>
#include <conio.h>
int ary[10][10],completed[10],n,cost=0;
void takeInput()
{
int i,j;
printf("Enter the number of villages: ");
scanf("%d",&n);
printf("nEnter the Cost Matrixn");
for(i=0;i < n;i++)
{
printf("nEnter Elements of Row: %dn",i+1);
for( j=0;j < n;j++)
scanf("%d",&ary[i][j]);
completed[i]=0;
}
printf("nnThe cost list is:");
for( i=0;i < n;i++)
{
printf("n");
for(j=0;j < n;j++)
printf("t%d",ary[i][j]);
}
}
void mincost(int city)
{
int i,ncity;
completed[city]=1;
printf("%d--->",city+1);
ncity=least(city);
if(ncity==999)
{
ncity=0;
printf("%d",ncity+1);
cost+=ary[city][ncity];
return;
}
mincost(ncity);
}
int least(int c)
{
int i,nc=999;
int min=999,kmin;
for(i=0;i < n;i++)
{
if((ary[c][i]!=0)&&(completed[i]==0))
if(ary[c][i]+ary[i][c] < min)
{
min=ary[i][0]+ary[c][i];
kmin=ary[c][i];
nc=i;
}
}
if(min!=999)
cost+=kmin;
return nc;
}
int main()
{
takeInput();
printf("nnThe Path is:n");
mincost(0); //passing 0 because starting vertex
printf("nnMinimum cost is %dn ",cost);
return 0;
}
Result :-
Experiment :- 9
Objective: - Find Minimum Spanning Tree using kruskal’s Algorithm
Program
#include<stdio.h>
#incluse <conio.h>
#define MAX 30
typedef struct edge
{
int u,v,w;
}edge;
typedef struct edgelist
{
edge data[MAX];
int n;
}edgelist;
edgelist elist;
int G[MAX][MAX],n;
edgelist spanlist;
void kruskal();
int find(int belongs[],int vertexno);
void union1(int belongs[],int c1,int c2);
void sort();
void print();
void main()
{
int i,j,total_cost;
printf("nEnter number of vertices:");
scanf("%d",&n);
printf("nEnter the adjacency matrix:n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
scanf("%d",&G[i][j]);
kruskal();
print();
}
void kruskal()
{
int belongs[MAX],i,j,cno1,cno2;
elist.n=0;
for(i=1;i<n;i++)
for(j=0;j<i;j++)
{
if(G[i][j]!=0)
{
elist.data[elist.n].u=i;
elist.data[elist.n].v=j;
elist.data[elist.n].w=G[i][j];
elist.n++;
}
}
sort();
for(i=0;i<n;i++)
belongs[i]=i;
spanlist.n=0;
for(i=0;i<elist.n;i++)
{
cno1=find(belongs,elist.data[i].u);
cno2=find(belongs,elist.data[i].v);
if(cno1!=cno2)
{
spanlist.data[spanlist.n]=elist.data[i];
spanlist.n=spanlist.n+1;
union1(belongs,cno1,cno2);
}
}
}
int find(int belongs[],int vertexno)
{
return(belongs[vertexno]);
}
void union1(int belongs[],int c1,int c2)
{
int i;
for(i=0;i<n;i++)
if(belongs[i]==c2)
belongs[i]=c1;
}
void sort()
{
int i,j;
edge temp;
for(i=1;i<elist.n;i++)
for(j=0;j<elist.n-1;j++)
if(elist.data[j].w>elist.data[j+1].w)
{
temp=elist.data[j];
elist.data[j]=elist.data[j+1];
elist.data[j+1]=temp;
}
}
void print()
{
int i,cost=0;
for(i=0;i<spanlist.n;i++)
{
printf("n%dt%dt%d",spanlist.data[i].u,spanlist.data[i].v,spanlist.data[i].w);
cost=cost+spanlist.data[i].w;
}
printf("nnCost of the spanning tree=%d",cost);
}
Result: -
Experiment :- 10
Objective: - Implement N Queen Problem using Backtracking
Program
#include<stdio.h>
#include <conio.h>
#include<math.h>
int board[20],count;int main()
{
int n,i,j;
void queen(int row,int n);
printf(" - N Queens Problem Using Backtracking -");
printf("nnEnter number of Queens:");
scanf("%d",&n);
queen(1,n);
return 0;
}
void print(int n)
{
int i,j;
printf("nnSolution %d:nn",++count);
for(i=1;i<=n;++i)
printf("t%d",i);
for(i=1;i<=n;++i)
{
printf("nn%d",i);
for(j=1;j<=n;++j) //for nxn board
{
if(board[i]==j)
printf("tQ"); //queen at i,j position
else
printf("t-"); //empty slot
}
}
}
int place(int row,int column)
{
int i;
for(i=1;i<=row-1;++i)
{
if(board[i]==column)
return 0;
else
if(abs(board[i]-column)==abs(i-row))
return 0;
}
return 1;
}
void queen(int row,int n)
{
int column;
for(column=1;column<=n;++column)
{
if(place(row,column))
{
board[row]=column; //no conflicts so place queen
if(row==n) //dead end
print(n); //printing the board configuration
else //try queen with next position
queen(row+1,n);
}
}
}
Result :-

Mais conteúdo relacionado

Mais procurados

Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna university
sangeethajames07
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
Ehtisham Ali
 

Mais procurados (20)

Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Introduction to data structure and algorithms
Introduction to data structure and algorithmsIntroduction to data structure and algorithms
Introduction to data structure and algorithms
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Strings in c
Strings in cStrings in c
Strings in c
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Ds lab handouts
Ds lab handoutsDs lab handouts
Ds lab handouts
 
2D Array
2D Array 2D Array
2D Array
 
Introduction to Array ppt
Introduction to Array pptIntroduction to Array ppt
Introduction to Array ppt
 
Pointer in C++
Pointer in C++Pointer in C++
Pointer in C++
 
Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna university
 
Python Collections
Python CollectionsPython Collections
Python Collections
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 Data Structures - Lecture 9 [Stack & Queue using Linked List] Data Structures - Lecture 9 [Stack & Queue using Linked List]
Data Structures - Lecture 9 [Stack & Queue using Linked List]
 
This pointer
This pointerThis pointer
This pointer
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 

Semelhante a design and analysis of algorithm Lab files

Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
SANTOSH RATH
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
rushabhshah600
 

Semelhante a design and analysis of algorithm Lab files (20)

Pnno
PnnoPnno
Pnno
 
DSC program.pdf
DSC program.pdfDSC program.pdf
DSC program.pdf
 
ADA FILE
ADA FILEADA FILE
ADA FILE
 
Ada file
Ada fileAda file
Ada file
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
 
Data structure new lab manual
Data structure  new lab manualData structure  new lab manual
Data structure new lab manual
 
Cpds lab
Cpds labCpds lab
Cpds lab
 
Solutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structuresSolutionsfor co2 C Programs for data structures
Solutionsfor co2 C Programs for data structures
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
 
Data struture lab
Data struture labData struture lab
Data struture lab
 
C programs
C programsC programs
C programs
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
Ds
DsDs
Ds
 
DataStructures notes
DataStructures notesDataStructures notes
DataStructures notes
 
Operating system labs
Operating system labsOperating system labs
Operating system labs
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
1sequences and sampling. Suppose we went to sample the x-axis from X.pdf
 

Mais de Nitesh Dubey

Mais de Nitesh Dubey (20)

HTML Presentation
HTML  PresentationHTML  Presentation
HTML Presentation
 
MLApproachToProgramming.ppt
MLApproachToProgramming.pptMLApproachToProgramming.ppt
MLApproachToProgramming.ppt
 
seminar topic of holography.ppt
seminar topic of holography.pptseminar topic of holography.ppt
seminar topic of holography.ppt
 
Compiler design.pdf
Compiler design.pdfCompiler design.pdf
Compiler design.pdf
 
Online shopping ppt
Online shopping pptOnline shopping ppt
Online shopping ppt
 
Python lab manual all the experiments are available
Python lab manual all the experiments are availablePython lab manual all the experiments are available
Python lab manual all the experiments are available
 
Web Technology Lab files with practical
Web Technology Lab  files with practicalWeb Technology Lab  files with practical
Web Technology Lab files with practical
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
 
Software engineering practical
Software engineering practicalSoftware engineering practical
Software engineering practical
 
Principal of programming language lab files
Principal of programming language lab files Principal of programming language lab files
Principal of programming language lab files
 
database management system lab files
database management system lab filesdatabase management system lab files
database management system lab files
 
Computer Organization And Architecture lab manual
Computer Organization And Architecture lab manualComputer Organization And Architecture lab manual
Computer Organization And Architecture lab manual
 
industrial training report on Ethical hacking
industrial training report on Ethical hackingindustrial training report on Ethical hacking
industrial training report on Ethical hacking
 
Project synopsis on face recognition in e attendance
Project synopsis on face recognition in e attendanceProject synopsis on face recognition in e attendance
Project synopsis on face recognition in e attendance
 
Hrms industrial training report
Hrms industrial training reportHrms industrial training report
Hrms industrial training report
 
Industrial training report on core java
Industrial training report on core java Industrial training report on core java
Industrial training report on core java
 
SEWAGE TREATMENT PLANT mini project report
SEWAGE TREATMENT PLANT mini project reportSEWAGE TREATMENT PLANT mini project report
SEWAGE TREATMENT PLANT mini project report
 
synopsis report on BIOMETRIC ONLINE VOTING SYSTEM
synopsis report on BIOMETRIC ONLINE VOTING SYSTEMsynopsis report on BIOMETRIC ONLINE VOTING SYSTEM
synopsis report on BIOMETRIC ONLINE VOTING SYSTEM
 
artificial intelligence ppt
artificial intelligence pptartificial intelligence ppt
artificial intelligence ppt
 
object oriented Programming ppt
object oriented Programming pptobject oriented Programming ppt
object oriented Programming ppt
 

Último

+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
Health
 

Último (20)

Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 
Employee leave management system project.
Employee leave management system project.Employee leave management system project.
Employee leave management system project.
 
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
+97470301568>> buy weed in qatar,buy thc oil qatar,buy weed and vape oil in d...
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Engineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planesEngineering Drawing focus on projection of planes
Engineering Drawing focus on projection of planes
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptx
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 

design and analysis of algorithm Lab files

  • 1. Department of Computer Science & Engineering Lucknow Institute of Technology Lucknow Session (2019-20) Practical lab Files “Design and Analysis of Algorithm Lab” (RCS 552) Submitted To:- Submitted By:- Ms. Arti Singh Nitesh Kumar Dubey (Asst. professor) B.Tech ( 5th semester) Dept. of Computer Science Computer Science & Engg. & Engineering 1836210903
  • 2. Index Sr .no Experiment Signatures Remarks 1 2 3 4 5 6 7 8 9 10 Program for Recursive Binary & Linear Search. Program for Heap sort Program for Merge sort Program for selection sort Program for Insertion sort Program for Quick sort Knapsack Problem using Greedy Solution Perform Travelling Salesman Problem Find Minimum Spanning Tree Using Kruskal’s algorithm Implement N Queen Problem using Backtracking
  • 3. Experiment:- 1 Objective: - Program for Recursive Binary & Linear Search. Program Binary Search #include <stdio.h> #include <conio.h> #include <stdlib.h> int main() { int c, first, last, middle, n, search, array[100]; printf("Enter number of elements:n"); scanf("%d",&n); printf("Enter %d integers:n", n); for (c = 0; c < n; c++) scanf("%d",&array[c]); printf("Enter the value to find:n"); scanf("%d", &search); first = 0; last = n - 1; middle = (first+last)/2; while (first <= last) { if (array[middle] < search) first = middle + 1; else if (array[middle] == search) { printf("%d is present at index %d.n", search, middle+1); break; } else last = middle - 1; middle = (first + last)/2; }
  • 4. if (first > last) printf("Not found! %d is not present in the list.n", search); return 0; } Program Linear search :- #include <stdio.h> #include <conio.h> int search(int [], int, int); int main() { int size, index, key; int list[20]; int count = 0; int i; printf("Enter the size of the list: "); scanf("%d", &size); index = size; printf("Printing the list:n"); for (i = 0; i < size; i++) { list[i] = rand() % size; printf("%dt", list[i]); } printf("nEnter the key to search: "); scanf("%d", &key); while (index > 0) { index = search(list, index - 1, key); /* In an array first position is indexed by 0 */ printf("Key found at position: %dn", index + 1); count++; }
  • 5. if (!count) printf("Key not found.n"); return 0; } int search(int array[], int size, int key) { int location; if (array[size] == key) { return size; } else if (size == -1) { return -1; } else { return (location = search(array, size - 1, key)); } } Result:- Binary search Linear search
  • 6. Experiment: - 2 Objective: - Program for heap sort Program #include<stdio.h> #include <conio.h> void create(int []); void down_adjust(int [],int); void main() { int heap[30],n,i,last,temp; printf("Enter no. of elements:"); scanf("%d",&n); printf("nEnter elements:"); for(i=1;i<=n;i++) scanf("%d",&heap[i]); heap[0]=n; create(heap); while(heap[0] > 1) { //swap heap[1] and heap[last] last=heap[0]; temp=heap[1]; heap[1]=heap[last]; heap[last]=temp; heap[0]--; down_adjust(heap,1); } printf("nArray after sorting:n"); for(i=1;i<=n;i++) printf("%d ",heap[i]); }
  • 7. void create(int heap[]) { int i,n; n=heap[0]; for(i=n/2;i>=1;i--) down_adjust(heap,i); } void down_adjust(int heap[],int i) { int j,temp,n,flag=1; n=heap[0]; while(2*i<=n && flag==1) { j=2*i; if(j+1<=n && heap[j+1] > heap[j]) j=j+1; if(heap[i] > heap[j]) flag=0; else { temp=heap[i]; heap[i]=heap[j]; heap[j]=temp; i=j; } } } Result
  • 8. Experiment:- 3 Program :- #include<stdio.h> #include <conio.h> void mergesort(int a[],int i,int j); void merge(int a[],int i1,int j1,int i2,int j2); int main() { int a[30],n,i; printf("Enter no of elements:"); scanf("%d",&n); printf("Enter array elements:"); for(i=0;i<n;i++) scanf("%d",&a[i]); mergesort(a,0,n-1); printf("nSorted array is :"); for(i=0;i<n;i++) printf("%d ",a[i]); return 0; } void mergesort(int a[],int i,int j) { int mid; if(i<j) { mid=(i+j)/2; mergesort(a,i,mid); mergesort(a,mid+1,j); merge(a,i,mid,mid+1,j);
  • 9. } } void merge(int a[],int i1,int j1,int i2,int j2) { int temp[50]; int i,j,k; i=i1; j=i2; k=0; while(i<=j1 && j<=j2) { if(a[i]<a[j]) temp[k++]=a[i++]; else temp[k++]=a[j++]; } while(i<=j1) temp[k++]=a[i++]; while(j<=j2) temp[k++]=a[j++]; for(i=i1,j=0;i<=j2;i++,j++) a[i]=temp[j]; } Result:-
  • 10. Experiment:- 4 Objective :- Program for selection Sort #include <stdio.h> #include <conio.h> int main() { int array[100], n, c, d, position, swap; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integersn", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); for (c = 0; c < (n - 1); c++) { position = c; for (d = c + 1; d < n; d++) { if (array[position] > array[d]) position = d; } if (position != c) { swap = array[c]; array[c] = array[position]; array[position] = swap; } }
  • 11. printf("Sorted list in ascending order:n"); for (c = 0; c < n; c++) printf("%dn", array[c]); return 0; } Result:-
  • 12. Experiment:- 5 Objective: - Program for Insertion sort Program #include <stdio.h> #include <conio.h> int main() { int n, array[1000], c, d, t; printf("Enter number of elementsn"); scanf("%d", &n); printf("Enter %d integersn", n); for (c = 0; c < n; c++) scanf("%d", &array[c]); for (c = 1 ; c <= n - 1; c++) { d = c; while ( d > 0 && array[d-1] > array[d]) { t = array[d]; array[d] = array[d-1]; array[d-1] = t; d--; } } printf("Sorted list in ascending order:n");
  • 13. for (c = 0; c <= n - 1; c++) { printf("%dn", array[c]); } return 0; } Result:-
  • 14. Experiment:- 6 Objective:- Program for Quick Sort Program #include <stdio.h> #include <conio.h> void quicksort (int [], int, int); int main() { int list[50]; int size, i; printf("Enter the number of elements: "); scanf("%d", &size); printf("Enter the elements to be sorted:n"); for (i = 0; i < size; i++) { scanf("%d", &list[i]); } quicksort(list, 0, size - 1); printf("After applying quick sortn"); for (i = 0; i < size; i++) { printf("%d ", list[i]); } printf("n"); return 0; } void quicksort(int list[], int low, int high) { int pivot, i, j, temp; if (low < high) {
  • 15. pivot = low; i = low; j = high; while (i < j) { while (list[i] <= list[pivot] && i <= high) { i++; } while (list[j] > list[pivot] && j >= low) { j--; } if (i < j) { temp = list[i]; list[i] = list[j]; list[j] = temp; } } temp = list[j]; list[j] = list[pivot]; list[pivot] = temp; quicksort(list, low, j - 1); quicksort(list, j + 1, high); } } Result :-
  • 16. Experiment:- 7 Objective: - Knapsack Problem using Greedy Solution 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) break; else { x[i] = 1.0; tp = tp + profit[i]; u = u - weight[i]; } } if (i < n) x[i] = u / weight[i]; tp = tp + (x[i] * profit[i]); printf("nThe result vector is:- "); for (i = 0; i < n; i++) printf("%ft", x[i]); printf("nMaximum profit is:- %f", tp); }
  • 17. int main() { float weight[20], profit[20], capacity; int num, i, j; float ratio[20], temp; printf("nEnter the no. of objects:- "); scanf("%d", &num); printf("nEnter the wts and profits of each object:- "); for (i = 0; i < num; i++) { scanf("%f %f", &weight[i], &profit[i]); } printf("nEnter the capacityacity of knapsack:- "); scanf("%f", &capacity); for (i = 0; i < num; i++) { ratio[i] = profit[i] / weight[i]; } for (i = 0; i < num; i++) { for (j = i + 1; j < num; j++) { if (ratio[i] < ratio[j]) { temp = ratio[j]; ratio[j] = ratio[i]; ratio[i] = temp; temp = weight[j]; weight[j] = weight[i]; weight[i] = temp; temp = profit[j]; profit[j] = profit[i]; profit[i] = temp; } } } knapsack(num, weight, profit, capacity);
  • 19. Experiment: - 8 Objective: - Perform Travelling Salesman Problem Program #include<stdio.h> #include <conio.h> int ary[10][10],completed[10],n,cost=0; void takeInput() { int i,j; printf("Enter the number of villages: "); scanf("%d",&n); printf("nEnter the Cost Matrixn"); for(i=0;i < n;i++) { printf("nEnter Elements of Row: %dn",i+1); for( j=0;j < n;j++) scanf("%d",&ary[i][j]); completed[i]=0; } printf("nnThe cost list is:"); for( i=0;i < n;i++) { printf("n"); for(j=0;j < n;j++) printf("t%d",ary[i][j]); } } void mincost(int city) {
  • 20. int i,ncity; completed[city]=1; printf("%d--->",city+1); ncity=least(city); if(ncity==999) { ncity=0; printf("%d",ncity+1); cost+=ary[city][ncity]; return; } mincost(ncity); } int least(int c) { int i,nc=999; int min=999,kmin; for(i=0;i < n;i++) { if((ary[c][i]!=0)&&(completed[i]==0)) if(ary[c][i]+ary[i][c] < min) { min=ary[i][0]+ary[c][i]; kmin=ary[c][i]; nc=i; } } if(min!=999) cost+=kmin; return nc;
  • 21. } int main() { takeInput(); printf("nnThe Path is:n"); mincost(0); //passing 0 because starting vertex printf("nnMinimum cost is %dn ",cost); return 0; } Result :-
  • 22. Experiment :- 9 Objective: - Find Minimum Spanning Tree using kruskal’s Algorithm Program #include<stdio.h> #incluse <conio.h> #define MAX 30 typedef struct edge { int u,v,w; }edge; typedef struct edgelist { edge data[MAX]; int n; }edgelist; edgelist elist; int G[MAX][MAX],n; edgelist spanlist; void kruskal(); int find(int belongs[],int vertexno); void union1(int belongs[],int c1,int c2); void sort(); void print(); void main() { int i,j,total_cost; printf("nEnter number of vertices:"); scanf("%d",&n); printf("nEnter the adjacency matrix:n");
  • 24. { spanlist.data[spanlist.n]=elist.data[i]; spanlist.n=spanlist.n+1; union1(belongs,cno1,cno2); } } } int find(int belongs[],int vertexno) { return(belongs[vertexno]); } void union1(int belongs[],int c1,int c2) { int i; for(i=0;i<n;i++) if(belongs[i]==c2) belongs[i]=c1; } void sort() { int i,j; edge temp; for(i=1;i<elist.n;i++) for(j=0;j<elist.n-1;j++) if(elist.data[j].w>elist.data[j+1].w) { temp=elist.data[j]; elist.data[j]=elist.data[j+1]; elist.data[j+1]=temp; }
  • 26. Experiment :- 10 Objective: - Implement N Queen Problem using Backtracking Program #include<stdio.h> #include <conio.h> #include<math.h> int board[20],count;int main() { int n,i,j; void queen(int row,int n); printf(" - N Queens Problem Using Backtracking -"); printf("nnEnter number of Queens:"); scanf("%d",&n); queen(1,n); return 0; } void print(int n) { int i,j; printf("nnSolution %d:nn",++count); for(i=1;i<=n;++i) printf("t%d",i); for(i=1;i<=n;++i) { printf("nn%d",i); for(j=1;j<=n;++j) //for nxn board
  • 27. { if(board[i]==j) printf("tQ"); //queen at i,j position else printf("t-"); //empty slot } } } int place(int row,int column) { int i; for(i=1;i<=row-1;++i) { if(board[i]==column) return 0; else if(abs(board[i]-column)==abs(i-row)) return 0; } return 1; } void queen(int row,int n) { int column; for(column=1;column<=n;++column) {
  • 28. if(place(row,column)) { board[row]=column; //no conflicts so place queen if(row==n) //dead end print(n); //printing the board configuration else //try queen with next position queen(row+1,n); } } } Result :-