0% found this document useful (0 votes)
25 views13 pages

Ada Lab

Uploaded by

Vishal S
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)
25 views13 pages

Ada Lab

Uploaded by

Vishal S
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

1.

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

void selSort(int a[], int n)


{
int i, j, min;
for (i = 0; i < n; i++)
{
min = i;
for (j = i + 1; j < n; j++)
{
if (a[j] < a[min])
min = j;
}
int temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
int main()
{
int i, n;
printf("Enter the no of elements \n");
scanf("%d", &n);
int a[n];
srand(time(0));
for (i = 0; i < n; i++)
{
a[i] = rand() % 100;
}
clock_t start = clock();
selSort(a, n);
clock_t stop = clock();
double time = (stop - start);
printf("Sorted array \n");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nTime = %lf\n", time / CLOCKS_PER_SEC);
return 0;
}
2.

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

int partition(int a[], int low, int high); // Declaration of partition fun

void quickSort(int a[], int low, int high)


{
int j;
if (low < high)
{
j = partition(a, low, high);
quickSort(a, low, j - 1);
quickSort(a, j + 1, high);
}
}

int partition(int a[], int low, int high)


{
int i, j, key, temp;
key = a[low];
i = low + 1;
j = high;
while (1)
{
while (i < high && key >= a[i])
i++;

while (j >= low && key < a[j])


j--;

if (i < j)
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
else
{
temp = a[low];
a[low] = a[j];
a[j] = temp;
return j;
}
}
}
int main()
{
int i, n;
printf("Enter the no of elements \n");
scanf("%d", &n);
int a[n];
srand(time(0));
for (i = 0; i < n; i++)
{
a[i] = rand() % 100;
}
clock_t start = clock();
quickSort(a, 0, n - 1);
clock_t stop = clock();
double time = (stop - start);
printf("Sorted Array \n");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nTime = %lf\n", time / CLOCKS_PER_SEC);
return 0;
}

3.

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

int n;
void merge(int A[], int mid, int low, int high);

void mergeSort(int A[], int low, int high)


{
int mid;
if (low < high)
{
mid = (low + high) / 2;
mergeSort(A, low, mid);
mergeSort(A, mid + 1, high);
merge(A, mid, low, high);
}
}

void merge(int A[], int mid, int low, int high)


{
int i, j, k, B[100];
i = low;
j = mid + 1;
k = low;
while (i <= mid && j <= high)
{
if (A[i] < A[j])
{
B[k] = A[i];
i++;
k++;
}
else
{
B[k] = A[j];
j++;
k++;
}
}
while (i <= mid)
{
B[k] = A[i];
i++;
k++;
}
while (j <= high)
{
B[k] = A[j];
j++;
k++;
}
for (i = low; i <= high; i++)
A[i] = B[i];
}

int main()
{
int i, n;
printf("Enter the no of elements \n");
scanf("%d", &n);
int a[n];
srand(time(0));
for (i = 0; i < n; i++)
{
a[i] = rand() % 100;
}
clock_t start = clock();
mergeSort(a, 0, n - 1);
clock_t stop = clock();
double time = (stop - start);
printf("Sorted Array \n");
for (i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\nTime = %lf\n", time / CLOCKS_PER_SEC);
return 0;
}

4.

#include <stdio.h>

int parent[10], min, ne = 1, mincost = 0, cost[10][10];


int i, j, a, b, u, v, n;
int main()
{
printf("Enter the number of vertices:\n");
scanf("%d", &n);
printf("Enter the graph data:\n");
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
scanf("%d", &cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = 999;
}
}
while (ne < n)
{
for (i = 1, min = 999; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if (cost[i][j] < min)
{
min = cost[i][j];
a = u = i;
b = v = j;
}
}
}
while (parent[u])
u = parent[u];
while (parent[v])
v = parent[v];
if (u != v)
{
printf("\n%d.Edge(%d,%d)=%d", ne++, a, b, min);
mincost += min;
parent[v] = u;
}
cost[a][b] = cost[b][a] = 999;
}
printf("\nMINCOST = %d\n", mincost);
return 0;
}

5.

#include <stdio.h>

int visited[10], min, ne = 1, mincost = 0, cost[10][10];


int i, j, a, b, u, v, n;
int main()
{
printf("Enter the number of vertices:\n");
scanf("%d", &n);
printf("Enter the graph data:\n");
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
scanf("%d", &cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = 999;
}
}
for (i = 2; i <= n; i++)
visited[i] = 0;
printf("The edges of Spanning tree are:\n");
visited[1] = 1;

while (ne < n)


{
for (i = 1, min = 999; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
if (cost[i][j] < min)
{
if (visited[i] == 0)
continue;
else
{
min = cost[i][j];
a = u = i;
b = v = j;
}
}
}
}

if (visited[u] == 0 || visited[v] == 0)
{
printf("\n%d.Edge(%d,%d)=%d", ne++, u, v, min);
mincost += min;
visited[v] = 1;
}
cost[u][v] = cost[v][u] = 999;
}
printf("\nMINCOST = %d\n", mincost);
return 0;
}

6.

#include <stdio.h>
void dijikstra(int n, int v, int cost[10][10], int dist[])
{
int i, u, count, w, flag[10], min;
for (i = 1; i <= n; i++)
flag[i] = 0, dist[i] = cost[v][i];

flag[v] = 1;
count = 2;

while (count <= n)


{
min = 999;
for (w = 1; w <= n; w++)
if (dist[w] < min && !flag[w])
min = dist[w], u = w;

flag[u] = 1;
count++;

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


if (dist[u] + cost[u][w] < dist[w] && !flag[w])
dist[w] = dist[u] + cost[u][w];
}
}

int main()
{
int n, v, i, j, cost[10][10], dist[10];
printf("Enter no. of nodes:\n");
scanf("%d", &n);
printf("Enter cost matrix:\n");
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
{
scanf("%d", &cost[i][j]);
if (cost[i][j] == 0)
cost[i][j] = 999;
}
printf("Enter the source vertex:\n");
scanf("%d", &v);
dijikstra(n, v, cost, dist);
printf("Shortest path from\n");
for (j = 1; j <= n; j++)
if (j != v)
printf("%d -> %d :::: %d\n", v, j, dist[j]);
return 1;
}

7.

#include <stdio.h>

void main()
{
int i, j, a[10][10], n, m = 0, k, que[10], flag[10];
printf("enter the no of vertices\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
flag[i] = 0, que[i] = 0;
printf("enter the matrix:\n");
for (i = 1; i <= n; i++)
{
for (j = 1; j <= n; j++)
{
scanf("%d", &a[i][j]);
if (a[i][j])
flag[j]++;
}
}
for (k = 0; k < n; k++)
{
for (i = 1; i <= n; i++)
{
if (!flag[i])
{
flag[i] = -1;
que[++m] = i;
for (j = 1; j <= n; j++)
{
if (a[i][j] && flag[j] > 0)
{
flag[j]--;
}
}
}
}
}

if (m == n)
{
printf("The topological order is:\n");
for (i = 1; i <= n; i++)
{
printf("\t %d", que[i]);
}
}
else
printf("No topological order\n");
}

8.

#include <stdio.h>

// Function to find maximum of two integers


int max(int a, int b){
return (a > b) ? a : b;
}
int knapsack(int W, int wt[], int val[], int n)
{
int i, w;
int K[n + 1][W + 1];
// Build K[][] in bottom-up manner
for (i = 0; i <= n; i++)
{
for (w = 0; w <= W; w++)
{
if (i == 0 || w == 0)
K[i][w] = 0;
else if (wt[i - 1] <= w)
K[i][w] = max(val[i - 1] + K[i - 1][w - wt[i - 1]], K[i -
1][w]);
else
K[i][w] = K[i - 1][w];
}
}
return K[n][W];
}
int main()
{
int n, W;
printf("Enter number of items: ");
scanf("%d", &n);
int val[n], wt[n];
printf("Enter value and weight for each item:\n");
for (int i = 0; i < n; i++){
printf("Item %d value: ", i + 1);
scanf("%d", &val[i]);
printf("Item %d weight: ", i + 1);
scanf("%d", &wt[i]);
}
printf("Enter the capacity of knapsack: ");
scanf("%d", &W);
printf("Maximum value that can be obtained is %d\n", knapsack(W, wt, val,
n));
return 0;
}

9.

#include <stdio.h>
#include <stdlib.h>
int w[10], x[10], d, b = 1;
void sumsubset(int s, int k, int r)
{
int i;
x[k] = 1;
if (s + w[k] == d)
{
printf("\nSubset %d:\n", b++);
for (i = 1; i <= k; i++)
if (x[i] == 1)
printf("%d ", w[i]);
}
else if (s + w[k] + w[k + 1] <= d)
sumsubset(s + w[k], k + 1, r - w[k]);
if ((s + r - w[k] >= d) && (s + w[k + 1] <= d))
{
x[k] = 0;
sumsubset(s, k + 1, r - w[k]);
}
}
int main()
{
int n, i, sum = 0;
printf("Read number of elements: \n");
scanf("%d", &n);
printf("Read elements in increasing order\n");
for (i = 1; i <= n; i++)
{
scanf("%d", &w[i]);
sum = sum + w[i];
}
printf("Read the subset max value: \n");
scanf("%d", &d);
if (sum < d || w[i] > d)
{
printf("No subset");
exit(0);
}
sumsubset(0, 1, sum);
return 0;
}

Output:

Read number of elements:

Read elements in increasing order

1234

Read the subset max value:

Subset 1:

1 3

Subset 2:

4
10.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int a[30], count = 0;

int place(int pos){


int i;
for (i = 1; i < pos; i++){
if ((a[i] == a[pos]) || ((abs(a[i] - a[pos]) == abs(i - pos))))
return 0;
}
return 1;
}
void print_sol(int n){
int i, j;
count++;
printf("\n\nSolution #%d:\n", count);
for (i = 1; i <= n; i++){
for (j = 1; j <= n; j++){
if (a[i] == j)
printf("Q\t");
else
printf("*\t");
}
printf("\n");
}
}
void queen(int n){
int k = 1;
a[k] = 0;
while (k != 0){
do
{
a[k]++;
} while ((a[k] <= n) && !place(k));
if (a[k] <= n){
if (k == n)
print_sol(n);
else{
k++;
a[k] = 0;
}
}
else
k--;
}
}
void main(){
int i, n;
printf("Enter the number of Queens\n");
scanf("%d", &n);
queen(n);
printf("\nTotal solutions=%d", count);
}

Output:

Enter the number of Queens

Solution #1:

* Q * *

* * * Q

Q * * *

* * Q *

Solution #2:

* * Q *

Q * * *

* * * Q

* Q * *

Total solutions=2

You might also like