You are on page 1of 20

Experiment No: 1

Selection Sort
Code:
#include<stdio.h>
#include<conio.h>
void main(){
int size,i,j,temp,list[100];
printf("Dhruva Kawli\n");
printf("Enter the size of the list:");
scanf("%d",&size);
printf("Enter %d integer
values:",size); for(i=0;i<size;i++)
scanf("%d",&list[i]);
for(i=0;i<size;i++){
for(j=i+1;j<size;j++){
if(list[i]>list[j])
{
temp=list[i];
list[i]=list[j];
list[j]=temp;
}
}
}
printf("list after sorting is:");
for(i=0;i<size;i++)
printf("\t%d",list[i]);
getch();
}
Output:

Insertion Sort
Code:
#include<stdio.h>
#include<conio.h>
void main(){
int size,i,j,temp,list[100];
printf("Dhruva Kawli\n");
printf("Enter the size of the
list:"); scanf("%d",&size);
printf("Enter %d integer
values:",size); for(i=0;i<size;i++)
scanf("%d",&list[i]);
for(i=0;i<size;i++){
temp=list[i];
j=i-1;
while((temp<list[j])&&(j>=0)){ list[j+1
]=list[j];
j=j-1;
}
list[j+1]=temp;
}
printf("list after sorting
is:"); for(i=0;i<size;i++)
printf("\t%d",list[i]);
getch();
}
Output:

Experiment No:2
Merge Sort
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void
merge_sort(int I, int j, int a[], int temp[]) {
if(j<=i)
{
return;
}
int mid = (i+j)/2;
merge_sort(I,mid,a,temp);
merge_sort(mid+1,j,a,temp);
int left=I;
int right=mid+1;
int k;
for (k = I; k <= j; k++)
{
if (left == mid + 1)
{
temp[k] = a[right];
right++;
}
else if (right == j + 1)
{
temp[k] = a[left];
left++;
} else if (a[left] < a[right])
{
temp[k] = a[left];
left++;
} else
{
temp[k] = a[right];
right++;
}
}
for (k=I;k<=j;k++)
{
a[k] = temp[k];
}
}
int main()
{
int a[50], temp[50], n, I, d, swap;
printf(“Dhruva Kawli\n”);
printf(“Enter size of an array : “);
scanf(“%d”, &n);
printf(“Enter the %d elements for sorting \n”,
n); for (I = 0; I < n; i++)
scanf(“%d”, &a[i]);
merge_sort(0, n – 1, a, temp);
printf(“Sorted array:\n”);
for (I = 0; I < n; i++)
printf(“%d “, a[i]);
return 0;
}
Output:

Quick sort
Code:
#include<stdio.h>
void quicksort(int number[25],int first,int
last){ int I, j, pivot, temp;
if(first<last){
pivot=first;
i=first;
j=last;
while(i<j){
while(number[i]<=number[pivot]&&i<la
st) i++;
while(number[j]>number[pivot])
j--;
if(i<j){
temp=number[i];
number[i]=number[j];
number[j]=temp;
}
}
temp=number[pivot];
number[pivot]=number[j];
number[j]=temp;
quicksort(number,first,j-1);
quicksort(number,j+1,last);
}
}
int main(){
int I, count, number[25];
printf(“Dhruva Kawli\n”);
printf(“Enter number of elements: “);
scanf(“%d”,&count);
printf(“Enter %d elements: “, count);
for(i=0;i<count;i++)
scanf(“%d”,&number[i]);
quicksort(number,0,count-1);
printf(“The Sorted Order is: “);
for(i=0;i<count;i++)
printf(“ %d”,number[i]);
return 0;
}
Output:

Experiment No 3
Code:-

#include<stdio.h>
#include<conio.h>
#define INFINITY 9999
#define MAX 10

void dijkstra(int G[MAX][MAX],int n,int startnode);

int main()
{
int G[MAX][MAX],i,j,n,u;
printf("Enter no. 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]);
printf("\nEnter the starting node:");
scanf("%d",&u);
dijkstra(G,n,u);
return 0;
}

void dijkstra(int G[MAX][MAX],int n,int startnode) {


int cost[MAX][MAX],distance[MAX],pred[MAX];
int visited[MAX],count,mindistance,nextnode,i,j; //pred[] stores
the predecessor of each node //count gives the number of nodes seen so
far //create the cost matrix
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(G[i][j]==0)
cost[i][j]=INFINITY;
else
cost[i][j]=G[i][j];
//initialize pred[],distance[] and visited[] for(i=0;i<n;i++) {
distance[i]=cost[startnode][i];
pred[i]=startnode;
visited[i]=0;
}
distance[startnode]=0;
visited[startnode]=1;
count=1;
while(count<n-1)
{
mindistance=INFINITY;
//nextnode gives the node at minimum distance for(i=0;i<n;i++)
if(distance[i]<mindistance&&!visited[i]) {
mindistance=distance[i];
nextnode=i;
}
//check if a better path exists through nextnode visited[nextnode]=1;
for(i=0;i<n;i++)
if(!visited[i])
if(mindistance+cost[nextnode][i]<distance[i]) {
distance[i]=mindistance+cost[nextnode][i]; pred[i]=nextnode; }
count++;
}

//print the path and distance of each node for(i=0;i<n;i++)


if(i!=startnode)
{
printf("\nDistance of node%d=%d",i,distance[i]);
printf("\nPath=%d",i);
j=i;
do
{
j=pred[j];
printf("<-%d",j);
}while(j!=startnode);
}
}
OUTPUT:-

Enter no. of vertices:5

Enter the adjacency matrix: 0 10 3 0 0


00120
04082
00007
00090

Enter the starting node:0


Distance of node1=7
Path=1<-2<-0
Distance of node2=3
Path=2<-0
Distance of node3=9
Path=3<-1<-2<-0
Distance of node4=5
Path=4<-2<-0

Experiment no. 4

Code:

# include<stdio.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("%f\t", 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);
}

Output:

Enter the no. of objects:- 7

Enter the wts and profits of each object:-


15
18
24
3 10
39
47
13

Enter the capacityacity of knapsack:- 15

The result vector is:- 1.000000


1.000000 1.000000 1.000000 1.000000 1.000000 1.000000 Maximum
profit is:- 46.000000
Experiment No: 5
Program:

#include <stdio.h>

void main()
{
int a[100][100],n,i,j,k;

printf("Dhruva Kawli\n");
printf("Roll no: 57\n");
printf("Enter the number of rows and column:"); scanf("%d", &n);

printf("Enter the matrix:\n");

for(i=0; i< n; i++)


{
for(j=0; j< n; j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The Entered matrix is:\n");
for(i=0; i< n; i++)
{
for(j=0; j< n; j++)
{
printf("%d ", a[i][j]);
}

printf("\n");
}

for(i=0; i< n; i++)


{
for(j=0; j< n; j++)
{
for(k=0; k< n; k++)
{
if(a[i][j] > a[i][k] + a[k][j])
{
a[i][j] = a[i][k] + a[k][j];
}
else
{
a[i][j] = a[i][j];
}
}
}
}

printf("output after calculation is: \n");


for(i=0; i< n; i++)
{
for(j=0; j<n; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}

Output:

Dhruva Kawli
Roll no: 57
Enter the number of rows and column:3 Enter the matrix:
0 4 11
602
3 9999 0
The Entered matrix is:
0 4 11
602
3 9999 0
output after calculation is:
046
502
370
Experiment No: 6
Program:

#include <stdio.h>

void main()
{
int a[100][100],n,i,j,k;

printf("Dhruva Kawli\n");
printf("Roll no: 57\n");
printf("Enter the number of rows and column:"); scanf("%d", &n);

printf("Enter the matrix:\n");

for(i=0; i< n; i++)


{
for(j=0; j< n; j++)
{
scanf("%d",&a[i][j]);
}
}
printf("The Entered matrix is:\n");
for(i=0; i< n; i++)
{
for(j=0; j< n; j++)
{
printf("%d ", a[i][j]);
}

printf("\n");
}

for(i=0; i< n; i++)


{
for(j=0; j< n; j++)
{
for(k=0; k< n; k++)
{
if(a[i][j] > a[i][k] + a[k][j])
{
a[i][j] = a[i][k] + a[k][j];
}
else
{
a[i][j] = a[i][j];
}
}
}
}

printf("output after calculation is: \n");


for(i=0; i< n; i++)
{
for(j=0; j<n; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}

Output:

Dhruva Kawli
Roll no: 57
Enter the number of rows and column:3 Enter the matrix:
0 4 11
602
3 9999 0
The Entered matrix is:
0 4 11
602
3 9999 0
output after calculation is:
046
502
370

Experiment No: 7
Code:
#include<stdio.h>
#include<string.h>
#define d 256
void search(char pat[], char txt[], int q) {
int M = strlen(pat); int N = strlen(txt); int i, j; int p = 0; int t = 0; int h = 1;
for (i = 0; i < M-1; i++)
h = (h*d)%q;
for (i = 0; i < M; i++)
{
p = (d*p + pat[i])%q;
t = (d*t + txt[i])%q;
}
for (i = 0; i <= N - M; i++)
{
if ( p == t )
{
for (j = 0; j < M; j++)
{
if (txt[i+j] != pat[j])
break;
}
if (j == M)
printf("Pattern found at index %d \n", i); }
if ( i < N-M )
{
t = (d*(t - txt[i]*h) + txt[i+M])%q; if (t < 0)
t = (t + q);
}
}
}
int main()
{ char txt[80],pat[80];
int q;
printf("Enter some text \n");
scanf("%s",txt);
printf("Enter a pattern to be searched \n"); scanf("%s",&pat);
printf("Enter a prime number \n"); scanf("%d",&q);
search(pat, txt, q);
return 0;
}

Output:
Enter some text
2359023141526739921
Enter a pattern to be searched 31415
Enter a prime number
13
Pattern found at index 6
Experiment No 8
Code:
#include <stdbool.h>
#include <stdio.h>
void printSolution(int board[10][10],int N) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++)
printf(" %d ", board[i][j]);
printf("\n");
}
}
bool isSafe(int board[10][10], int row, int col, int N) {
int i, j;
for (i = 0; i < col; i++)
if (board[row][i])
return false;
for (i = row, j = col; i >= 0 && j >= 0; i--, j--) if (board[i][j])
return false;
for (i = row, j = col; j >= 0 && i < N; i++, j--) if (board[i][j])
return false;
return true;
}
bool solveNQUtil(int board[10][10], int col, int N) {
if (col >= N)
return true;
for (int i = 0; i < N; i++) {

if (isSafe(board, i, col,N)) {
board[i][col] = 1;
if (solveNQUtil(board, col + 1,N))
return true;
board[i][col] = 0; // BACKTRACK
}
}
return false;
}
bool solveNQ()
{
int board[10][10], N,i,j;
printf("Enter the Size of board\n");
scanf("%d",&N);
for(i=0;i<N;i++)
{
for(j=0;j<N;j++)
{
board[i][j]=0;
}
}
if (solveNQUtil(board, 0,N) == false) { printf("Solution
does not exist"); return false;
}
printSolution(board,N);
return true;
}
int main()
{
solveNQ();
return 0;
}
Output:
Enter the Size of board 8
10000000 00000010 0
0001000 00000001 01
000000 00010000 000
00100 00100000

You might also like