SlideShare a Scribd company logo
Program 1 
Write a program to perform insertion sort. 
#include<stdio.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int i,t,a[100],d,n; 
printf("Enter The No. Of Elementsn"); 
scanf("%d",&n); 
printf("Enter %d Integersn",n); 
for(i=0;i<n;i++) 
{ 
scanf("%d",&a[i]); 
} 
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("%dt",a[i]); 
} 
getch(); 
}
DAA Lab File C Programs
Program 2 
Write a program to perform selection sort. 
#include<stdio.h> 
#include<conio.h> 
void main() 
{ 
clrscr(); 
int i,position,swap,a[100],d,n; 
printf("Enter The No. Of Elementsn"); 
scanf("%d",&n); 
printf("Enter %d Integersn",n); 
for(i=0;i<n;i++) 
{ 
scanf("%d",&a[i]); 
} 
for(i=0;i<(n-1);i++) 
{ 
position=i; 
for(d=c+1;d<n;d++) 
{ 
if(array[position]>array[d]) 
position=d; 
} 
if(position!=i) 
{ 
swap=a[i]; 
a[i]=a[position]; 
a[position]=swap; 
} 
} 
printf("Sorted List in Ascending Order is:n"); 
for(i=0;i<=n;i++) 
{ 
printf("%dt",a[i]); 
} 
getch(); 
}
DAA Lab File C Programs
Program 3 
Write a program to perform heap sort. 
#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]); 
} 
heapsort(array,n); 
printf("Sorted list in ascending order using heap sort is:n"); 
for(i = 0; i < n; i++) 
{ 
printf("%dt", 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; 
j = 0; 
item = array[j]; 
i = 2*j+1; 
while(i<=n-1) 
{ 
if(i+1 <= n-1) 
if(array[i] < array[i+1]) 
i++; 
if(item < array[i]) 
{ 
array[j] = array[i]; 
j = i; 
i = 2*j+1; 
} 
else 
break; 
} 
array[j] = item; 
}
DAA Lab File C Programs
Program 4 
Write a program to perform quick sort. 
#include<stdio.h> 
#include<conio.h> 
void quicksort(int [10],int,int); 
void main() 
{ 
clrscr(); 
int a[20],n,i; 
printf("Enter size of the array:n"); 
scanf("%d",&n); 
printf("Enter %d elements:n",n); 
for(i=0;i<n;i++) 
{ 
scanf("%d",&a[i]); 
} 
quicksort(a,0,n-1); 
printf("Sorted elements:n "); 
for(i=0;i<n;i++) 
{ 
printf("t%d",a[i]); 
} 
getch(); 
} 
void quicksort(int a[10],int first,int last) 
{ 
int pivot,j,temp,i; 
if(first<last) 
{ 
pivot=first; 
i=first; 
j=last; 
while(i<j) 
{ 
while(a[i]<=a[pivot]&&i<last) 
i++; 
while(a[j]>a[pivot]) 
j--; 
if(i<j) 
{ 
temp=a[i]; 
a[i]=a[j]; 
a[j]=temp; 
} 
} 
temp=a[pivot]; 
a[pivot]=a[j]; 
a[j]=temp; 
quicksort(a,first,j-1); 
quicksort(a,j+1,last); 
} 
}
DAA Lab File C Programs
Program 5 
Write a program to perform counting sort. 
#include<stdio.h> 
#include<conio.h> 
void Counting_sort(int A[], int k, int n) 
{ 
int i, j; 
int B[15], C[100]; 
for(i = 0; i <= k; i++) 
C[i] = 0; 
for(j =1; j <= n; j++) 
C[A[j]] = C[A[j]] + 1; 
for(i = 1; i <= k; i++) 
C[i] = C[i] + C[i-1]; 
for(j = n; j >= 1; j--) 
{ 
B[C[A[j]]] = A[j]; 
C[A[j]] = C[A[j]] - 1; 
} 
printf("nThe Sorted array is :n"); 
for(i = 1; i <= n; i++) 
printf("t%d",B[i]); 
} 
void main() 
{ 
clrscr(); 
int n,i,k = 0, A[15]; 
printf("ttCOUNTING SORT ALGORITHMnnnn"); 
printf("Enter the number of input : "); 
scanf("%d",&n); 
printf("nnEnter the elements to be sorted :n"); 
for ( i = 1; i <= n; i++) 
{ 
scanf("%d",&A[i]); 
if(A[i] > k) 
{ 
k = A[i]; 
} 
} 
Counting_sort(A, k, n); 
getch(); 
}
DAA Lab File C Programs
Program 6 
Write a program to perform merge sort. 
#include <stdio.h> 
#include<conio.h> 
void mergesort(int arr[], int l, int h); 
void main(void) 
{ 
int array[100],n,i = 0; 
clrscr(); 
printf("tttMerge Sortnnnn"); 
printf("Enter the number of elements to be sorted: "); 
scanf("%d",&n); 
printf("nEnter the elements to be sorted: n"); 
for(i = 0 ; i < n ; i++ ) 
{ 
printf("tArray[%d] = ",i); 
scanf("%d",&array[i]); 
} 
printf("nBefore Mergesort:"); 
for(i = 0; i < n; i++) 
{ 
printf("%4d", array[i]); 
} 
printf("n"); 
mergesort(array, 0, n - 1); 
printf("nAfter Mergesort:"); 
for(i = 0; i < n; i++) 
{ 
printf("%4d", array[i]); 
} 
printf("n"); 
getch(); 
} 
void mergesort(int arr[], int l, int h) 
{ 
int i = 0; 
int length = h - l + 1; 
int pivot = 0; 
int merge1 = 0; 
int merge2 = 0; 
int temp[100]; 
if(l == h) 
return; 
pivot = (l + h) / 2; 
mergesort(arr, l, pivot); 
mergesort(arr, pivot + 1, h); 
for(i = 0; i < length; i++) 
{ 
temp[i] = arr[l + i]; 
} 
merge1 = 0; 
merge2 = pivot - l + 1;
for(i = 0; i < length; i++) 
{ 
if(merge2 <= h - l) 
{ 
if(merge1 <= pivot - l) 
{ 
if(temp[merge1] > temp[merge2]) 
{ 
arr[i + l] = temp[merge2++]; 
} 
else 
{ 
arr[i + l] = temp[merge1++]; 
} 
} 
else 
{ 
arr[i + l] = temp[merge2++]; 
} 
} 
else 
{ 
arr[i + l] = temp[merge1++]; 
} 
} 
}
DAA Lab File C Programs
Program 7 
Write a program to perform radix sort. 
#include<stdio.h> 
#include<conio.h> 
radix_sort(int array[], int n); 
void main() 
{ 
int array[100],n,i; 
clrscr(); 
printf("tttRadix Sortnnnn"); 
printf("Enter the number of elements to be sorted: "); 
scanf("%d",&n); 
printf("nEnter the elements to be sorted: n"); 
for(i = 0 ; i < n ; i++ ) 
{ 
printf("tArray[%d] = ",i); 
scanf("%d",&array[i]); 
} 
printf("nArray Before Radix Sort:"); //Array Before Radix Sort 
for(i = 0; i < n; i++) 
{ 
printf("%8d", array[i]); 
} 
printf("n"); 
radix_sort(array,n); 
printf("nArray After Radix Sort: "); //Array After Radix Sort 
for(i = 0; i < n; i++) 
{ 
printf("%8d", array[i]); 
} 
printf("n"); 
getch(); 
} 
radix_sort(int arr[], int n) 
{ 
int bucket[10][5],buck[10],b[10]; 
int i,j,k,l,num,div,large,passes; 
div=1; 
num=0; 
large=arr[0]; 
for(i=0 ; i<n ; i++) 
{ 
if(arr[i] > large) 
{ 
large = arr[i]; 
} 
while(large > 0)
{ 
num++; 
large = large/10; 
} 
for(passes=0 ; passes<num ; passes++) 
{ 
for(k=0 ; k<10 ; k++) 
{ 
buck[k] = 0; 
} 
for(i=0 ; i<n ;i++) 
{ 
l = ((arr[i]/div)%10); 
bucket[l][buck[l]++] = arr[i]; 
} 
i=0; 
for(k=0 ; k<10 ; k++) 
{ 
for(j=0 ; j<buck[k] ; j++) 
{ 
arr[i++] = bucket[k][j]; 
} 
} 
div*=10; 
} 
} 
return 0; 
}
DAA Lab File C Programs
Program 8 
Write a program to perform Knapsack Problem using Greedy Solution 
#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); 
} 
void main() 
{ 
clrscr(); 
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 weights and profits of each object:- "); 
for (i = 0; i < num; i++) 
{ 
scanf("%f %f", &weight[i], &profit[i]); 
} 
printf("nEnter the capacity 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); 
getch(); 
}
DAA Lab File C Programs
Program 9 
Write a program to perform Travelling Salesman Problem 
#include<stdio.h> 
#include<conio.h> 
#include<stdlib.h> 
int a[10][10],visited[10],n,cost=0; 
void get() 
{ 
int i,j; 
printf("nnEnter Number of Cities: "); 
scanf("%d",&n); 
printf("nEnter Cost Matrix: n"); 
for( i=0;i<n;i++) 
{ 
printf("n Enter Elements of Row # : %dn",i+1); 
for( j=0;j<n;j++) 
scanf("%d",&a[i][j]); 
visited[i]=0; 
} 
printf("nnThe Cost Matrix is:n"); 
for( i=0;i<n;i++) 
{ 
printf("nn"); 
for(j=0;j<n;j++) 
printf("t%d",a[i][j]); 
} 
} 
void mincost(int city) 
{ 
int i,ncity,least(int city); 
visited[city]=1; 
printf("%d ===> ",city+1); 
ncity=least(city); 
if(ncity==999) 
{ 
ncity=0; 
printf("%d",ncity+1); 
cost+=a[city][ncity]; 
return; 
} 
mincost(ncity); 
} 
int least(int c) 
{ 
int i,nc=999; 
int min=999,kmin; 
for(i=0;i<n;i++)
{ 
if((a[c][i]!=0)&&(visited[i]==0)) 
if(a[c][i]<min) 
{ 
min=a[i][0]+a[c][i]; 
kmin=a[c][i]; 
nc=i; 
} 
} 
if(min!=999) 
cost+=kmin; 
return nc; 
} 
void put() 
{ 
printf("nnMinimum cost:"); 
printf("%d",cost); 
} 
void main() 
{ 
clrscr(); 
get(); 
printf("nnThe Path is:nn"); 
mincost(0); 
put(); 
getch(); 
}
DAA Lab File C Programs
Program 10 
Write a program to find Minimum Spanning Tree using Kruskal’s Algorithm 
#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) 
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("ntEnter the No. of Nodes : "); 
scanf("%d",&n); 
for(i=0;i<n;i++) 
{ 
printf("ntEnter %d value : ",i+1); 
fflush(stdin); 
scanf("%c",&vertex[i]); 
for(j=0;j<n;j++) 
{ 
wght[i][j]=INF; 
span_wght[i][j]=INF; 
} 
} 
printf("nnGetting Weightn"); 
for(i=0;i<n;i++) 
for(j=i+1;j<n;j++) 
{ 
printf("nEnter 0 if path Doesn't exist between %c to %c : ",vertex[i],vertex[j]); 
scanf("%d",&ed); 
if(ed>=1) 
{ 
wght[i][j]=wght[j][i]=ed; 
que[r].v1=i; 
que[r].v2=j; 
que[r].weight=wght[i][j]; 
if(r) 
{ 
for(k=0;k<r;k++) 
if(que[k].weight>que[r].weight) 
{ 
swap(&que[k].weight,&que[r].weight); 
swap(&que[k].v1,&que[r].v1); 
swap(&que[k].v2,&que[r].v2); 
} 
} 
r++; 
} 
} 
clrscr(); 
printf("ntORIGINAL GRAPH WEIGHT MATRIXnn");
printf("ntweight matrixnnt"); 
for(i=0;i<n;i++,printf("nt")) 
for(j=0;j<n;j++,printf("t")) 
printf("%d",wght[i][j]); 
build_tree(); 
printf("nnttMINIMUM SPANNING TREEnn"); 
printf("nttLIST OF EDGESnn"); 
for(i=0;i<n;i++) 
for(j=i+1;j<n;j++) 
if(span_wght[i][j]!=INF) 
{ 
printf("ntt%c ------ %c = %d ",vertex[i],vertex[j],span_wght[i][j]); 
sum+=span_wght[i][j]; 
} 
printf("nnttTotal Weight : %d ",sum); 
getch(); 
}
DAA Lab File C Programs
Program 11 
Write a program to implement N Queen Problem using Backtracking 
#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++) 
{ 
for (j = 0; j < n; j++) 
printf("%ct", a[i][j]); 
printf("nn"); 
} 
} 
int getmarkedcol(int row) 
{ 
int i; 
for (i = 0; i < n; i++) 
if (a[row][i] == 'Q') 
{ 
return (i); 
break; 
} 
} 
int feasible(int row, int col) 
{ 
int i, tcol; 
for (i = 0; i < n; i++) 
{ 
tcol = getmarkedcol(i); 
if (col == tcol || abs(row - i) == abs(col - tcol)) 
return 0; 
} 
return 1; 
} 
void nqueen(int row) 
{ 
int i, j; 
if (row < n) 
{ 
for (i = 0; i < n; i++) 
{ 
if (feasible(row, i))
{ 
a[row][i] = 'Q'; 
nqueen(row + 1); 
a[row][i] = '.'; 
} 
} 
} 
else 
{ 
printf("nThe solution is:- "); 
printmatrix(); 
} 
} 
void main() 
{ 
clrscr(); 
int i, j; 
printf("nEnter the no. of queens:- "); 
scanf("%d", &n); 
for (i = 0; i < n; i++) 
for (j = 0; j < n; j++) 
a[i][j] = '.'; 
nqueen(0); 
getch(); 
}
DAA Lab File C Programs
Ad

More Related Content

What's hot (20)

Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
ShahDhruv21
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
PyData
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
Rahul Chugh
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
Narayan Sau
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
sumitbardhan
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
Kandarp Tiwari
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
Protap Mondal
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
Abhishek Singh
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
Dr Shashikant Athawale
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
Saga Valsalan
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
Self-Employed
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
Rajendra Dangwal
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
srutisenpatra
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
Amrinder Arora
 
CS8651 Internet Programming - Basics of HTML, HTML5, CSS
CS8651   Internet Programming - Basics of HTML, HTML5, CSSCS8651   Internet Programming - Basics of HTML, HTML5, CSS
CS8651 Internet Programming - Basics of HTML, HTML5, CSS
Vigneshkumar Ponnusamy
 
Python programming : List and tuples
Python programming : List and tuplesPython programming : List and tuples
Python programming : List and tuples
Emertxe Information Technologies Pvt Ltd
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
Mary Margarat
 
Topological Sorting
Topological SortingTopological Sorting
Topological Sorting
ShahDhruv21
 
Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)Introduction to NumPy (PyData SV 2013)
Introduction to NumPy (PyData SV 2013)
PyData
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
Lemia Algmri
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
Rahul Chugh
 
Data structure and algorithm using java
Data structure and algorithm using javaData structure and algorithm using java
Data structure and algorithm using java
Narayan Sau
 
Algorithm analysis
Algorithm analysisAlgorithm analysis
Algorithm analysis
sumitbardhan
 
All pairs shortest path algorithm
All pairs shortest path algorithmAll pairs shortest path algorithm
All pairs shortest path algorithm
Srikrishnan Suresh
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 
Os lab file c programs
Os lab file c programsOs lab file c programs
Os lab file c programs
Kandarp Tiwari
 
15 puzzle problem using branch and bound
15 puzzle problem using branch and bound15 puzzle problem using branch and bound
15 puzzle problem using branch and bound
Abhishek Singh
 
Prims and kruskal algorithms
Prims and kruskal algorithmsPrims and kruskal algorithms
Prims and kruskal algorithms
Saga Valsalan
 
Infix prefix postfix
Infix prefix postfixInfix prefix postfix
Infix prefix postfix
Self-Employed
 
Merge sort algorithm
Merge sort algorithmMerge sort algorithm
Merge sort algorithm
srutisenpatra
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
Amrinder Arora
 
CS8651 Internet Programming - Basics of HTML, HTML5, CSS
CS8651   Internet Programming - Basics of HTML, HTML5, CSSCS8651   Internet Programming - Basics of HTML, HTML5, CSS
CS8651 Internet Programming - Basics of HTML, HTML5, CSS
Vigneshkumar Ponnusamy
 
Data Structures and Algorithm Analysis
Data Structures  and  Algorithm AnalysisData Structures  and  Algorithm Analysis
Data Structures and Algorithm Analysis
Mary Margarat
 

Viewers also liked (9)

Ada lab manual
Ada lab manualAda lab manual
Ada lab manual
sumansanketh
 
Daa practicals
Daa practicalsDaa practicals
Daa practicals
Rekha Yadav
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
Arvind Krishnaa
 
Unix practical file
Unix practical fileUnix practical file
Unix practical file
Soumya Behera
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
Sushil Mishra
 
Huminity and Social Science Lab Manual
Huminity and Social Science Lab ManualHuminity and Social Science Lab Manual
Huminity and Social Science Lab Manual
Sachin Airan
 
Algorithm Analysis and Design Class Notes
Algorithm Analysis and Design Class NotesAlgorithm Analysis and Design Class Notes
Algorithm Analysis and Design Class Notes
Kumar Avinash
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
Jenny Galino
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
bca2010
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
Arvind Krishnaa
 
c-programming-using-pointers
c-programming-using-pointersc-programming-using-pointers
c-programming-using-pointers
Sushil Mishra
 
Huminity and Social Science Lab Manual
Huminity and Social Science Lab ManualHuminity and Social Science Lab Manual
Huminity and Social Science Lab Manual
Sachin Airan
 
Algorithm Analysis and Design Class Notes
Algorithm Analysis and Design Class NotesAlgorithm Analysis and Design Class Notes
Algorithm Analysis and Design Class Notes
Kumar Avinash
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
bca2010
 
Ad

Similar to DAA Lab File C Programs (20)

Pnno
PnnoPnno
Pnno
shristichaudhary4
 
Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02Daapracticals 111105084852-phpapp02
Daapracticals 111105084852-phpapp02
Er Ritu Aggarwal
 
ADA FILE
ADA FILEADA FILE
ADA FILE
Gaurav Singh
 
Chapter 8 c solution
Chapter 8 c solutionChapter 8 c solution
Chapter 8 c solution
Azhar Javed
 
Cpds lab
Cpds labCpds lab
Cpds lab
praveennallavelly08
 
week-21x
week-21xweek-21x
week-21x
KITE www.kitecolleges.com
 
Ada file
Ada fileAda file
Ada file
Kumar Gaurav
 
Cpd lecture im 207
Cpd lecture im 207Cpd lecture im 207
Cpd lecture im 207
Syed Tanveer
 
Data Structure in C Programming Language
Data Structure in C Programming LanguageData Structure in C Programming Language
Data Structure in C Programming Language
Arkadeep Dey
 
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
vrgokila
 
Sorting programs
Sorting programsSorting programs
Sorting programs
Varun Garg
 
Ds
DsDs
Ds
kooldeep12345
 
Data Structure using C
Data Structure using CData Structure using C
Data Structure using C
Bilal Mirza
 
Array Programs.pdf
Array Programs.pdfArray Programs.pdf
Array Programs.pdf
RajKamal557276
 
C Prog - Pointers
C Prog - PointersC Prog - Pointers
C Prog - Pointers
vinay arora
 
COgdfgdffgdgfgdasdasdaffadadsdsd5ANS.pdf
COgdfgdffgdgfgdasdasdaffadadsdsd5ANS.pdfCOgdfgdffgdgfgdasdasdaffadadsdsd5ANS.pdf
COgdfgdffgdgfgdasdasdaffadadsdsd5ANS.pdf
mbpgbca
 
C programs
C programsC programs
C programs
Koshy Geoji
 
programs on arrays.pdf
programs on arrays.pdfprograms on arrays.pdf
programs on arrays.pdf
sowmya koneru
 
SaraPIC
SaraPICSaraPIC
SaraPIC
Sara Sahu
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
PRATHAMESH DESHPANDE
 
Ad

More from Kandarp Tiwari (12)

Artificial Intelligence Lab File
Artificial Intelligence Lab FileArtificial Intelligence Lab File
Artificial Intelligence Lab File
Kandarp Tiwari
 
Speed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariSpeed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp Tiwari
Kandarp Tiwari
 
Speed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariSpeed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp Tiwari
Kandarp Tiwari
 
Web Technology Lab File
Web Technology Lab FileWeb Technology Lab File
Web Technology Lab File
Kandarp Tiwari
 
Web Technology Front Page
Web Technology Front PageWeb Technology Front Page
Web Technology Front Page
Kandarp Tiwari
 
Web technology
Web technologyWeb technology
Web technology
Kandarp Tiwari
 
Compiler design front page
Compiler design front pageCompiler design front page
Compiler design front page
Kandarp Tiwari
 
Computer Networks Front Page
Computer Networks Front PageComputer Networks Front Page
Computer Networks Front Page
Kandarp Tiwari
 
Computer Networks Lab File
Computer Networks Lab FileComputer Networks Lab File
Computer Networks Lab File
Kandarp Tiwari
 
Compiler Design Lab File
Compiler Design Lab FileCompiler Design Lab File
Compiler Design Lab File
Kandarp Tiwari
 
Java Programs Lab File
Java Programs Lab FileJava Programs Lab File
Java Programs Lab File
Kandarp Tiwari
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
Kandarp Tiwari
 
Artificial Intelligence Lab File
Artificial Intelligence Lab FileArtificial Intelligence Lab File
Artificial Intelligence Lab File
Kandarp Tiwari
 
Speed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariSpeed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp Tiwari
Kandarp Tiwari
 
Speed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp TiwariSpeed Detecting Camera by Kandarp Tiwari
Speed Detecting Camera by Kandarp Tiwari
Kandarp Tiwari
 
Web Technology Lab File
Web Technology Lab FileWeb Technology Lab File
Web Technology Lab File
Kandarp Tiwari
 
Web Technology Front Page
Web Technology Front PageWeb Technology Front Page
Web Technology Front Page
Kandarp Tiwari
 
Compiler design front page
Compiler design front pageCompiler design front page
Compiler design front page
Kandarp Tiwari
 
Computer Networks Front Page
Computer Networks Front PageComputer Networks Front Page
Computer Networks Front Page
Kandarp Tiwari
 
Computer Networks Lab File
Computer Networks Lab FileComputer Networks Lab File
Computer Networks Lab File
Kandarp Tiwari
 
Compiler Design Lab File
Compiler Design Lab FileCompiler Design Lab File
Compiler Design Lab File
Kandarp Tiwari
 
Java Programs Lab File
Java Programs Lab FileJava Programs Lab File
Java Programs Lab File
Kandarp Tiwari
 
Computer Graphics Lab File C Programs
Computer Graphics Lab File C ProgramsComputer Graphics Lab File C Programs
Computer Graphics Lab File C Programs
Kandarp Tiwari
 

Recently uploaded (20)

K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
Presentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem KayaPresentation of the MIPLM subject matter expert Erdem Kaya
Presentation of the MIPLM subject matter expert Erdem Kaya
MIPLM
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025Stein, Hunt, Green letter to Congress April 2025
Stein, Hunt, Green letter to Congress April 2025
Mebane Rash
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
P-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 finalP-glycoprotein pamphlet: iteration 4 of 4 final
P-glycoprotein pamphlet: iteration 4 of 4 final
bs22n2s
 
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Michelle Rumley & Mairéad Mooney, Boole Library, University College Cork. Tra...
Library Association of Ireland
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
Metamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative JourneyMetamorphosis: Life's Transformative Journey
Metamorphosis: Life's Transformative Journey
Arshad Shaikh
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Introduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe EngineeringIntroduction to Vibe Coding and Vibe Engineering
Introduction to Vibe Coding and Vibe Engineering
Damian T. Gordon
 

DAA Lab File C Programs

  • 1. Program 1 Write a program to perform insertion sort. #include<stdio.h> #include<conio.h> void main() { clrscr(); int i,t,a[100],d,n; printf("Enter The No. Of Elementsn"); scanf("%d",&n); printf("Enter %d Integersn",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } 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("%dt",a[i]); } getch(); }
  • 3. Program 2 Write a program to perform selection sort. #include<stdio.h> #include<conio.h> void main() { clrscr(); int i,position,swap,a[100],d,n; printf("Enter The No. Of Elementsn"); scanf("%d",&n); printf("Enter %d Integersn",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } for(i=0;i<(n-1);i++) { position=i; for(d=c+1;d<n;d++) { if(array[position]>array[d]) position=d; } if(position!=i) { swap=a[i]; a[i]=a[position]; a[position]=swap; } } printf("Sorted List in Ascending Order is:n"); for(i=0;i<=n;i++) { printf("%dt",a[i]); } getch(); }
  • 5. Program 3 Write a program to perform heap sort. #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]); } heapsort(array,n); printf("Sorted list in ascending order using heap sort is:n"); for(i = 0; i < n; i++) { printf("%dt", 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;
  • 6. 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; j = 0; item = array[j]; i = 2*j+1; while(i<=n-1) { if(i+1 <= n-1) if(array[i] < array[i+1]) i++; if(item < array[i]) { array[j] = array[i]; j = i; i = 2*j+1; } else break; } array[j] = item; }
  • 8. Program 4 Write a program to perform quick sort. #include<stdio.h> #include<conio.h> void quicksort(int [10],int,int); void main() { clrscr(); int a[20],n,i; printf("Enter size of the array:n"); scanf("%d",&n); printf("Enter %d elements:n",n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } quicksort(a,0,n-1); printf("Sorted elements:n "); for(i=0;i<n;i++) { printf("t%d",a[i]); } getch(); } void quicksort(int a[10],int first,int last) { int pivot,j,temp,i; if(first<last) { pivot=first; i=first; j=last; while(i<j) { while(a[i]<=a[pivot]&&i<last) i++; while(a[j]>a[pivot]) j--; if(i<j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } temp=a[pivot]; a[pivot]=a[j]; a[j]=temp; quicksort(a,first,j-1); quicksort(a,j+1,last); } }
  • 10. Program 5 Write a program to perform counting sort. #include<stdio.h> #include<conio.h> void Counting_sort(int A[], int k, int n) { int i, j; int B[15], C[100]; for(i = 0; i <= k; i++) C[i] = 0; for(j =1; j <= n; j++) C[A[j]] = C[A[j]] + 1; for(i = 1; i <= k; i++) C[i] = C[i] + C[i-1]; for(j = n; j >= 1; j--) { B[C[A[j]]] = A[j]; C[A[j]] = C[A[j]] - 1; } printf("nThe Sorted array is :n"); for(i = 1; i <= n; i++) printf("t%d",B[i]); } void main() { clrscr(); int n,i,k = 0, A[15]; printf("ttCOUNTING SORT ALGORITHMnnnn"); printf("Enter the number of input : "); scanf("%d",&n); printf("nnEnter the elements to be sorted :n"); for ( i = 1; i <= n; i++) { scanf("%d",&A[i]); if(A[i] > k) { k = A[i]; } } Counting_sort(A, k, n); getch(); }
  • 12. Program 6 Write a program to perform merge sort. #include <stdio.h> #include<conio.h> void mergesort(int arr[], int l, int h); void main(void) { int array[100],n,i = 0; clrscr(); printf("tttMerge Sortnnnn"); printf("Enter the number of elements to be sorted: "); scanf("%d",&n); printf("nEnter the elements to be sorted: n"); for(i = 0 ; i < n ; i++ ) { printf("tArray[%d] = ",i); scanf("%d",&array[i]); } printf("nBefore Mergesort:"); for(i = 0; i < n; i++) { printf("%4d", array[i]); } printf("n"); mergesort(array, 0, n - 1); printf("nAfter Mergesort:"); for(i = 0; i < n; i++) { printf("%4d", array[i]); } printf("n"); getch(); } void mergesort(int arr[], int l, int h) { int i = 0; int length = h - l + 1; int pivot = 0; int merge1 = 0; int merge2 = 0; int temp[100]; if(l == h) return; pivot = (l + h) / 2; mergesort(arr, l, pivot); mergesort(arr, pivot + 1, h); for(i = 0; i < length; i++) { temp[i] = arr[l + i]; } merge1 = 0; merge2 = pivot - l + 1;
  • 13. for(i = 0; i < length; i++) { if(merge2 <= h - l) { if(merge1 <= pivot - l) { if(temp[merge1] > temp[merge2]) { arr[i + l] = temp[merge2++]; } else { arr[i + l] = temp[merge1++]; } } else { arr[i + l] = temp[merge2++]; } } else { arr[i + l] = temp[merge1++]; } } }
  • 15. Program 7 Write a program to perform radix sort. #include<stdio.h> #include<conio.h> radix_sort(int array[], int n); void main() { int array[100],n,i; clrscr(); printf("tttRadix Sortnnnn"); printf("Enter the number of elements to be sorted: "); scanf("%d",&n); printf("nEnter the elements to be sorted: n"); for(i = 0 ; i < n ; i++ ) { printf("tArray[%d] = ",i); scanf("%d",&array[i]); } printf("nArray Before Radix Sort:"); //Array Before Radix Sort for(i = 0; i < n; i++) { printf("%8d", array[i]); } printf("n"); radix_sort(array,n); printf("nArray After Radix Sort: "); //Array After Radix Sort for(i = 0; i < n; i++) { printf("%8d", array[i]); } printf("n"); getch(); } radix_sort(int arr[], int n) { int bucket[10][5],buck[10],b[10]; int i,j,k,l,num,div,large,passes; div=1; num=0; large=arr[0]; for(i=0 ; i<n ; i++) { if(arr[i] > large) { large = arr[i]; } while(large > 0)
  • 16. { num++; large = large/10; } for(passes=0 ; passes<num ; passes++) { for(k=0 ; k<10 ; k++) { buck[k] = 0; } for(i=0 ; i<n ;i++) { l = ((arr[i]/div)%10); bucket[l][buck[l]++] = arr[i]; } i=0; for(k=0 ; k<10 ; k++) { for(j=0 ; j<buck[k] ; j++) { arr[i++] = bucket[k][j]; } } div*=10; } } return 0; }
  • 18. Program 8 Write a program to perform Knapsack Problem using Greedy Solution #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); } void main() { clrscr(); 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 weights and profits of each object:- "); for (i = 0; i < num; i++) { scanf("%f %f", &weight[i], &profit[i]); } printf("nEnter the capacity of knapsack:- "); scanf("%f", &capacity); for (i = 0; i < num; i++) {
  • 19. 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); getch(); }
  • 21. Program 9 Write a program to perform Travelling Salesman Problem #include<stdio.h> #include<conio.h> #include<stdlib.h> int a[10][10],visited[10],n,cost=0; void get() { int i,j; printf("nnEnter Number of Cities: "); scanf("%d",&n); printf("nEnter Cost Matrix: n"); for( i=0;i<n;i++) { printf("n Enter Elements of Row # : %dn",i+1); for( j=0;j<n;j++) scanf("%d",&a[i][j]); visited[i]=0; } printf("nnThe Cost Matrix is:n"); for( i=0;i<n;i++) { printf("nn"); for(j=0;j<n;j++) printf("t%d",a[i][j]); } } void mincost(int city) { int i,ncity,least(int city); visited[city]=1; printf("%d ===> ",city+1); ncity=least(city); if(ncity==999) { ncity=0; printf("%d",ncity+1); cost+=a[city][ncity]; return; } mincost(ncity); } int least(int c) { int i,nc=999; int min=999,kmin; for(i=0;i<n;i++)
  • 22. { if((a[c][i]!=0)&&(visited[i]==0)) if(a[c][i]<min) { min=a[i][0]+a[c][i]; kmin=a[c][i]; nc=i; } } if(min!=999) cost+=kmin; return nc; } void put() { printf("nnMinimum cost:"); printf("%d",cost); } void main() { clrscr(); get(); printf("nnThe Path is:nn"); mincost(0); put(); getch(); }
  • 24. Program 10 Write a program to find Minimum Spanning Tree using Kruskal’s Algorithm #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) 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) {
  • 25. int t; t=*i; *i=*j; *j=t; } void main() { int i,j,k=0,temp; int sum=0; clrscr(); printf("ntEnter the No. of Nodes : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("ntEnter %d value : ",i+1); fflush(stdin); scanf("%c",&vertex[i]); for(j=0;j<n;j++) { wght[i][j]=INF; span_wght[i][j]=INF; } } printf("nnGetting Weightn"); for(i=0;i<n;i++) for(j=i+1;j<n;j++) { printf("nEnter 0 if path Doesn't exist between %c to %c : ",vertex[i],vertex[j]); scanf("%d",&ed); if(ed>=1) { wght[i][j]=wght[j][i]=ed; que[r].v1=i; que[r].v2=j; que[r].weight=wght[i][j]; if(r) { for(k=0;k<r;k++) if(que[k].weight>que[r].weight) { swap(&que[k].weight,&que[r].weight); swap(&que[k].v1,&que[r].v1); swap(&que[k].v2,&que[r].v2); } } r++; } } clrscr(); printf("ntORIGINAL GRAPH WEIGHT MATRIXnn");
  • 26. printf("ntweight matrixnnt"); for(i=0;i<n;i++,printf("nt")) for(j=0;j<n;j++,printf("t")) printf("%d",wght[i][j]); build_tree(); printf("nnttMINIMUM SPANNING TREEnn"); printf("nttLIST OF EDGESnn"); for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(span_wght[i][j]!=INF) { printf("ntt%c ------ %c = %d ",vertex[i],vertex[j],span_wght[i][j]); sum+=span_wght[i][j]; } printf("nnttTotal Weight : %d ",sum); getch(); }
  • 28. Program 11 Write a program to implement N Queen Problem using Backtracking #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++) { for (j = 0; j < n; j++) printf("%ct", a[i][j]); printf("nn"); } } int getmarkedcol(int row) { int i; for (i = 0; i < n; i++) if (a[row][i] == 'Q') { return (i); break; } } int feasible(int row, int col) { int i, tcol; for (i = 0; i < n; i++) { tcol = getmarkedcol(i); if (col == tcol || abs(row - i) == abs(col - tcol)) return 0; } return 1; } void nqueen(int row) { int i, j; if (row < n) { for (i = 0; i < n; i++) { if (feasible(row, i))
  • 29. { a[row][i] = 'Q'; nqueen(row + 1); a[row][i] = '.'; } } } else { printf("nThe solution is:- "); printmatrix(); } } void main() { clrscr(); int i, j; printf("nEnter the no. of queens:- "); scanf("%d", &n); for (i = 0; i < n; i++) for (j = 0; j < n; j++) a[i][j] = '.'; nqueen(0); getch(); }