0% found this document useful (0 votes)
7 views89 pages

Sodapdf Watermarked (10)

The document outlines the practical work record for the CS3401 Algorithms Laboratory at the University College of Engineering Villupuram for the academic year 2024-2025. It includes an index of various algorithms implemented, such as linear search, binary search, sorting algorithms, and graph traversal techniques, along with their respective program codes and results. The document serves as a comprehensive guide for students to document their laboratory work and practical examinations.

Uploaded by

firoz52797
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views89 pages

Sodapdf Watermarked (10)

The document outlines the practical work record for the CS3401 Algorithms Laboratory at the University College of Engineering Villupuram for the academic year 2024-2025. It includes an index of various algorithms implemented, such as linear search, binary search, sorting algorithms, and graph traversal techniques, along with their respective program codes and results. The document serves as a comprehensive guide for students to document their laboratory work and practical examinations.

Uploaded by

firoz52797
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 89

CS3401

ALGORITHMS LABORATORY

Name: _____________________________________________________
Reg No: ____________________________________________________
Department:________________________________________________
Subject:____________________________________________________

1
Bonafide record of work done in the Operating Systems Laboratory of
University College of Engineering Villupuram for CS3401 –Algorithms
Laboratory during the academic year 2024-2025 by ___________________
_ ________________________________________________________________________________________

Reg no. _____________________________________ studying in the fourth


semester B.E(Computer Science and Engineering).

Staff In-Charge Head of the Department

Submitted for the practical examination held at University College of


Engineering Villupuram on ________________________.

Internal Examiner External Examiner

2
INDEX

S.NO DATE TITLE PAGE.NO SIGNATURE

1. IMPLEMENTATION OF LINEAR
SEARCH

05
2. IMPLEMENTATION RECURSIVE
BINARY SEARCH.DETERMINE
THE TIME REQUIRED TO
SEARCH AN ELEMENT

40
10
3. FUNCTION SEARCH
23

4(A) INSERTION SORT


25

4(B) HEAP SORT


42

5(A) BREADTH FIRST SEARCH

5(B) DEPTH FIRST SEARCH

6(A) DIJKSTRA’S ALGORITHM

3
7(A) FLOYD’S ALGORITHM

7(B) WARSHALL’S ALGORITHM

8. DIVIDE AND CONQUER


TECHNIQUE

05
9(A) MERGE SORT

9(B) QUICK SORT

10
40
N-QUEENS PROBLEM USING
10
BACKTRACKING
23

11(A) TRAVELLING SALESPERSON


PROBLEM
25

11(B) IMPLEMENTATION
42

RANDOMIZED ALGORITHMS
FOR FINDING THE KTH
SMALLEST NUMBER

4
EX.NO: 1 IMPLEMENTATION OF LINEAR SEARCH
DATE:

05
40
10
23
25
42

5
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int LinearSearch(int, int[], int);
int main() {
int ch = 1;
double t;

05
int n, i, k, op, pos, a[20];
clock_t begin, end;
while (ch) {
40
printf("\n....MENU\n1. Linear search\n2. Exit\n");
10
printf("\nEnter your choice: ");
scanf("%d", &op);
23

switch (op) {
case 1:
25

printf("\nEnter the number of elements: ");


scanf("%d", &n);
42

printf("\nEnter the elements of the array:\n");


for (i = 0; i < n; i++)
scanf("%d", &a[i]);
printf("\nEnter the element to be searched: ");
scanf("%d", &k);
begin = clock();
pos = LinearSearch(n, a, k);

6
end=clock();
if(pos==-1)
printf(("\nUnsuccessful search\n");
else
printf("Element %d is found at position %d\n", k, pos + 1);
printf("Time taken is %.6f CPU cycles\n", (double)(end - begin) /
CLOCKS_PER_SEC);

05
break;
case 2:
exit(0);
break; 40
10
default:
printf("Invalid choice entered\n");
23

exit(0);
}
25

printf("\nDo you wish to run again (1/0)? ");


scanf("%d", &ch);
42

}
return 0;
}
int LinearSearch(int n, int a[], int k) {
int i;
for (i = 0; i < n; i++) {
if(a[i]==k)

7
return i;
}
Return -1;
}
}

05
40
10
23
25
42

8
RESULT:
42
25

9
23
10
40
05
EX.NO: 2 IMPLEMENTATION RECURSIVE BINARY SEARCH.
DATE: DETERMINE THE TIME REQUIRED TO SEARCH AN ELEMENT

05
40
10
23
25
42

10
PROGRAM:
#include <stdio.h>
#include <conio.h>
#include <time.h>
#include <stdlib.h>
#define MAX 20
int binsearch(int, int[], int, int, int);

05
int main() {
int ch = 1;
double t;
int n, i, a[20], e, low, high, pos;
40
10
clock_t begin, end;
clrscr();
23

printf("Enter the size of the array: ");


scanf("%d", &n);
25

printf("Enter the elements of the array: ");


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

scanf("%d", &a[i]);
printf("Enter the element to search: ");
scanf("%d", &e);
low = 0; high = n - 1;
begin = clock();
pos = binsearch(n, a, e, low, high); end = clock();
if (pos == -1)

11
printf(“Unsuccessful Search”):
else;
printf(“Element %d found at position %d”,e,pos+1);
printf("\nTime taken is %f if CPU cycles", (double)(end - begin) /
CLOCKS_PER_SEC);
getch();
return 0;

05
}
int binsearch(int n, int a[], int e, int low, int high) { int mid;
delay(1000);
mid = (low + high) / 2; 40
10
if (low > high) return -1;
if (e == a[mid]) return mid;
23

else if (e < a[mid])


return binsearch(n, a, e, low, mid - 1); else
25

return binsearch(n, a, e, mid + 1, high);


}
42

12
RESULT:
42
25

13
23
10
40
05
EX.NO: 3 FUNCTION SEARCH
DATE:

05
40
10
23
25
42

14
PROGRAM:
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>

05
void search(char pat[], char txt[]) { int i, j;
int m = strlen(pat);
int n = strlen(txt);
int count = 0;
40
10
for (i = 0; i <= n - m; i++) {
int j;
23

for (j = 0; j < m; j++) {


if (txt[i + j] != pat[j]) {
25

break;
}
42

}
if (j == m) {
printf("Pattern found at index %d\n", i);
count++;
}
}
if (count == 0) {

15
printf("Pattern not found in text.\n");
{
printf("Total occurrences: %d\n", count);
}
int main() {
char txt[] = "AABAACAADAABAAABAA";
char pat[] = "AABA";

05
printf("Text: %s\n", txt);
printf("Pattern: %s\n", pat);
printf("Occurrences:\n");
search(pat, txt);
40
10
printf("Time Complexity: O(n^2)\n");
getch();
23

return 0;
25
42

16
RESULT:
42
25

17
23
10
40
05
EX.NO:4(A) INSERTION SORT
DATE:

05
40
10
23
25
42

18
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void insertion(int a[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {

05
key = a[i];
j = i - 1;
while (j >= 0 && a[j] > key) {
a[j + 1] = a[j];
40
10
j = j - 1;
}
23

a[j + 1] = key;
}
25

}
int main() {
42

int a[50], i, n;
clock_t t;
double time;
t = clock();
printf("Enter n: ");
scanf("%d", &n);
printf("Enter elements: ");

19
for (i = 0; i < n; i++)
{
scanf(“%d”,&a[i]);
}
insertion(a, n);
t = clock() - t;
printf("Sorted elements: ");

05
for (i = 0; i < n; i++) {
printf("%d ", a[i]);
}
time = ((double)t) / CLOCKS_PER_SEC;
40
10
printf("\nTime taken: %f sec", time);
return 0;
23
25
42

20
RESULT:
42
25

21
23
10
40
05
EX.NO:4(B) HEAP SORT
DATE

05
40
10
23
25
42

22
PROGRAM:
#include <stdio.h>
#include <time.h>
void heapify(int *, int, int);
void heapsort(int *, int);
void print_array(int *, int);
int main() {

05
int n, i, a[50];
clock_t t;
double time;
t = clock();
40
10
printf("Enter the value of n: ");
scanf("%d", &n);
23

printf("\nEnter elements:\n");
for (i = 0; i < n; i++) {
25

scanf("%d", &a[i]);
}
42

heapsort(a, n);
printf("\n\nAfter sorting:\n");
print_array(a, n);
t = clock() - t;
time = ((double)t) / CLOCKS_PER_SEC;
printf("Time taken is %f", time);
return 0;

23
}
void heapsort(int *a,int n)
{
int I;
for (i = n - 1; i >= 0; i--) {
heapify(a, n, i);
}

05
for (i = n - 1; i >= 0; i--) {
int temp = a[i];
a[i] = a[0];
a[0] = temp;
40
10
heapify(a, i, 0);
}
23

}
void heapify(int *a, int n, int i) {
25

int large = i;
int left = 2 * i + 1;
42

int right = 2 * i + 2;
if (left < n && a[left] > a[large]) {
large = left;
}
if (right < n && a[right] > a[large]) {
large= right;
}

24
if (large != i) {
int temp = a[i];
a[i] = a[large];
a[large] = temp;
heapify(a, n, large);
}
}

05
void print_array(int *a, int n) {
int i;
for (i = 0; i < n; i++) {
printf("%d\n", a[i]);
40
10
}
}
23
25
42

25
RESULT:
42
25

26
23
10
40
05
EX.NO:5(A) BREADTH FIRST SEARCH
DATE:

05
40
10
23
25
42

27
PROGRAM:
#include <stdio.h>
#include <conio.h>
int a[20][20], q[20], visited[20], n, i, j, f = 0, r = -1;
void bfs(int v) {
for (i = 1; i <= n; i++)
if (a[v][i] && !visited[i])

05
q[++r] = i;
if (f <= r) {
visited[q[f]] = 1;
bfs(q[f++]);
40
10
}
}
23

void main() {
int v;
25

clrscr();
printf("\nEnter the number of vertices: ");
42

scanf("%d", &n);
for (i = 1; i <= n; i++) {
q[i] = 0;
visited[i] = 0;
}
printf("\nEnter graph data in matrix form:\n");
for (i = 1; i <= n; i++)

28
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);
printf("\nEnter the starting vertex: ");
scanf("%d", &v);
bfs(v);
printf("\nThe nodes which are reachable are:\n");
for (i = 1; i <= n; i++) {

05
if (visited[i])
printf("%d\t", i);
else
printf("\nBFS is not possible");
40
10
}
getch();
23

}
25
42

29
RESULT:
42
25

30
23
10
40
05
EX.NO:5(B) DEPTH FIRST SEARCH
DATE:

05
40
10
23
25
42

31
PROGRAM:
#include <stdio.h>
#include <conio.h>
int a[20][20], reach[20], n;
void dfs(int v) {
int i; reach[v] = 1;
for (i = 1; i <= n; i++)

05
if (a[v][i] && !reach[i]) {
printf("\n %d -> %d", v, i);
dfs(i);
}
40
10
}
void main() {
23

int i, j, count = 0;
clrscr();
25

printf("\nEnter number of vertices: ");


scanf("%d", &n);
42

for (i = 1; i <= n; i++) { reach[i] = 0;


for (j = 1; j <= n; j++) a[i][j] = 0;
}
printf("\nEnter the adjacency matrix:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
scanf("%d", &a[i][j]);

32
dfs(1);
printf(“\n”);
for(i=1;i<n;i++){
if (reach[i])
count++;
}
if (count == n)

05
printf("\nGraph is connected");
else
printf("\nGraph is not connected");
getch();
40
10
}
23
25
42

33
RESULT:
42
25

34
23
10
40
05
EX.NO:6(A): DIJKSTRA’S ALGORITHM
DTAE:

05
40
10
23
25
42

35
PROGRAM:
#include<stdio.h>
#include<conio.h>
void main()
{
int graph[15][15],s[15],pathestimate[15],mark[15];
int num_of_vertices,source,i,j,u,predecessor[15];

05
int count=0;
int minimum(int a[],int m[],int k);
void printpath(int,int,int[]);
clrscr();
40
10
printf("\nIMPLEMENTATION OF DIJKSTRA's ALGORITHM\n");
printf(" ");
23

printf("\nEnter the no.of vertices: ");


scanf("%d",&num_of_vertices);
25

if(num_of_vertices<=0)
{
42

printf("\nThis is meaningless\n");
exit(1);
}
printf("\nEnter the adjacent matrix:\n\n");
printf(" ");
for(i=1;i<=num_of_vertices;i++)
{

36
printf("\nEnter the elements of row %d: ",i);
for(j=1;j<=num_of_vertices;j++)
{
scanf("%d",&graph[i][j]);
}
}
printf("Adjacency Matrix for the given graph is:\n");

05
for(i=1;i<=num_of_vertices;i++)
{
for(j=1;j<=num_of_vertices;j++)
{ 40
10
printf("%d ",graph[i][j]);
}
23

printf("\n");
}
25

printf("\nEnter the source vertex:\n");


scanf("%d",&source);
42

for(j=1;j<=num_of_vertices;j++)
{
mark[j]=0;
pathestimate[j]=999;
predecessor[j]=0;
}
pathestimate[source]=0;

37
while(count<num_of_vertices)
{
u=minimum(pathestimate,mark,num_of_vertices);
s[++count]=u;
mark[u]=1;
for(i=1;i<=num_of_vertices;i++)
{

05
if(graph[u][i]>0)
{
if(mark[i]!=1)
{
40
10
if(pathestimate[i]>pathestimate[u]+graph[u][i])
{
23

pathestimate[i]=pathestimate[u]+graph[u][i];
predecessor[i]=u;
25

}
}
42

}
}
}
for(i=1;i<=num_of_vertices;i++)
{
printpath(source,i,predecessor);
if(pathestimate[i]!=999)

38
printf("-> (%d)\n",pathestimate[i]);
}
getch();
}
int minimum(int a[],int m[],int k)
{
int mi=999;

05
int i,t;
for(i=1;i<=k;i++)
{
if(m[i]!=1)
40
10
{
if(mi>=a[i])
23

{
mi=a[i]; t=i;
25

}
}}
42

return t;
}
void printpath(int x,int i,int p[])
{
printf("\n"); if(i==x)
{
printf("%d",x);

39
}
else if(p[i]==0)
printf("No path from %d to %d",x,i);
else
{
printpath(x,p[i],p);
printf(“….%d”,i);

05
}
}

40
10
23
25
42

40
RESULT:
42
25

41
23
10
40
05
EX.NO:6(B) PRIM’S ALGORITHM
DATE:

05
40
10
23
25
42

42
PROGRAM:
#include<stdio.h>
#include<conio.h>
#define MAX 10
#define TEMP 0
#define PERM 1
#define FALSE 0

05
#define TRUE 1
#define infinity 9999
struct node
{
40
10
int predecessor;
int dist; /*Distance from predecessor*/
23

int status;
};
25

struct edge
{
42

int u;
int v;
};
int adj[MAX][MAX];
int n;
void main()
{

43
int i,j;
int path[MAX];
int wt_tree,count;
struct edge tree[MAX];
void create_graph();
void display();
clrscr();

05
printf("\nIMPLEMENTATION OF PRIM's ALGORITHM\n");
printf(“\n________________________\n”);
create_graph();
printf("Adjacency matrix is :\n");
40
10
display();
count = maketree(tree,&wt_tree);
23

printf("Weight of spanning tree is : %d\n", wt_tree);


printf("Edges to be included in spanning tree are : \n");
25

for(i=1;i<=count;i++)
{
42

printf("%d->",tree[i].u);
printf("%d\n",tree[i].v);
}
getch();
}/*End of main()*/
void create_graph()

44
{
int i,max_edges,origin,destin,wt;
printf("Enter number of vertices : ");
scanf("%d",&n);
max_edges=n*(n-1)/2;
for(i=1;i<=max_edges;i++)
{

05
printf("Enter edge %d(0 0 to quit) : ",i);
scanf("%d %d",&origin,&destin);
if((origin==0) && (destin==0))
break;
40
10
printf("Enter weight for this edge : ");
scanf("%d",&wt);
23

if( origin > n || destin > n || origin<=0 || destin<=0)


{
25

printf("Invalid edge!\n");
i--;
42

}
else
{
adj[origin][destin]=wt;
adj[destin][origin]=wt;
}
}/*End of for*/ if(i<n-1)

45
{
printf("Spanning tree is not possible\n");
exit(1);
}}/*End of create_graph()*/
int maketree(struct edge tree[MAX],int * weight)
{
struct node state[MAX];

05
int i,k,min,count,current,newdist;
int m;
int u1,v1;
*weight=0;
40
10
/*Make all nodes temporary*/
for(i=1;i<=n;i++)
23

{
state[i].predecessor=0;
25

state[i].dist = infinity;
state[i].status = TEMP;
42

}
state[1].predecessor=0;
state[1].dist = 0;
state[1].status = PERM;
current=1;
count=0; /*count represents number of nodes in tree */
while( all_perm(state) != TRUE ) /*Loop till all the nodes become PERM*/

46
{
for(i=1;i<=n;i++)
{
if ( adj[current][i] > 0 && state[i].status == TEMP )
{
if(adj[current[i]<state[i].dist)
{

05
state[i].predecessor=current;
state[i].dist = adj[current][i];
}
}
40
10
}/*End of for*/
min=infinity;
23

for(i=1;i<=n;i++)
{
25

if(state[i].status == TEMP && state[i].dist < min)


{
42

min = state[i].dist;
current=i;
}
}/*End of for*/
state[current].status=PERM;
u1=state[current].predecessor;
v1=current;

47
count++;
tree[count].u=u1;
tree[count].v=v1;
/*Add wt on this edge to weight of tree */
*weight=*weight+adj[u1][v1];
}/*End of while*/
return (count);

05
}/*End of maketree()*/
/*This function returns TRUE if all nodes are permanent*/
int all_perm(struct node state[MAX] )
return(count);
40
10
}/*End of maketree()*/
/*This function returns TRUE if all nodes are permanent*/
23

Int all_perm(struct node state[max])


int I;
25

for(i=1;i<=n;i++)
if( state[i].status == TEMP ) return FALSE;
42

return TRUE;
}/*End of all_perm()*/
void display()
{
int i,j; for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++) printf("%3d",adj[i][j]);

48
printf("\n");
}
}/*End of display()*/

05
40
10
23
25
42

49
RESULT:
42
25

50
23
10
40
05
EX.NO:7(A) FLOYD’S ALGORITHM
DATE:

05
40
10
23
25
42

51
PROGRAM:
#include <stdio.h>
#include <stdlib.h>
void floydWarshall(int **graph, int n) {
int i, j, k;
for (k = 0; k < n; k++) {
for (i = 0; i < n; i++) {

05
for (j = 0; j < n; j++) {
if (graph[i][j] > graph[i][k] + graph[k][j])
graph[i][j] = graph[i][k] + graph[k][j];
}
40
10
}
}
23

}
int main(void) {
25

int n, i, j;
printf("Enter the number of vertices: ");
42

scanf("%d", &n);
int **graph = (int **)malloc(n * sizeof(int *));
for (i = 0; i < n; i++) {
graph[i] = (int *)malloc(n * sizeof(int));
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {

52
if (i == j) graph[i][j] = 0;
else
graph[i][j] = 100;
}
}
printf("Enter the edges:\n");
for (i = 0; i < n; i++) {

05
for (j = 0; j < n; j++) {
printf("[%d][%d]: ", i, j);
scanf("%d", &graph[i][j]);
}
40
10
}
printf("The original graph is:\n");
23

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


for (j = 0; j < n; j++) {
25

printf("%d ", graph[i][j]);


}
42

printf("\n");
}
floydWarshall(graph, n);
printf("The shortest path matrix is:\n");
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d ", graph[i][j]);

53
}
printf("\n");
}
return 0;
}

05
40
10
23
25
42

54
RESULT:
42
25

55
23
10
40
05
EX.NO:7(B) WARSHALL’S ALGORITHM
DATE:

05
40
10
23
25
42

56
PROGRAM:
#include <stdio.h>
const int MAX = 100;
void WarshallTransitiveClosure(int graph[MAX][MAX], int numVert);
int main(void) {
int i, j, numVert;
int graph[MAX][MAX];

05
printf("Warshall’s Transitive Closure\n");
printf("Enter the number of vertices: ");
scanf("%d", &numVert);
printf("Enter the adjacency matrix:-\n");
40
10
for (i = 0; i < numVert; i++) {
for (j = 0; j < numVert; j++) {
23

scanf("%d", &graph[i][j]);
}
25

}
WarshallTransitiveClosure(graph, numVert);
42

printf("\nThe Transitive closure for the given graph is:\n");


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

57
return 0;
}
void WarshallTransitiveClosure(int graph[MAX][MAX], int numVert) {
int i, j, k;
for (k = 0; k < numVert; k++) {
for (i = 0; i < numVert; i++) {
for (j = 0; j < numVert; j++) {

05
if (graph[i][j] || (graph[i][k] && graph[k][j])) {
graph[i][j] = 1;
}
}
40
10
}
}
23

}
25
42

58
RESULT:
42
25

59
23
10
40
05
EX.NO:8 DIVIDE AND CONQUER TECHNIQUE
DATE:

05
40
10
23
25
42

60
PROGRAM:
#include<stdio.h>
struct pair {
int min; int max;
};
struct pair getMinMax(int arr[], int n) {
struct pair minmax;

05
int i;
if (n == 1) {
minmax.max = arr[0];
minmax.min = arr[0];
40
10
return minmax;
}
23

if (arr[0] > arr[1]) {


minmax.max = arr[0];
25

minmax.min = arr[1];
}
42

Else
{
minmax.max = arr[1];
minmax.min = arr[0];
}
for (i = 2; i < n; i++) {
if (arr[i] > minmax.max)

61
minmax.max = arr[i];
else if(arr[i]<minmax.min)
minmax.min = arr[i];
}
return minmax;
}
int main() {

05
int arr[] = {1000, 11, 445, 1, 330, 3000};
int arr_size = 6;
struct pair minmax = getMinMax(arr, arr_size);
printf("Minimum element is %d\n", minmax.min);
40
10
printf("Maximum element is %d\n", minmax.max);
getchar();
23

return 0;
}
25
42

62
RESULT:
42
25

63
23
10
40
05
EX.NO:9(A) MERGE SORT
DATE:

05
40
10
23
25
42

64
PROGRAM:
#include<stdio.h>
void mergesort(int list[],int lb,int ub);
void merge(int list[],int lb,int mid,int ub);
void main()
{
int list[20];

05
int i,size;
int mid;
printf("Enter the size of the list(<20):");
scanf("%d",&size);
40
10
printf("\n Enter the elements one by one:");
for(i=0;i<size;i++)
23

{
scanf("%d",&list[i]);
25

}
mergesort(list,0,size-1); printf("\n The sorted list is...\t"); for(i=0;i<size;i++)
42

{
printf("%d ",list[i]);
}
}
void mergesort(int list[],int lb,int ub)
{
int mid;

65
if(lb<ub)
}
Mid=(lb+ub)/2;
Mergesort(list,lb,mid);
Mergesort(list,mid+1,ub);
Merge(list,lb,mid+1,ub);
}

05
}
void merge(int list[],int lb,int mid,int ub)
{
int mergelist[20];
40
10
int ptr1,ptr2,ptr3;
int i;
23

ptr1=lb;
ptr2=mid;
25

ptr3=lb;
while(ptr1<mid&&ptr2<=ub)
42

{
if(list[ptr1]<=list[ptr2])
{
mergelist[ptr3]=list[ptr1];
ptr1++;
ptr3++;
}

66
else
{
Mergelist[ptr3]=list[ptr2];
Ptr2++;
Ptr3++;
}
}

05
while(ptr1<mid)
{
mergelist[ptr3]=list[ptr1]; ptr1++;
ptr3++;
40
10
}
while(ptr2<=ub)
23

{
mergelist[ptr3]=list[ptr2]; ptr2++;
25

ptr3++;
}
42

for(i=lb;i<ptr3;i++)
{
list[i]=mergelist[i];
}
}

67
RESULT:
42
25

68
23
10
40
05
EX.NO:9(B) QUICK SORT
DATE:

05
40
10
23
25
42

69
PROGRAM:
#include<stdio.h>
#include<stblib.h>
#include<time.h>
void swap(int*a, int*b)
{
int temp=*a;

05
*a=*b;
*b=temp;
}
int partition(int arr[],int low,int high)
40
10
{
int pivot=arr[low];
23

int i=low+1; int j=high; while(1)


{
25

while(i<=j&&arr[i]<pivot) i++;
while(i<=j&&arr[j]>pivot) j--;
42

if(i<=j)
{
swap(&arr[i],&arr[j]); i++;
j--;
}
else break;
}

70
swap(&arr[low],&arr[j]);
return j;
}
void quicksort(int arr[],int low,int high)
{
if(low<high)
{

05
int pivotIndex=partition(arr,low,high);
}
}
int main()
40
10
{
int n;
23

printf(“Enter the number of elements:”);


scanf(“%d”,&n);
25

int arr[10];
stand(time(0));
42

for(int i=0;i<n;i++)
{
arr[i]=rand()%100;
}
printf(“Original array:\n”);
for(int i=0;i<n;i++)
{

71
printf(“%d”,arr[i]);
}
clock_t start=clock();
double time_taken=((double)(end-start));
printf(“\nSorted array:\n);
for(int i=0;i<n;i++)
{

05
printf(“%d”,arr[i]);
}

40
printf(“\nTime taken:%.2f ms\n”,time_taken);
return 0;
10
}
23
25
42

72
RESULT:
42
25

73
23
10
40
05
EX.NO:10 N-QUEENS PROBLEM USING BACKTRACKING
DATE:

05
40
10
23
25
42

74
PROGRAM:
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
int board[20], count;
void print(int n) {
int i, j;

05
printf("\n\nSolution %d:\n\n", ++count);
for(i = 1; i <= n; ++i)
printf("\t%d", i);
for(i = 1; i <= n; ++i) {
40
10
printf("\n\n%d", i);
for(j = 1; j <= n; ++j) {
23

if(board[i] == j)
printf("\tQ");
25

else
printf("\t-");
42

}
}
}
int place(int row, int column) {
int i;
for(i = 1; i <= row - 1; ++i) {
if(board[i] == column || abs(board[i] - column) == abs(i - row))

75
return 0;
}
return 1;
}
void queen(int row, int n) {
int column;
for(column = 1; column <= n; ++column) {

05
if(place(row, column)) {
board[row] = column;
if(row == n)
print(n);
40
10
else
queen(row + 1, n);
23

}
}
25

}
int main() {
42

int n, i, j;
printf("Enter number of Queens: ");
scanf("%d", &n);
if(n <= 3)
printf("Number should be greater than 3\n");
else
queen(1, n);

76
return 0;
}

05
40
10
23
25
42

77
RESULT:
42
25

78
23
10
40
05
EX.NO:11(A) TRAVELLING SALESPERSON PROBLEM
DATE:

05
40
10
23
25
42

79
PROGRAM:
#include<stdio.h>
int matrix[25][25], visited_cities[10], limit, cost = 0;
int tsp(int c)
{
int count, nearest_city = 999;
int minimum = 999, temp;

05
for(count = 0; count < limit; count++) {
if((matrix[c][count] != 0) && (visited_cities[count] == 0))
{
if(matrix[c][count] < minimum) {
40
10
minimum = matrix[c][count];
}
23

temp = matrix[c][count];
nearest_city = count;
25

}
}
42

if(minimum != 999)
{
cost = cost + temp;
}
return nearest_city;
}
void minimum_cost(int city) {

80
int nearest_city;
visited_cities[city] = 1;
printf("%d ", city + 1);
nearest_city = tsp(city);
if(nearest_city == 999)
{
nearest_city = 0;

05
printf("%d ", nearest_city + 1);
cost = cost + matrix[city][nearest_city];
return;
}
40
10
minimum_cost(nearest_city);
}
23

int main() { int i, j;


printf("Enter total number of cities: ");
25

scanf("%d", &limit);
printf("\nEnter Cost Matrix\n");
42

for(i = 0; i < limit; i++) {


printf("Enter %d elements in row[%d]\n", limit, i + 1);
for(j = 0; j < limit; j++)
{
scanf("%d", &matrix[i][j]);
}
visited_cities[i] = 0;

81
}
printf("\nEntered Cost Matrix:\n");
for(i = 0; i < limit; i++) {
printf("\n");
for(j = 0; j < limit; j++) {
printf("%d ", matrix[i][j]);
}

05
}
printf("\n\nPath: ");
minimum_cost(0);
printf("\n\nMinimum Cost: %d\n", cost);
40
10
return 0;
}
23
25
42

82
RESULT:
42
25

83
23
10
40
05
EX.NO:11(B): IMPLEMENT RANDOMIZED ALGORITHMS FOR FINDING
DATE: THE KTH SMALLEST NUMBER

05
40
10
23
25
42

84
PROGRAM:
#include<stdio.h>
#include<math.h>
#include<time.h>
#include<stdlib.h>
#define N 20 int A[20];
void swap(int dex1, int dex2)

05
{
int temp = A[dex1];
A[dex1] = A[dex2];
A[dex2] = temp;
40
10
}
int partition(int start, int end)
23

{
int i = start + 1;
25

int j = start;
int pivot = start;
42

for(; i < end; i++)


{
if(A[i] < A[pivot])
{
j++;
swap(i, j);
}

85
}
if(j <= end)
swap(pivot, (j - 1));
return j - 1;
}
void quick_sort(int start, int end, int k)
{

05
int part;
if(start < end)
{
part = partition(start, end);
40
10
if(part == k - 1)
printf("kth smallest element: %d\n", A[part]);
23

if(part > k - 1)
quick_sort(start, part, k);
25

}
return;
42

}
int main(int argc, char **argv)
{
int i;
A[i] = rand() % 1000 + 1;
printf("The Original sequence is: ");
for(i = 0; i < N; i++)

86
printf("%d ", A[i]);
printf("\nEnter the Kth smallest you want to find: ");
int k;
scanf("%d", &k);
quick_sort(0, N, k);
return 0;
}

05
40
10
23
25
42

87
RESULT:
42
25

88
23
10
40
05
89

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy