0% found this document useful (0 votes)
52 views30 pages

DAA Lab File Ashutosh

The document contains programs for various sorting and graph algorithms including binary search, insertion sort, bubble sort, selection sort, shell sort, counting sort, quick sort, merge sort, heap sort, and graph algorithms BFS and DFS implemented using an adjacency list representation of a graph. Experiments 1-3 cover sorting algorithms and Experiment 4 covers graph algorithms BFS and DFS.

Uploaded by

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

DAA Lab File Ashutosh

The document contains programs for various sorting and graph algorithms including binary search, insertion sort, bubble sort, selection sort, shell sort, counting sort, quick sort, merge sort, heap sort, and graph algorithms BFS and DFS implemented using an adjacency list representation of a graph. Experiments 1-3 cover sorting algorithms and Experiment 4 covers graph algorithms BFS and DFS.

Uploaded by

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

AJAY KUMAR GARG ENGINEERING

COLLEGE

B.Tech. Information
Technology Sem 5TH , 2023-24
CODE- KCS-553
DESIGN AND ANALYSIS OF ALGORITHM LAB
Submitted To :- Submitted By :
Mr. Birendra Ayush Dixit
Kumar
(2100270130052)

Dr. A.P.J. Abdul Kalam Technical University, Uttar Pradesh,


Lucknow
EXPERIMENT NO. 1
AIM: Program for Binary Search, Insertion Sort and Bubble
Sort.

1. BINARY SEARCH

#include <stdio.h>
int binarySearch(int arr[], int l, int r, int x)
{
while (l <= r) {
int m = l + (r - l) /
2; if (arr[m] == x)
return m;
if (arr[m] <
x) l = m + 1;
else
r = m - 1;
}
return -1;
}
int main(void)
{
int arr[] = { 2, 3, 4, 10, 40 };
int n = sizeof(arr) /
sizeof(arr[0]); int x = 10;
int result = binarySearch(arr, 0, n - 1, x);
(result == -1) ? printf("Element is not
present" " in array")
: printf("Element is present at "
"index %d",
result);
return 0;
}

OUTPUT:
Element is present t index 3

2. INSERTION SORT

Ayush Dixit 2100270130052


#include <stdio.h>
void insertionSort(int arr[], int n)
{
int i, key, j;
for (i = 1; i < n; i++)
{
key =
arr[i]; j = i -
1;
while (j >= 0 && arr[j] > key)
{
arr[j + 1] =
arr[j]; j = j - 1;
}
arr[j + 1] = key;
}
}
void printArray(int arr[], int n)
{
int i;
for (i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) /
sizeof(arr[0]); insertionSort(arr,
n); printArray(arr, n);

return 0;
}
OUTPUT:
5 6 11 12 13

3. BUBBLE SORT

#include <stdio.h>
void swap(int* xp, int* yp)
{
int temp = *xp;
*xp = *yp;

Ayush Dixit 2100270130052


*yp = temp;
}
void bubbleSort(int arr[], int n)
{
int i, j;
bool swapped;
for (i = 0; i < n - 1; i++) {
swapped = false;
for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
swapped = true;
}
}
if (swapped ==
false) break;
}
}
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++){
printf("%d ", arr[i]);}
}
int main()
{
int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int n = sizeof(arr) /
sizeof(arr[0]); bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

OUTPUT:
SORTED ARRAY
11 12 22 25 34 64
90

Ayush Dixit 2100270130052


Experiment-2
AIM: Program for Selection, Shell Sort and Counting Sort
1. SELECTION SORT
#include <stdio.h>
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

void selectionSort(int arr[], int n)


{
int i, j, min_idx;
for (i = 0; i < n-1; i++)
{
min_idx = i;
for (j = i+1; j < n; j++)
if (arr[j] <
arr[min_idx])
min_idx = j;

if(min_idx != i)
swap(&arr[min_idx], &arr[i]);
}
}
void printArray(int arr[], int size)
Ayush Dixit 2100270130052
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr)/sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

OUTPUT:
Sorted array: 11 12 22 25 64

2. SHELL SORT
#include <stdio.h>
void shellSort(int array[], int n)
{ for (int interval = n / 2; interval > 0; interval /= 2) {
for (int i = interval; i < n; i += 1) {
int temp = array[i];
int j;
for (j = i; j >= interval && array[j - interval] > temp; j -= interval) {

Ayush Dixit 2100270130052


array[j] = array[j - interval];
}
array[j] = temp;
}
}
}
void printArray(int array[], int size)
{ for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
} printf("\n");
}
int main() {
int data[] = {9, 8, 3, 7, 5, 6, 4, 1};
int size = sizeof(data) / sizeof(data[0]);
shellSort(data, size);
printf("Sorted array: \n");
printArray(data, size);
}

OUTPUT
13456789

3. COUNTING SORT

#include <stdio.h>
void countingSort(int array[], int size) {

Ayush Dixit 2100270130052


int output[10];
int max = array[0];
for (int i = 1; i < size; i++) {
if (array[i] > max)
max = array[i];
}
int count[10];
for (int i = 0; i <= max; ++i) {
count[i] = 0;
}
for (int i = 0; i < size; i++) {
count[array[i]]++;
}
for (int i = 1; i <= max; i++)
{ count[i] += count[i - 1];
}
for (int i = size - 1; i >= 0; i--) {
output[count[array[i]] - 1] =
array[i]; count[array[i]]--;
}
for (int i = 0; i < size; i++) { array[i] = output[i];
}
}
void printArray(int array[], int size)
{ for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);

Ayush Dixit 2100270130052


}
printf("\n");
}
int main() {
int array[] = {4, 2, 2, 8, 3, 3, 1};
int n = sizeof(array) / sizeof(array[0]);
countingSort(array, n);
printArray(array, n);
}

Output
1223348

Ayush Dixit 2100270130052


EXPERIMENT NO. 3
AIM : Program for Quick Sort, Merge Sort and Heap Sort .
1. Quick Sort
#include <stdio.h>
void swap(int *a, int *b)
{ int t = *a;
*a = *b;
*b = t;
}
int partition(int array[], int low, int high) {
int pivot = array[high];
int i = (low - 1);
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
i++;
swap(&array[i], &array[j]);
}
}
swap(&array[i + 1], &array[high]);
return (i + 1);
}
void quickSort(int array[], int low, int high) {
if (low < high) {
int pi = partition(array, low, high);
quickSort(array, low, pi - 1);
quickSort(array, pi + 1, high);

Ayush Dixit 2100270130052


}
}
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
printf("%d ", array[i]);
}
printf("\n");
}
int main() {
int data[] = {8, 7, 2, 1, 0, 9, 6};
int n = sizeof(data) / sizeof(data[0]);
printf("Unsorted Array\n");
printArray(data, n);
quickSort(data, 0, n - 1);
printf("Sorted array in ascending order: \n");
printArray(data, n);
}

2. Merge Sort
#include <stdio.h>
#include <stdlib.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];

Ayush Dixit 2100270130052


for (i = 0; i < n1; i+
+) L[i] = arr[l + i];
for (j = 0; j < n2; j+
+) R[j] = arr[m + 1 +
j]; i = 0;
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++;

Ayush Dixit 2100270130052


}
}
void mergeSort(int arr[], int l, int r)
{
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
void printArray(int A[], int size)
{
int i;
for (i = 0; i < size; i+
+) printf("%d ", A[i]);
printf("\n");
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int arr_size = sizeof(arr) / sizeof(arr[0]);
printf("Given array is \n");
print Array(arr, arr_size);
mergeSort(arr, 0, arr_size - 1);
printf("\nSorted array is \n");
printArray(arr, arr_size);

Ayush Dixit 2100270130052


return 0;
}

OUTPUT:
Sorted array
is 5 6 7 11 12
13

3. Heap Sort
include <stdio.h>
void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void heapify(int arr[], int N, int i)
{
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < N && arr[left] >
arr[largest]) largest = left;
if (right < N && arr[right] > arr[largest])
largest = right;
if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, N, largest);

Ayush Dixit 2100270130052


}
}
void heapSort(int arr[], int N)
{
for (int i = N / 2 - 1; i >= 0; i--)
heapify(arr, N, i);
for (int i = N - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);
heapify(arr, i, 0);
}
}
void printArray(int arr[], int N)
{
for (int i = 0; i < N; i++)
printf("%d ", arr[i]);
printf("\n");
}
int main()
{
int arr[] = { 12, 11, 13, 5, 6, 7 };
int N = sizeof(arr) / sizeof(arr[0]);
heapSort(arr, N);
printf("Sorted array is\n");
printArray(arr, N);
}

Ayush Dixit 2100270130052


OUTPUT:
Sorted array is :
5 6 7 11 12 13

Ayush Dixit 2100270130052


EXPERIMENT NO. 4
AIM: Program for BFS and DFS on a graph represented using
Adjacency list.
1. BFS
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 50
typedef struct Graph_t {
int V;
bool adj[MAX_VERTICES][MAX_VERTICES];
} Graph;
Graph* Graph_create(int V)
{
Graph* g =
malloc(sizeof(Graph)); g->V = V;

for (int i = 0; i < V; i++)


{ for (int j = 0; j < V; j+
+) { g->adj[i][j] = false;
}
}
return g;
}
void Graph_destroy(Graph* g) { free(g); }
void Graph_addEdge(Graph* g, int v, int w)

Ayush Dixit 2100270130052


{
g->adj[v][w] = true;
}
void Graph_BFS(Graph* g, int s)
{
bool visited[MAX_VERTICES];
for (int i = 0; i < g->V; i++) {
visited[i] = false;
}
int queue[MAX_VERTICES];
int front = 0, rear = 0;
visited[s] = true; queue[rear+
+] = s;
while (front != rear)
{ s = queue[front++];
printf("%d ", s);
for (int adjacent = 0; adjacent < g->V;
adjacent++) {
if (g->adj[s][adjacent] && !visited[adjacent]) {
visited[adjacent] = true;
queue[rear++] = adjacent;
}
}
}
}
int main()
{

Ayush Dixit 2100270130052


Graph* g = Graph_create(4);
Graph_addEdge(g, 0, 1);
Graph_addEdge(g, 0, 2);
Graph_addEdge(g, 1, 2);
Graph_addEdge(g, 2, 0);
Graph_addEdge(g, 2, 3);
Graph_addEdge(g, 3, 3);

printf("Following is Breadth First Traversal "


"(starting from vertex 2) \n");
Graph_BFS(g, 2);
Graph_destroy(g);

return 0;
}

OUTPUT:
Following is Breadth First Traversal (Starting from vertex 2)
2031

2. DFS
#include <stdio.h>
#include
<stdlib.h> int
vis[100];
struct Graph
{ int V;

Ayush Dixit 2100270130052


int E;
int** Adj;
};
struct Graph* adjMatrix()
{
struct Graph* G = (struct Graph*)
malloc(sizeof(struct Graph));
if (!G) {
printf("Memory Error\n");
return NULL;
}
G->V = 7;
G->E = 7;
G->Adj = (int**)malloc((G->V) * sizeof(int*));
for (int k = 0; k < G->V; k++) {
G->Adj[k] = (int*)malloc((G->V) * sizeof(int));
}
for (int u = 0; u < G->V; u++)
{ for (int v = 0; v < G->V; v+
+) { G->Adj[u][v] = 0;
}
}
G->Adj[0][1] = G->Adj[1][0] = 1;
G->Adj[0][2] = G->Adj[2][0] = 1;
G->Adj[1][3] = G->Adj[3][1] = 1;
G->Adj[1][4] = G->Adj[4][1] = 1;
G->Adj[1][5] = G->Adj[5][1] = 1;

Ayush Dixit 2100270130052


G->Adj[1][6] = G->Adj[6][1] = 1;
G->Adj[6][2] = G->Adj[2][6] = 1;
return G;
}
void DFS(struct Graph* G, int u)
{
vis[u] = 1;
printf("%d ", u);
for (int v = 0; v < G->V; v++)
{ if (!vis[v] && G->Adj[u][v])
{ DFS(G, v);
}
}
}
void DFStraversal(struct Graph* G)
{
for (int i = 0; i < 100; i++) {
vis[i] = 0;
}
for (int i = 0; i < G->V; i++) {
if (!vis[i]) {
DFS(G, i);
}
}
}
void main()
{

Ayush Dixit 2100270130052


struct Graph* G;
G = adjMatrix();
DFStraversal(G);
}

OUTPUT:
0134562

Ayush Dixit 2100270130052


EXPERIMENT NO. 5
AIM: Program for constructing a Minimum Cost Spanning
Tree using Prim’s and Kruskal’s algorithms.
1. PRIM’s
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#define V 5
int minKey(int key[], bool mstSet[])
{
int min = INT_MAX,
min_index; for (int v = 0; v < V;
v++)
if (mstSet[v] == false && key[v] < min)
min = key[v], min_index = v;
return min_index;
}
int printMST(int parent[], int graph[V][V])
{
printf("Edge \tWeight\n");
for (int i = 1; i < V; i++)
printf("%d - %d \t%d \n", parent[i], i,
graph[i][parent[i]]);
}
void primMST(int graph[V][V])
{
int parent[V];
Ayush Dixit 2100270130052
int key[V];
bool mstSet[V];
for (int i = 0; i < V; i++)
key[i] = INT_MAX, mstSet[i] =
false; key[0] = 0;
parent[0] = -1;
for (int count = 0; count < V - 1; count++) {
int u = minKey(key, mstSet);
mstSet[u] = true;
for (int v = 0; v < V; v++)
if (graph[u][v] && mstSet[v] == false
&& graph[u][v] < key[v])
parent[v] = u, key[v] = graph[u][v];
}
printMST(parent, graph);
}
int main()
{
int graph[V][V] = { { 0, 2, 0, 6, 0 },{ 2, 0, 3, 8, 5}, { 0, 3, 0, 0, 7},
{ 6, 8, 0, 0, 9 },{ 0, 5, 7, 9, 0 } };
primMST(graph);
return 0;
}

OUTPUT:
Edge Weight
0-1 2

Ayush Dixit 2100270130052


1-2 3
0-3 6
1-4 5

2. KRUSKAL’s
#include <stdio.h>
#include <stdlib.h>
int comparator(const void* p1, const void* p2)
{
const int(*x)[3] = p1;
const int(*y)[3] = p2;
return (*x)[2] - (*y)
[2];
}
void makeSet(int parent[], int rank[], int n)
{
for (int i = 0; i < n; i++) {
parent[i] = i;
rank[i] = 0;
}
}
int findParent(int parent[], int component)
{
if (parent[component] == component)
return component;
return parent[component]
= findParent(parent, parent[component]);
}

Ayush Dixit 2100270130052


void unionSet(int u, int v, int parent[], int rank[], int n)
{
u = findParent(parent, u);
v = findParent(parent, v);
if (rank[u] < rank[v]) {
parent[u] = v;
}
else if (rank[u] > rank[v]) {
parent[v] = u;
}
else {
parent[v] = u;
rank[u]++;
}
}
void kruskalAlgo(int n, int edge[n][3])
{
qsort(edge, n, sizeof(edge[0]),
comparator); int parent[n];
int rank[n];
makeSet(parent, rank, n);
int minCost = 0;
printf(
"Following are the edges in the constructed MST\n");
for (int i = 0; i < n; i++) {
int v1 = findParent(parent, edge[i][0]);
int v2 = findParent(parent, edge[i][1]);

Ayush Dixit 2100270130052


int wt = edge[i][2];
if (v1 != v2) {
unionSet(v1, v2, parent, rank, n);
minCost += wt;
printf("%d -- %d == %d\n", edge[i][0],
edge[i][1], wt);
}
}

printf("Minimum Cost Spanning Tree: %d\n", minCost);


}
int main()
{
int edge[5][3] = { { 0, 1, 10 },{ 0, 2, 6 }, { 0, 3, 5 }, { 1, 3, 15 },{ 2, 3, 4 } };
kruskalAlgo(5, edge);
return 0; }

OUTPUT:
Following are the edges in the constructed
MST 2 -- 3 == 4
0 -- 3 == 5
0 -- 1 == 10
Minimum Cost Spanning Tree: 19

Ayush Dixit 2100270130052


EXPERIMENT NO. 6
AIM: Program for Single Source Shortest path using
Dijkstra's Algorithm.
#include <limits.h>
#include <stdio.h>
#define V 9
int minDistance(int dist[], bool sptSet[]) {
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (sptSet[v] == false && dist[v] <= min)
min = dist[v], min_index = v;
return min_index;
}
int printSolution(int dist[], int n) {
printf("Vertex Distance from Source\n");
for (int i = 0; i < V; i++)
printf("%d \t %d\n", i, dist[i]);
return 0;
}
void dijkstra(int graph[V][V], int src) {
int dist[V];
bool sptSet[V];
for (int i = 0; i < V; i++)
dist[i] = INT_MAX, sptSet[i] = false;
dist[src] = 0;
for (int count = 0; count < V - 1; count++) {
int u = minDistance(dist, sptSet);

Ayush Dixit 2100270130052


sptSet[u] = true;
for (int v = 0; v < V; v++)
if (!sptSet[v] && graph[u][v] && dist[u] != INT_MAX && dist[u] +
graph[u][v] < dist[v]) dist[v] = dist[u] + graph[u][v];
}
printSolution(dist, V);
}
int main() {
int graph[V][V] = { { 0, 6, 0, 0, 0, 0, 0, 8, 0 },
{ 6, 0, 8, 0, 0, 0, 0, 13, 0 },
{ 0, 8, 0, 7, 0, 6, 0, 0, 2 },
{ 0, 0, 7, 0, 9, 14, 0, 0, 0 },
{ 0, 0, 0, 9, 0, 10, 0, 0, 0 },
{ 0, 0, 6, 14, 10, 0, 2, 0, 0 },
{ 0, 0, 0, 0, 0, 2, 0, 1, 6 },
{ 8, 13, 0, 0, 0, 0, 1, 0, 7 },
{ 0, 0, 2, 0, 0, 0, 6, 7, 0 }
};
dijkstra(graph, 0);
return 0;}
OUTPUT:
Vertex Distance from Source
00
16
2 14
3 21
4 21
5 11
69
78
8 15

Ayush Dixit 2100270130052


Ayush Dixit 2100270130052

You might also like