Daa Lab Manual
Daa Lab Manual
LAB MANUAL
CSE407
CT UNIVERSITY
UID : 71912086
List Of Programs:
Program Write a program for iterartive and recursive Binary Search
1
Program Write a program for Quick sort.
2
Program Write a program for Merge Sort
3
Program Write a program for Heap Sort
4
Program Write a program for insertion Sort
5
Program Write a program for for Selection Sort
6
Program a. To implement knapsack problem using Greedy
7 Technique
b. To implement 0/1 Knapsack problem using Dynamic
Programming
Linear Search:
return 0;
}
// Iterative Linear Search int
arr[], int n, int key, int index) { if (index >= n) return -1; if
if (arr[mid] ==
left = mid + 1;
else
right = mid - 1;
} return -
1;
arr[], int left, int right, int key) { if (left > right) return -1; int
key);
}
2. Write a Program to implement Quick Sort
QuickSort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array
around the picked pivot.
There are many different versions of QuickSort that pick pivot in different ways.
1. Always pick first element as pivot.
2. Always pick last element as pivot (implemented below)
3. Pick a random element as pivot.
4. Pick median as pivot.
The key process in QuickSort is partition. Target of partitions is, given an array and an element x
of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller
than x) before x, and put all greater elements (greater than x) after x.
include <stdio.h>
include <time.h>
voidExch(int *p, int *q)
{ int temp = *p; *p =
*q;
*q = temp;
}
voidQuickSort(int a[], int low,
int high){ int i, j, key, k;
if(low>=high) return;
key=low;
i=low+1; j=high;
while(i<=j){ while ( a[i] <=
a[key] ) i=i+1;
while ( a[j] > a[key] )
j=j -1;
if(i<j)
Exch(&a[i], &a[j]);
}
Exch(&a[j],
&a[key]);
QuickSort(a,
low, j-1);
QuickSort(a,
j+1, high);
}
void main(){
int n, a[1000],k;
clock_tst,et; double ts; clrscr();
printf("\n Enter How many
Numbers: "); scanf("%d", &n);
printf("\nTheRandom
Numbers are:\n"); for(k=1;
k<=n; k++){
a[k]=rand();
printf("%d\t",a[k])
;
}
st=clock();
QuickSor
t(a, 1, n);
et=clock();
ts=(double)(et-st)/CLOCKS
_PER_SEC; printf("\nSorted
Numbers are: \n "); for(k=1;
k<=n; k++)
printf("%d\t", a[k]); printf("\nThetime taken is %e",ts);
3. WRITE A PROGRAM TO IMPLEMENT MERGE SORT
#include <stdio.h>
// Main function
int main() {
int arr[] = {12, 11, 13, 5, 6, 7}; int
n = sizeof(arr) / sizeof(arr[0]);
return 0;
}
#include <stdio.h>
// Main function
int main() {
int arr[] = {12, 11, 13, 5, 6, 7}; int
n = sizeof(arr) / sizeof(arr[0]);
// Main function
int main() {
int arr[] = {64, 25, 12, 22, 11}; int
n = sizeof(arr) / sizeof(arr[0]);
return 0;
}
// Swap the found minimum element with the first element of the unsorted part
int temp = arr[minIndex]; arr[minIndex] = arr[i];
arr[i] = temp;
}
}
#include <stdio.h>
// Function to solve the fractional knapsack problem using the greedy technique
double knapsack(Item items[], int n, int capacity) {
qsort(items, n, sizeof(Item), compare); // Sort items by value/weight ratio
// Main function
int main() {
int n, capacity;
printf("Enter the number of items: ");
scanf("%d", &n);
return 0;
}
int main()
{
int profit[] = { 60, 100, 120 };
int weight[] = { 10, 20, 30 };
int W = 50;
int n = sizeof(profit) / sizeof(profit[0]);
printf("%d", knapSack(W, weight, profit, n));
return 0;
}
OUTPUT: 220
8. Find Minimum Cost Spanning Tree of a given undirected graph using Kruskal’s algorithm
#include <stdio.h>
#include <stdlib.h>
free(subsets);
int main() {
int vertices, edges;
printf("Enter the number of vertices and edges: ");
scanf("%d %d", &vertices, &edges);
KruskalMST(graph);
free(graph);
return 0;
}
• Kruskal's Algorithm:
• Steps:
• Key Components:
// Initial
#include <stdio.h>
if (board[i][j])
printf("Q ");
else printf(".
");
} printf("\
n");
} printf("\
n");
if (board[i][col])
return false;
}
// Check the upper-left diagonal for (int i =
if (board[i][j])
return false;
if (board[i][j])
return false;
}
return true;
printSolution(n);
return true;
solveNQueens(row + 1, n) || hasSolution;
board[row][col] = 0;
return hasSolution;
}
int main()
{ int n;
scanf("%d", &n);
// Initialize the
n; j++) { board[i][j] = 0;
}
// Solve the N-Queens problem if (!
above.\n");
return 0;
This program works for any N≥1N \geq 1N≥1 and prints all possible solutions for the N-Queens
problem