SlideShare una empresa de Scribd logo
1 de 28
Descargar para leer sin conexión
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 :-

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Hash table
Hash tableHash table
Hash table
 
Double ended queue
Double ended queueDouble ended queue
Double ended queue
 
Applets
AppletsApplets
Applets
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Array data structure
Array data structureArray data structure
Array data structure
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Stack using Array
Stack using ArrayStack using Array
Stack using Array
 
Bfs and Dfs
Bfs and DfsBfs and Dfs
Bfs and Dfs
 
stack & queue
stack & queuestack & queue
stack & queue
 
Call by value
Call by valueCall by value
Call by value
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Nested structure (Computer programming and utilization)
Nested structure (Computer programming and utilization)Nested structure (Computer programming and utilization)
Nested structure (Computer programming and utilization)
 
Binary Search
Binary SearchBinary Search
Binary Search
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
Unit 4 python -list methods
Unit 4   python -list methodsUnit 4   python -list methods
Unit 4 python -list methods
 
Queue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked ListQueue Implementation Using Array & Linked List
Queue Implementation Using Array & Linked List
 
Heaps
HeapsHeaps
Heaps
 
Red black tree
Red black treeRed black tree
Red black tree
 

Similar a design and analysis of algorithm Lab files

Similar 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
 
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
 
Sorting programs
Sorting programsSorting programs
Sorting programs
 

Más de Nitesh Dubey

MLApproachToProgramming.ppt
MLApproachToProgramming.pptMLApproachToProgramming.ppt
MLApproachToProgramming.pptNitesh Dubey
 
seminar topic of holography.ppt
seminar topic of holography.pptseminar topic of holography.ppt
seminar topic of holography.pptNitesh Dubey
 
Compiler design.pdf
Compiler design.pdfCompiler design.pdf
Compiler design.pdfNitesh Dubey
 
Online shopping ppt
Online shopping pptOnline shopping ppt
Online shopping pptNitesh Dubey
 
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 availableNitesh Dubey
 
Web Technology Lab files with practical
Web Technology Lab  files with practicalWeb Technology Lab  files with practical
Web Technology Lab files with practicalNitesh Dubey
 
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 manualNitesh Dubey
 
Software engineering practical
Software engineering practicalSoftware engineering practical
Software engineering practicalNitesh Dubey
 
Principal of programming language lab files
Principal of programming language lab files Principal of programming language lab files
Principal of programming language lab files Nitesh Dubey
 
database management system lab files
database management system lab filesdatabase management system lab files
database management system lab filesNitesh Dubey
 
Computer Organization And Architecture lab manual
Computer Organization And Architecture lab manualComputer Organization And Architecture lab manual
Computer Organization And Architecture lab manualNitesh Dubey
 
industrial training report on Ethical hacking
industrial training report on Ethical hackingindustrial training report on Ethical hacking
industrial training report on Ethical hackingNitesh Dubey
 
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 attendanceNitesh Dubey
 
Hrms industrial training report
Hrms industrial training reportHrms industrial training report
Hrms industrial training reportNitesh Dubey
 
Industrial training report on core java
Industrial training report on core java Industrial training report on core java
Industrial training report on core java Nitesh Dubey
 
SEWAGE TREATMENT PLANT mini project report
SEWAGE TREATMENT PLANT mini project reportSEWAGE TREATMENT PLANT mini project report
SEWAGE TREATMENT PLANT mini project reportNitesh Dubey
 
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 SYSTEMNitesh Dubey
 
artificial intelligence ppt
artificial intelligence pptartificial intelligence ppt
artificial intelligence pptNitesh Dubey
 
object oriented Programming ppt
object oriented Programming pptobject oriented Programming ppt
object oriented Programming pptNitesh Dubey
 

Más 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

Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncssuser2ae721
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxk795866
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...Chandu841456
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvLewisJB
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm Systemirfanmechengr
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfme23b1001
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsSachinPawar510423
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 

Último (20)

Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsyncWhy does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
Why does (not) Kafka need fsync: Eliminating tail latency spikes caused by fsync
 
Introduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptxIntroduction-To-Agricultural-Surveillance-Rover.pptx
Introduction-To-Agricultural-Surveillance-Rover.pptx
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...An experimental study in using natural admixture as an alternative for chemic...
An experimental study in using natural admixture as an alternative for chemic...
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Work Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvvWork Experience-Dalton Park.pptxfvvvvvvv
Work Experience-Dalton Park.pptxfvvvvvvv
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm System
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Electronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdfElectronically Controlled suspensions system .pdf
Electronically Controlled suspensions system .pdf
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Vishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documentsVishratwadi & Ghorpadi Bridge Tender documents
Vishratwadi & Ghorpadi Bridge Tender documents
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 

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