1. #include <stdio.
h>
void main() {
int i, position, swap, a[100], d, n;
printf("Enter the number of elements:\n");
scanf("%d", &n);
printf("Enter %d integers:\n", n);
// Read the elements of the array
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
// Selection Sort algorithm
for (i = 0; i < (n - 1); i++) {
position = i;
for (d = i + 1; d < n; d++) {
if (a[position] > a[d]) {
position = d; // Find the smallest element
// Swap if the position has changed
if (position != i) {
swap = a[i];
a[i] = a[position];
a[position] = swap;
} }
printf("Sorted list in ascending order is:\n");
for (i = 0; i < n; i++) {
printf("%d\t", a[i]);
printf("\n");
}
2. #include <stdio.h>
int matrix[25][25], visited_cities[10], limit, cost = 0;
int tsp(int c)
int count, nearest_city = 999;
int minimum = 999, temp;
for(count = 0; count < limit; count++)
if((matrix[c][count] != 0) && (visited_cities[count] == 0))
if(matrix[c][count] < minimum)
minimum = matrix[count][0] + matrix[c][count];
temp = matrix[c][count];
nearest_city = count;
if(minimum != 999)
cost = cost + temp;
return nearest_city;
void minimum_cost(int city)
int nearest_city;
visited_cities[city] = 1;
printf("%d ", city + 1);
nearest_city = tsp(city);
if(nearest_city == 999)
nearest_city = 0;
printf("%d", nearest_city + 1);
cost = cost + matrix[city][nearest_city];
return;
minimum_cost(nearest_city);
int main()
int i, j;
printf("Enter Total Number of Cities:\t");
scanf("%d", &limit);
printf("\nEnter Cost Matrix\n");
for(i = 0; i < limit; i++)
printf("\nEnter %d Elements in Row[%d]\n", limit, i + 1);
for(j = 0; j < limit; j++)
scanf("%d", &matrix[i][j]);
visited_cities[i] = 0;
printf("\nEntered Cost Matrix\n");
for(i = 0; i < limit; i++)
printf("\n");
for(j = 0; j < limit; j++)
printf("%d ", matrix[i][j]);
printf("\n\nPath:\t");
minimum_cost(0);
printf("\n\nMinimum Cost: \t");
printf("%d\n", cost);
return 0;
3. #include <stdio.h>
// Function to return the maximum of two integers
int max(int a, int b) {
return (a > b) ? a : b;
// Recursive function for the knapsack problem
int knapsack(int W, int wt[], int val[], int n) {
// Base case: no items or no remaining capacity
if (n == 0 || W == 0) {
return 0;
// If the weight of the nth item is more than the remaining capacity
// it cannot be included in the knapsack
if (wt[n - 1] > W) {
return knapsack(W, wt, val, n - 1);
// Return the maximum of two cases:
// 1. nth item included
// 2. nth item not included
else {
return max(val[n - 1] + knapsack(W - wt[n - 1], wt, val, n - 1),
knapsack(W, wt, val, n - 1));
int main() {
// Values (profits) associated with the items
int profit[] = {60, 100, 120};
// Weights of the items
int weight[] = {10, 20, 30};
// Maximum weight capacity of the knapsack
int W = 50;
// Number of items
int n = sizeof(profit) / sizeof(profit[0]);
// Calculate the maximum profit
int maxProfit = knapsack(W, weight, profit, n);
// Print the result
printf("Maximum profit: %d\n", maxProfit);
return 0;
4. #include <stdio.h>
#include <stdlib.h>
void knapsack(int M, int n, int w[20], int P[20]) {
double x[20]; // Solution vector
double sum = 0.0; // Maximum profit
int i, j;
double ratio[20];
int order[20];
int Rc = M; // Remaining capacity
// Calculate the ratio (profit/weight) for each item
for (i = 0; i < n; i++) {
ratio[i] = (double)P[i] / w[i];
order[i] = i; // Initialize the order with indices
// Sort items by ratio (bubble sort)
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (ratio[order[j]] < ratio[order[j + 1]]) {
int tempIndex = order[j];
order[j] = order[j + 1];
order[j + 1] = tempIndex;
// Initialize the solution vector to 0 (no items selected initially)
for (i = 0; i < n; i++) {
x[i] = 0;
// Select items for the knapsack based on sorted order
for (i = 0; i < n; i++) {
int currentIndex = order[i];
if (w[currentIndex] <= Rc) {
// If the item fits, take it completely
x[currentIndex] = 1;
Rc = Rc - w[currentIndex]; // Reduce remaining capacity
sum += P[currentIndex]; // Add profit of the item
} else {
// If the item doesn't fit, take the fractional part
x[currentIndex] = (double)Rc / w[currentIndex];
sum += P[currentIndex] * x[currentIndex];
break; // No more items can be taken after this point
// Output the solution vector and maximum profit
printf("Solution vector 'x': ");
for (i = 0; i < n; i++) {
printf("%.2f ", x[i]);
printf("\nMaximum profit: %.2f\n", sum);
int main() {
int M, n, i;
int w[20], p[20];
// Input for knapsack capacity (M) and number of items (n)
printf("Enter knapsack capacity (M): ");
scanf("%d", &M);
printf("Enter the number of objects (n): ");
scanf("%d", &n);
// Input for item weights
printf("Enter weights for each object:\n");
for (i = 0; i < n; i++) {
scanf("%d", &w[i]);
// Input for item profits
printf("Enter profits for each object: \n");
for (i = 0; i < n; i++) {
scanf("%d", &p[i]);
// Call the knapsack function
knapsack(M, n, w, p);
return 0;
5. #include <stdio.h>
#include <stdlib.h>
#define MAX 100
int c[MAX][MAX]; // Adjacency matrix
int visited[MAX];
int queue[MAX];
int n;
void BFS(int v) {
int front = 0, rear = -1, i;
visited[v] = 1; // Mark the starting vertex as visited
queue[++rear] = v; // Enqueue the starting vertex
while (front <= rear) {
v = queue[front++]; // Dequeue
printf("%d ", v); // Print the visited vertex
for (i = 1; i <= n; i++) {
if (c[v][i] == 1 && visited[i] == 0) {
queue[++rear] = i; // Enqueue unvisited adjacent vertex
visited[i] = 1; // Mark it as visited
void DFS(int v) {
int i;
visited[v] = 1; // Mark the current vertex as visited
printf("%d ", v); // Print the visited vertex
for (i = 1; i <= n; i++) {
if (c[v][i] == 1 && visited[i] == 0) {
DFS(i); // Recursively visit adjacent unvisited vertices
int main() {
int i, j, v;
printf("Enter the number of vertices in the graph: ");
scanf("%d", &n);
printf("Enter the adjacency matrix of the graph:\n");
for (i = 1; i <= n; i++) {
for (j = 1; j <= n; j++) {
scanf("%d", &c[i][j]);
// Initialize visited array to 0
for (i = 1; i <= n; i++) {
visited[i] = 0;
printf("Enter the starting vertex for BFS: ");
scanf("%d", &v);
printf("BFS traversal of the graph is: ");
BFS(v);
// Reset the visited array before DFS
for (i = 1; i <= n; i++) {
visited[i] = 0;
printf("\nEnter the starting vertex for DFS: ");
scanf("%d", &v);
printf("DFS traversal of the graph is: ");
DFS(v);
printf("\n");
return 0;
6. #include <stdio.h>
void findMinMax(int arr[], int start, int end, int *min, int *max) {
if (start == end) {
*min = *max = arr[start];
return;
if (end - start == 1) {
if (arr[start] < arr[end]) {
*min = arr[start];
*max = arr[end];
} else {
*min = arr[end];
*max = arr[start];
return;
int mid = (start + end) / 2;
int min1, max1, min2, max2;
findMinMax(arr, start, mid, &min1, &max1);
findMinMax(arr, mid + 1, end, &min2, &max2);
*min = (min1 < min2) ? min1 : min2;
*max = (max1 > max2) ? max1 : max2;
int main() {
int n, i;
int arr[100];
int min, max;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the array elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
findMinMax(arr, 0, n - 1, &min, &max);
printf("Minimum value in the array: %d\n", min);
printf("Maximum value in the array: %d\n", max);
return 0;
7.
#include <stdio.h>
void quicksort(int A[], int low, int high);
int partition(int A[], int low, int high);
int main()
int i, n;
int A[100];
printf("Enter the number of elements of the array: ");
scanf("%d", &n);
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++)
scanf("%d", &A[i]);
quicksort(A, 0, n - 1);
printf("Sorted list of elements: ");
for (i = 0; i < n; i++)
printf("%d ", A[i]);
return 0;
void quicksort(int A[], int low, int high)
int j;
if (low < high)
j = partition(A, low, high); // partitioning index
quicksort(A, low, j - 1); // Recursively sort the left part
quicksort(A, j + 1, high); // Recursively sort the right part
int partition(int A[], int low, int high)
int pivot, temp, i, j;
pivot = A[low]; // Choose the first element as pivot
i = low;
j = high;
while (i < j)
while (i <= high && A[i] <= pivot) // Move i to the right
i++;
while (A[j] > pivot) // Move j to the left
j--;
if (i < j)
// Swap elements A[i] and A[j]
temp = A[i];
A[i] = A[j];
A[j] = temp;
// Place pivot in its correct position
temp = A[low];
A[low] = A[j];
A[j] = temp;
return j; // Return the partition index
8. #include <stdio.h>
void mergesort(int arr[], int low, int high);
void merge(int arr[], int low, int mid, int high);
int main(void)
int array[100], n, i = 0;
printf("\t\t\tMerge Sort\n\n\n");
printf("Enter the number of elements to be sorted:\n");
scanf("%d", &n);
printf("\nEnter the elements to be sorted:\n");
for (i = 0; i < n; i++) // Corrected loop for input
printf("Array[%d] = ", i);
scanf("%d", &array[i]);
printf("Before Merge Sort:\n");
for (i = 0; i < n; i++) // Corrected loop for printing before sorting
{
printf("%d ", array[i]);
printf("\n");
mergesort(array, 0, n - 1); // Correct function call
printf("After Merge Sort:\n");
for (i = 0; i < n; i++) // Corrected loop for printing after sorting
printf("%d ", array[i]);
printf("\n");
return 0;
void mergesort(int arr[], int low, int high)
if (low < high)
int mid = (low + high) / 2;
mergesort(arr, low, mid); // Sort the left half
mergesort(arr, mid + 1, high); // Sort the right half
merge(arr, low, mid, high); // Merge the two halves
void merge(int arr[], int low, int mid, int high)
int temp[100];
int i = low, j = mid + 1, k = low;
// Merge the two halves into temp[]
while (i <= mid && j <= high)
if (arr[i] <= arr[j])
temp[k] = arr[i];
i++;
else
temp[k] = arr[j];
j++;
k++;
// Copy the remaining elements of the left half, if any
while (i <= mid)
temp[k] = arr[i];
i++;
k++;
// Copy the remaining elements of the right half, if any
while (j <= high)
temp[k] = arr[j];
j++;
k++;
// Copy the sorted subarray into the original array
for (i = low; i <= high; i++)
arr[i] = temp[i];
9.#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
// Create temporary arrays
int L[n1], R[n2];
// Copy data to temporary arrays L[] and R[]
for (int i = 0; i < n1; i++)
L[i] = arr[l + i];
for (int j = 0; j < n2; j++)
R[j] = arr[m + 1 + j];
// Merge the temporary arrays back into arr[l..r]
int 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++;
// Copy the remaining elements of L[], if any
while (i < n1) {
arr[k] = L[i];
i++;
k++;
// Copy the remaining elements of R[], if any
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;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
int main() {
int i, k;
srand(time(NULL));
int n_values[] = {10000, 15000, 20000};
int num_values = sizeof(n_values) / sizeof(n_values[0]);
for (k = 0; k < num_values; k++) {
int n = n_values[k];
int *arr = (int*) malloc(n * sizeof(int));
// Generate random numbers
for (i = 0; i < n; i++)
arr[i] = rand() % 100000;
clock_t start_time = clock();
mergeSort(arr, 0, n - 1);
clock_t end_time = clock();
double total_time = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
printf("Time taken to sort for n = %d: %f seconds\n", n, total_time);
free(arr);
return 0;