You are on page 1of 24

ANALYSIS AND DESIGN OF

ALGORITHMS
19BSIT6D11L
LAB RECORD
Submitted To: Ms. Rachana R Babu
Assistant Professor
CS&IT Department
JAIN (Deemed-To-Be University), Kochi
Submitted By: Akarsh P
B.Sc. IT
Semester V
JAIN (Deemed-To-Be University), Kochi
CERTIFICATE

Certified that this is the bonafide record work done in the …ANALYSIS AND DESIGN OF

ALGORITHMS … Laboratory by Mr./Ms.……….… Akarsh P……………………….Of ……………

SIXTH……….…Semester BSc Degree Course in.....................................INFORMATION…

TECHNOLOGY……………………… Branch having University Register No ……………………

………19BSCKR033…………………………….. During the academic year ……2022…………………….

Faculty in Charge
INDEX
Sl. No. Title Date

1 Traveling salesman problem 04-02-2022

2 Selection Sort 07-02-2022

3 Bubble Sort 12-02-2022

4 Sequential Search 22-02-2022

5 Merge Sort 26-02-2022

6 Quick Sort 28-02-2022

7 Bucket Sort 11-03-2022

8 Radix Sort 16-03-2022

9 Breadth First Search 19-03-2022

10 Depth First Search 23-03-2022


1. Travelling Salesman Problem

Aim: Program to solve the Traveling salesman problem by Brute Force Approach

Code:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 40

struct Graph
{
int numVertices;
struct node **adjLists;
int *visited;
};

struct node
{
int vertex;
struct node *next;
};

struct node *createNode(int v)


{
struct node *newNode = malloc(sizeof(struct node));

newNode->vertex = v;
newNode->next = NULL;

return newNode;
}

struct Graph *createGraph(int vertices)


{
struct Graph *graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;
;

graph->adjLists = malloc(vertices * sizeof(struct node *));


graph->visited = malloc(vertices * sizeof(int));

int i;
for (i = 0; i < vertices; i++)
{
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}

return graph;
};

void addEdge(struct Graph *graph, int src, int dest)


{
struct node *newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}

void printGraph(struct Graph *graph)


{
int i;

for (i = 0; i < graph->numVertices; i++)


{
struct node *temp = graph->adjLists[i];
printf("\nAdjacency list of vertex %d \n", i);

while (temp)
{
printf("%d -> ", temp->vertex);
temp = temp->next;
}
printf("\n");
}
}

void dfs(struct Graph *graph, int vertex)


{
struct node *adjList = graph->adjLists[vertex];
struct node *temp = adjList;

graph->visited[vertex] = 1;
printf("Visited %d \n", vertex);

while (temp != NULL)


{
int connectedVertex = temp->vertex;

if (graph->visited[connectedVertex] == 0)
{
dfs(graph, connectedVertex);
}

temp = temp->next;
}
}

int main()
{
struct Graph *graph = createGraph(4);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 2, 3);

printGraph(graph);
printf("\n");
dfs(graph, 2);

return 0;
}
Result:
2. Selection Sort

Aim: Program to sort a set of elements by implementing Selection Sort

Code:

#include<stdio.h>

int main(){
int size;
printf("Enter array size : ");
scanf("%d", &size);
int array[size], i, imin, j, tmp;
for (i=0; i<size; i++){
printf("Element %d : ", i);
scanf("%d", &array[i]);
}
for (i=0; i < size-1; i++){
imin=i;
for (j=i+1; j<size; j++)
if (array[j]<array[imin])
imin=j;
if (imin!=i){
tmp=array[imin];
array[imin]=array[i];
array[i]=tmp;
}
}
for (i=0; i < size; i++){
printf("%d ", array[i]);
}
}

Result:
3. Bubble Sort

Aim: Program to sort a set of elements by implementing Bubble Sort

Code:

#include <stdio.h>

void printArr(int *arr, int n) {


for (int i=0; i<n; i++) {
printf("%d", arr[i]);
if (i<n-1) printf(", ");
}
}

void main() {
int arr[] = {88,84,21,04,8864,534,9872,1001,23,03};
int n = sizeof(arr)/sizeof(arr[0]);
int i, j, tmp;

printf("Array before sorting: \n");


printArr(arr, n);

// Bubble sort
for (i=0; i<(n-1); i++)
for (j=0; j<(n-i); j++)
if (arr[j-1]>arr[j]) {
tmp = arr[j-1];
arr[j-1] = arr[j];
arr[j] = tmp;
}

printf("\n\nArray after sorting: \n");


printArr(arr, n);
}

Result:
4. Sequential Search

Aim: Program to find an element using Sequential Searching from an Array

Code:

#include <stdio.h>
int main()
{
int *arr, arr_size, inp_num, i;
printf("Enter a number:");
scanf("%d", &inp_num);

printf("Enter array size:");


scanf("%d", &arr_size);

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


{
printf("Array element[%d]:", i);
scanf("%d", &arr[i]);
// fflush(stdin);
}

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


{
if (arr[i] == inp_num)
printf("Element %d found at %d", inp_num, i);
}

return 0;
}

Result:
5. Merge Sort

Aim: Program to sort a set of elements by implementing Merge Sort

Code:

#include <stdio.h>

void merge(int arr[], int l, int m, int r)


{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];

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


L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + j + 1];

i = j = 0;
k = l;

while (i < n1 && j < n2)


{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}

void mSort(int arr[], int l, int r)


{
if (l < r)
{
int m = l + (r - l) / 2;
mSort(arr, l, m);
mSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

int main()
{
int size;
printf("Enter array size : ");
scanf("%d", &size);
int array[size], i, imin, j, tmp;
for (i = 0; i < size; i++)
{
printf("Element %d : ", i);
scanf("%d", &array[i]);
}
mSort(array, 0, size - 1);
for (i = 0; i < size; i++)
printf("%d ", array[i]);
}

Result:
6. Quick Sort

Aim: Program to sort a set of elements by implementing Quick sort

Code:

#include <stdio.h>

void swap(int *x, int *y)


{
int t = *x;
*x = *y;
*y = t;
}

int part(int array[], int srt, int end)


{
int pvt = array[end];
int i = srt - 1;

for (int j = srt; j <= end - 1; j++)


if (array[j] < pvt)
swap(&array[++i], &array[j]);

swap(&array[i + 1], &array[end]);

return i + 1;
}

void quickSort(int array[], int srt, int end)


{
if (srt < end)
{
int partid = part(array, srt, end);
quickSort(array, srt, partid - 1);
quickSort(array, partid + 1, end);
}
}

int main()
{
int size, array[50], i, imin, j, tmp;

printf("Enter array size : ");


scanf("%d", &size);

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


{
printf("Element %d : ", i);
scanf("%d", &array[i]);
}

quickSort(array, 0, size - 1);

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


printf("%d ", array[i]);
}
Result:
7. Bucket Sort

Aim: Program to sort a set of elements by implementing Bucket sort

Code:

#include <stdio.h>

void printArr(int *arr, int n)


{
for (int i = 0; i < n; i++)
{
printf("%d", arr[i]);
if (i < n - 1)
printf(", ");
}
}

void bucketSort(int *arr, int n)


{
int max = arr[0], i, j = 0;
for (int i = 0; i < n; i++)
{
if (max < arr[i])
{
max = arr[i];
}
}

int bucket[max];

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


bucket[i] = 0;

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


bucket[arr[i]]++;

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


while (bucket[i] > 0)
{
arr[j++] = i;
bucket[i]--;
}
}

int main()
{
int arr[] = {88,84,21,04,8864,534,9872,1001,23,03};
int n = sizeof(arr) / sizeof(arr[0]);
int i, j, tmp;

printf("Array before sorting: \n");


printArr(arr, n);

bucketSort(arr, n);

printf("\n\nArray after sorting: \n");


printArr(arr, n);
return 0;
}

Result:
8. Radix Sort

Aim: Program to sort a set of elements by implementing Radix sort

Code:

#include <stdio.h>

void printArr(int *arr, int n)


{
for (int i = 0; i < n; i++)
{
printf("%d", arr[i]);
if (i < n - 1)
printf(", ");
}
}

void placeSort(int *arr, int size, int place)


{
int i;
int output[size + 1];
int max = (arr[0] / place) % 10;

for (i = 1; i < size; i++)


if (((arr[i] / place) % 10) > max)
max = arr[i];

int count[max + 1];

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


count[i] = 0;

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


count[(arr[i] / place) % 10]++;

for (i = 1; i < 10; i++)


count[i] += count[i - 1];

for (i = size - 1; i >= 0; i--)


{
output[count[(arr[i] / place) % 10] - 1] = arr[i];
count[(arr[i] / place) % 10]--;
}

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


arr[i] = output[i];
}

void radixSort(int *arr, int n)


{
int max = arr[0], place;
for (int i = 0; i < n; i++)
{
if (max < arr[i])
{
max = arr[i];
}
}
for (place = 1; max / place > 0; place *= 10)
placeSort(arr, n, place);
}

int main()
{
int arr[] = {301, 654, 453, 786, 812, 345, 754, 246};
int n = sizeof(arr) / sizeof(arr[0]);

printf("Array before sorting: \n");


printArr(arr, n);

radixSort(arr, n);

printf("\n\nArray after sorting: \n");


printArr(arr, n);

return 0;
}

Result:
9. Breadth First Search

Aim: Program to implement Breadth First Search (BFS)

Code:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 40

struct queue
{
int items[SIZE];
int front;
int rear;
};

struct Graph
{
int numVertices;
struct node **adjLists;
int *visited;
};

struct node
{
int vertex;
struct node *next;
};

struct node *createNode(int v)


{
struct node *newNode = malloc(sizeof(struct node));

newNode->vertex = v;
newNode->next = NULL;

return newNode;
}

struct Graph *createGraph(int vertices)


{
struct Graph *graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;
;

graph->adjLists = malloc(vertices * sizeof(struct node *));


graph->visited = malloc(vertices * sizeof(int));

int i;
for (i = 0; i < vertices; i++)
{
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}

return graph;
};
void addEdge(struct Graph *graph, int src, int dest)
{
struct node *newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;

newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}

int isEmpty(struct queue *q)


{
if (q->rear == -1)
return 1;
else
return 0;
}

void printQueue(struct queue *q)


{
int i = q->front;

if (isEmpty(q))
{
printf("Queue is empty");
}
else
{
printf("\nQueue contains :\n");
for (i = q->front; i < q->rear + 1; i++)
{
printf("%d", q->items[i]);
if (i < q->rear)
{
printf(", ");
}
}
}
printf("\n");
}

struct queue *createQueue()


{
struct queue *q = malloc(sizeof(struct queue));
q->front = -1;
q->rear = -1;

return q;
}

void enqueue(struct queue *q, int value)


{
if (q->rear == (SIZE - 1))
printf("\nQueue is Full.\n");
else
{
if (q->front == -1)
q->front = 0;
q->rear++;
q->items[q->rear] = value;
}
}

int dequeue(struct queue *q)


{
int item;

if (isEmpty(q))
{
printf("\nQueue is empty.\n");
item = -1;
}
else
{
item = q->items[q->front];
q->front++;

if (q->front > q->rear)


{
printf("\nResetting Queue.\n");
q->front = q->rear = -1;
}
}

return item;
}

void bfs(struct Graph *graph, int startVertex)


{
struct queue *q = createQueue();

graph->visited[startVertex] = 1;
enqueue(q, startVertex);

while (!isEmpty(q))
{
printQueue(q);
int currentVertex = dequeue(q);

printf("\nVisited: %d\n", currentVertex);

struct node *temp = graph->adjLists[currentVertex];

while (temp)
{
int adjVertex = temp->vertex;

if (graph->visited[adjVertex] == 0)
{
graph->visited[adjVertex] = 1;
enqueue(q, adjVertex);
}
temp = temp->next;
}
}
}

int main()
{
struct Graph *graph = createGraph(6);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 1, 4);
addEdge(graph, 1, 3);
addEdge(graph, 2, 4);
addEdge(graph, 3, 4);

bfs(graph, 0);

return 0;
}

Result:
10. Depth First Search

Aim: Program to implement Depth First Search(DFS)

Code:

#include <stdio.h>
#include <stdlib.h>

#define SIZE 40

struct Graph
{
int numVertices;
struct node **adjLists;
int *visited;
};

struct node
{
int vertex;
struct node *next;
};

struct node *createNode(int v)


{
struct node *newNode = malloc(sizeof(struct node));

newNode->vertex = v;
newNode->next = NULL;

return newNode;
}

struct Graph *createGraph(int vertices)


{
struct Graph *graph = malloc(sizeof(struct Graph));
graph->numVertices = vertices;
;

graph->adjLists = malloc(vertices * sizeof(struct node *));


graph->visited = malloc(vertices * sizeof(int));

int i;
for (i = 0; i < vertices; i++)
{
graph->adjLists[i] = NULL;
graph->visited[i] = 0;
}

return graph;
};

void addEdge(struct Graph *graph, int src, int dest)


{
struct node *newNode = createNode(dest);
newNode->next = graph->adjLists[src];
graph->adjLists[src] = newNode;
newNode = createNode(src);
newNode->next = graph->adjLists[dest];
graph->adjLists[dest] = newNode;
}

void printGraph(struct Graph *graph)


{
int i;

for (i = 0; i < graph->numVertices; i++)


{
struct node *temp = graph->adjLists[i];
printf("\nAdjacency list of vertex %d \n", i);

while (temp)
{
printf("%d -> ", temp->vertex);
temp = temp->next;
}
printf("\n");
}
}

void dfs(struct Graph *graph, int vertex)


{
struct node *adjList = graph->adjLists[vertex];
struct node *temp = adjList;

graph->visited[vertex] = 1;
printf("Visited %d \n", vertex);

while (temp != NULL)


{
int connectedVertex = temp->vertex;

if (graph->visited[connectedVertex] == 0)
{
dfs(graph, connectedVertex);
}

temp = temp->next;
}
}

int main()
{
struct Graph *graph = createGraph(4);
addEdge(graph, 0, 1);
addEdge(graph, 0, 2);
addEdge(graph, 1, 2);
addEdge(graph, 2, 3);

printGraph(graph);
printf("\n");
dfs(graph, 2);

return 0;
}
Result:

You might also like