Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DAA File (HAPPY MITTAL)

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 23

KRISHNA ENGINEERING COLLEGE

Department of Information Technology

Subject Name: Design and Analysis of Algorithm Lab


Subject Code: KCS-553
Student’s Name: HAPPY MITTAL
Roll No: 1901610130018
Section: A

Faculty Incharge: Mr. Abhuday Tripathi


KRISHNA ENGINEERING COLLEGE
Department of Information Technology
INDEX
S. No. Description of Experiment Date Remark

1 Program for Recursive Binary & Linear 05/10/2021


Search.
2 Program for Heap Sort. 12/10/2021
3 Program for Merge Sort. 12/10/2021
4 Program for Selection Sort. 19/10/2021
5 Program for Insertion Sort. 19/10/2021
6 Program for Quick Sort. 26/10/2021
7 Program to implement knapsack 09/11/2021
problem using Greedy Method.
8 Program to implement MST using 16/11/2021
Prim’s Algorithm.
9 Program to implement MST using 16/11/2021
Kruskal’s Algorithm.
10 Program to implement n-Queen 23/11/2021
problem using Backtracking.
PROGRAMS

1. Program for:
a. Binary Search (Recursive)
#include <stdio.h>

void binary_search(int [], int, int, int);


void bubble_sort(int [], int);

int main()
{
int key, size, i;
int list[25];

printf("Name – HAPPY MITTAL\n");


printf("Roll No - 1901610130018\n");
printf("Enter size of a list: ");
scanf("%d", &size);
printf("Enter elements\n");
for(i = 0; i < size; i++)
{
scanf("%d",&list[i]);
}
bubble_sort(list, size);
printf("\n");
printf("Enter key to search\n");
scanf("%d", &key);
binary_search(list, 0, size, key);

void bubble_sort(int list[], int size)


{
int temp, i, j;
for (i = 0; i < size; i++)
{
for (j = i; j < size; j++)
{
if (list[i] > list[j])
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
}

void binary_search(int list[], int lo, int hi, int key)


{
int mid;

if (lo > hi)


{
printf("Key not found\n");
return;
}
mid = (lo + hi) / 2;
if (list[mid] == key)
{
printf("Key found\n");
}
else if (list[mid] > key)
{
binary_search(list, lo, mid - 1, key);
}
else if (list[mid] < key)
{
binary_search(list, mid + 1, hi, key);
}
}

OUTPUT:
b. Linear Search (Recursive)
#include <stdio.h>
int RecursiveLS(int arr[], int value, int index, int n)
{
int pos = 0;

if(index >= n)
{
return 0;
}

else if (arr[index] == value)


{
pos = index + 1;
return pos;
}

else
{
return RecursiveLS(arr, value, index+1, n);
}
return pos;
}

int main()
{
int n, value, pos, m = 0, arr[100];
printf("Name – HAPPY MITTAL\n");
printf("Roll No - 1901610130018\n");
printf("Enter the total elements in the array ");
scanf("%d", &n);

printf("Enter the array elements\n");


for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}

printf("Enter the element to search ");


scanf("%d", &value);

pos = RecursiveLS(arr, value, 0, n);


if (pos != 0)
{
printf("Element found at pos %d ", pos);
}
else
{
printf("Element not found");
}
return 0;
}

OUTPUT:

2. Program for Heap Sort.


#include <stdio.h>

void main()
{
int heap[10], no, i, j, c, root, temp;
printf("Name – HAPPY MITTAL\n");
printf("Roll No - 1901610130018\n");
printf("\n Enter no of elements :");
scanf("%d", &no);
printf("\n Enter the numbers : ");
for (i = 0; i < no; i++)
scanf("%d", &heap[i]);
for (i = 1; i < no; i++)
{
c = i;
do
{
root = (c - 1) / 2;
if (heap[root] < heap[c])
{
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
}
c = root;
} while (c != 0);
}

printf("Heap array : ");


for (i = 0; i < no; i++)
printf("%d\t ", heap[i]);
for (j = no - 1; j >= 0; j--)
{
temp = heap[0];
heap[0] = heap[j];
heap[j] = temp;
root = 0;
do
{
c = 2 * root + 1;
if ((heap[c] < heap[c + 1]) && c < j-1)
c++;
if (heap[root]<heap[c] && c<j) /* again rearrange to
max heap array */
{
temp = heap[root];
heap[root] = heap[c];
heap[c] = temp;
}
root = c;
} while (c < j);
}
printf("\n The sorted array is : ");
for (i = 0; i < no; i++)
printf("\t %d", heap[i]);
}

OUTPUT:
3. Program for 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];

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++;
}
}

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("Name – HAPPY MITTAL\n");


printf("Roll No - 1901610130018\n");
printf("Given array is \n");
printArray(arr, arr_size);

mergeSort(arr, 0, arr_size - 1);

printf("\nSorted array is \n");


printArray(arr, arr_size);
return 0;
}

OUTPUT:

4. Program for 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;
swap(&arr[min_idx], &arr[i]);
}
}
void printArray(int arr[], int size)
{
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("Name – HAPPY MITTAL\n");
printf("Roll No - 1901610130018\n");
printf("Sorted array: \n");
printArray(arr, n);
return 0;
}

OUTPUT:

5. Program for Insertion Sort.


#include <stdio.h>
void main()
{
int n, i, j, temp;
int arr[64];
printf("Name – HAPPY MITTAL\n");
printf("Roll No - 1901610130018\n");
printf("Enter number of elements:\n");
scanf("%d", &n);
printf("Enter %d integers:\n", n);
for (i = 0; i < n; i++){
scanf("%d", &arr[i]);
}
for (i = 1 ; i <= n - 1; i++){
j = i;
while ( j > 0 && arr[j-1] > arr[j]){
temp = arr[j];
arr[j] = arr[j-1];
arr[j-1] = temp;
j--;
}
}
printf("Sorted list in ascending order:\n");
for (i = 0; i <= n - 1; i++)
{
printf("%d\n", arr[i]);
}
}
OUTPUT:
6. Program for Quick Sort.
#include <stdio.h>

void quicksort (int [], int, int);

int main()
{
int list[50];
int size, i;

printf("Name – HAPPY MITTAL\n");


printf("Roll No - 1901610130018\n");
printf("Enter the number of elements: ");
scanf("%d", &size);
printf("Enter the elements to be sorted:\n");
for (i = 0; i < size; i++)
{
scanf("%d", &list[i]);
}
quicksort(list, 0, size - 1);
printf("After applying quick sort\n");
for (i = 0; i < size; i++)
{
printf("%d ", list[i]);
}
printf("\n");

return 0;
}
void quicksort(int list[], int low, int high)
{
int pivot, i, j, temp;
if (low < high)
{
pivot = low;
i = low;
j = high;
while (i < j)
{
while (list[i] <= list[pivot] && i <= high)
{
i++;
}
while (list[j] > list[pivot] && j >= low)
{
j--;
}
if (i < j)
{
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
temp = list[j];
list[j] = list[pivot];
list[pivot] = temp;
quicksort(list, low, j - 1);
quicksort(list, j + 1, high);
}
}

OUTPUT:
7. Program to implement Knapsack Problem using Greedy
Solution.
#include <stdio.h>
void main()
{
int capacity, no_items, cur_weight, item;
int used[10];
float total_profit;
int i;
int weight[10];
int value[10];

printf("Name – HAPPY MITTAL\n");


printf("Roll No - 1901610130018\n");
printf("Enter the capacity of knapsack:\n");
scanf("%d", &capacity);

printf("Enter the number of items:\n");


scanf("%d", &no_items);

printf("Enter the weight and value of %d item:\n", no_items);


for (i = 0; i < no_items; i++)
{
printf("Weight[%d]:\t", i);
scanf("%d", &weight[i]);
printf("Value[%d]:\t", i);
scanf("%d", &value[i]);
}

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


used[i] = 0;

cur_weight = capacity;
while (cur_weight > 0)
{
item = -1;
for (i = 0; i < no_items; ++i)
if ((used[i] == 0) &&
((item == -1) || ((float) value[i] / weight[i] >
(float) value[item] / weight[item])))
item = i;

used[item] = 1;
cur_weight -= weight[item];
total_profit += value[item];
if (cur_weight >= 0)
printf("Added object %d (%d Rs., %dKg) completely in
the bag. Space left: %d.\n", item + 1, value[item], weight[item],
cur_weight);
else
{
int item_percent = (int) ((1 + (float) cur_weight /
weight[item]) * 100);
printf("Added %d%% (%d Rs., %dKg) of object %d in the
bag.\n", item_percent, value[item], weight[item], item + 1);
total_profit -= value[item];
total_profit += (1 + (float)cur_weight /
weight[item]) * value[item];
}
}

printf("Filled the bag with objects worth %.2f Rs.\n",


total_profit);
}
OUTPUT:

8. Program to impement MST using Prim’s Algorithm.


#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];
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 } };
printf("Name –HAPPY MITTAL\n");
printf("Roll No - 1901610130018\n");
primMST(graph);

return 0;
}

OUTPUT:

9. Program to implement MST using Kruskal’s Algorithm.


#include <stdio.h>

#define MAX 30

typedef struct edge {


int u, v, w;
} edge;

typedef struct edge_list {


edge data[MAX];
int n;
} edge_list;

edge_list elist;

int Graph[MAX][MAX], n;
edge_list spanlist;

void kruskalAlgo();
int find(int belongs[], int vertexno);
void applyUnion(int belongs[], int c1, int c2);
void sort();
void print();

void kruskalAlgo() {
int belongs[MAX], i, j, cno1, cno2;
elist.n = 0;

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


for (j = 0; j < i; j++) {
if (Graph[i][j] != 0) {
elist.data[elist.n].u = i;
elist.data[elist.n].v = j;
elist.data[elist.n].w = Graph[i][j];
elist.n++;
}
}

sort();

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


belongs[i] = i;

spanlist.n = 0;

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


cno1 = find(belongs, elist.data[i].u);
cno2 = find(belongs, elist.data[i].v);

if (cno1 != cno2) {
spanlist.data[spanlist.n] = elist.data[i];
spanlist.n = spanlist.n + 1;
applyUnion(belongs, cno1, cno2);
}
}
}

int find(int belongs[], int vertexno) {


return (belongs[vertexno]);
}

void applyUnion(int belongs[], int c1, int c2) {


int i;

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


if (belongs[i] == c2)
belongs[i] = c1;
}

void sort() {
int i, j;
edge temp;

for (i = 1; i < elist.n; i++)


for (j = 0; j < elist.n - 1; j++)
if (elist.data[j].w > elist.data[j + 1].w) {
temp = elist.data[j];
elist.data[j] = elist.data[j + 1];
elist.data[j + 1] = temp;
}
}

void print() {
int i, cost = 0;

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


printf("\n%d - %d : %d", spanlist.data[i].u,
spanlist.data[i].v, spanlist.data[i].w);
cost = cost + spanlist.data[i].w;
}

printf("\nSpanning tree cost: %d", cost);


}

int main() {
int i, j, total_cost;

printf("Name – HAPPY MITTAL\n");


printf("Roll No - 1901610130018\n");
n = 6;

Graph[0][0] = 0;
Graph[0][1] = 4;
Graph[0][2] = 4;
Graph[0][3] = 0;
Graph[0][4] = 0;
Graph[0][5] = 0;
Graph[0][6] = 0;

Graph[1][0] = 4;
Graph[1][1] = 0;
Graph[1][2] = 2;
Graph[1][3] = 0;
Graph[1][4] = 0;
Graph[1][5] = 0;
Graph[1][6] = 0;

Graph[2][0] = 4;
Graph[2][1] = 2;
Graph[2][2] = 0;
Graph[2][3] = 3;
Graph[2][4] = 4;
Graph[2][5] = 0;
Graph[2][6] = 0;

Graph[3][0] = 0;
Graph[3][1] = 0;
Graph[3][2] = 3;
Graph[3][3] = 0;
Graph[3][4] = 3;
Graph[3][5] = 0;
Graph[3][6] = 0;

Graph[4][0] = 0;
Graph[4][1] = 0;
Graph[4][2] = 4;
Graph[4][3] = 3;
Graph[4][4] = 0;
Graph[4][5] = 0;
Graph[4][6] = 0;

Graph[5][0] = 0;
Graph[5][1] = 0;
Graph[5][2] = 2;
Graph[5][3] = 0;
Graph[5][4] = 3;
Graph[5][5] = 0;
Graph[5][6] = 0;

kruskalAlgo();
print();
}

OUTPUT:

10. Program to implement n-Queen problem using Backtracking.


#include<stdio.h>
#include<math.h>

int board[20],count;
int main()
{
int n,i,j;
void queen(int row,int n);
printf("Name – HAPPY MITTAL\n");
printf("Roll No - 1901610130018\n");
printf(" - N Queens Problem Using Backtracking -");
printf("\n\nEnter number of Queens:");
scanf("%d",&n);
queen(1,n);
return 0;
}

void print(int n)
{
int i,j;
printf("\n\nSolution %d:\n\n",++count);

for(i=1;i<=n;++i)
printf("\t%d",i);

for(i=1;i<=n;++i)
{
printf("\n\n%d",i);
for(j=1;j<=n;++j)
{
if(board[i]==j)
printf("\tQ");
else
printf("\t-");
}
}
}

int place(int row,int column)


{
int i;
for(i=1;i<=row-1;++i)
{
if(board[i]==column)
return 0;
else
if(abs(board[i]-column)==abs(i-row))
return 0;
}

return 1;
}

void queen(int row,int n)


{
int column;
for(column=1;column<=n;++column)
{
if(place(row,column))
{
board[row]=column;
if(row==n)
print(n);
else
queen(row+1,n);
}
}
}

OUTPUT:

You might also like