0% found this document useful (0 votes)
30 views19 pages

3B Daa Program File

The document contains a C program to solve the knapsack problem using the greedy method. It defines an Item structure to represent items with a weight and value. It implements a compare function to sort items by value/weight ratio. The knapsack function implements the greedy algorithm to find the maximum value combination of items that fit in the knapsack.

Uploaded by

Mir
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)
30 views19 pages

3B Daa Program File

The document contains a C program to solve the knapsack problem using the greedy method. It defines an Item structure to represent items with a weight and value. It implements a compare function to sort items by value/weight ratio. The knapsack function implements the greedy algorithm to find the maximum value combination of items that fit in the knapsack.

Uploaded by

Mir
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
You are on page 1/ 19

WAP IN C TO IMPLEMENT HEAP SORT ALGORTHIM.

#include <stdio.h>
void main()
{
int heap[10], num, i, j, c, rootElement, tempVar;
printf("\n Enter num of elements :");
scanf("%d", &num);
printf("\n Enter the nums : ");
for (i = 0; i < num; i++)
scanf("%d", &heap[i]);
for (i = 1; i < num; i++)
{
c = i;
do
{
rootElement = (c - 1) / 2;
if (heap[rootElement] < heap[c]) /* to create MAX heap array */
{
tempVar = heap[rootElement];
heap[rootElement] = heap[c];
heap[c] = tempVar;
}
c = rootElement;
} while (c != 0);
}
printf("Heap array : ");
for (i = 0; i < num; i++)
printf("%d\t ", heap[i]);
for (j = num - 1; j >= 0; j--)
{
tempVar = heap[0];
heap[0] = heap[j];
heap[j] = tempVar;
rootElement = 0;
do
{
c = 2 * rootElement + 1;
if ((heap[c] < heap[c + 1]) && c < j-1)
c++;
if (heap[rootElement]<heap[c] && c<j)
{
tempVar = heap[rootElement];
heap[rootElement] = heap[c];
heap[c] = tempVar;
}
rootElement = c;
} while (c < j);
}
printf("\n The sorted array is : ");
for (i = 0; i < num; i++)
printf("\t %d", heap[i]);
}
OUTPUT:-
C Program to Perform Merge Sort using Recursion and Function

#include <stdio.h>
#include <stdlib.h>
// merge function
void Merge(int arr[], int left, int mid, int right)
{
int i, j, k;
int size1 = mid - left + 1;
int size2 = right - mid;
// created temporary array
int Left[size1], Right[size2];
// copying the data from arr to temporary array
for (i = 0; i < size1; i++)
Left[i] = arr[left + i];
for (j = 0; j < size2; j++)
Right[j] = arr[mid + 1 + j];
// merging of the array
i = 0; // intital index of first subarray
j = 0; // inital index of second subarray
k = left; // initial index of parent array
while (i < size1 && j < size2)
{
if (Left[i] <= Right[j])
{
arr[k] = Left[i];
i++;
}
else
{
arr[k] = Right[j];
j++;
}
k++;
}
// copying the elements from Left[], if any
while (i < size1)
{
arr[k] = Left[i];
i++;
k++;
}
// copying the elements from Right[], if any
while (j < size2)
{
arr[k] = Right[j];
j++;
k++;
}
}
//merge sort function
void Merge_Sort(int arr[], int left, int right)
{
if (left < right)
{
int mid = left + (right - left) / 2;

// recursive calling of merge_sort


Merge_Sort(arr, left, mid);
Merge_Sort(arr, mid + 1, right);
Merge(arr, left, mid, right);
}
}
// driver code
int main()
{
int size;
printf("Enter the size: ");
scanf("%d", &size);
int arr[size];
printf("Enter the elements of array: ");
for (int i = 0; i < size; i++)
{
scanf("%d", &arr[i]);
}
Merge_Sort(arr, 0, size - 1);
printf("The sorted array is: ");
for (int i = 0; i < size; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
OUTPUT:-
C Program to Implement Selection Sort
#include <stdio.h>
void selectionSort(int arr[], int size);
void swap(int *a, int *b);
/* * Selection sort function */
void selectionSort(int arr[], int size)
{
int i, j;
for (i = 0 ; i < size;i++)
{
for (j = i ; j < size; j++)
{
if (arr[i] > arr[j])
swap(&arr[i], &arr[j]);
}
}
}

/* Function to swap two variables */


void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}

/*
* Main Function
*/
int main()
{
int array[10], i, size;
printf("How many numbers you want to sort: ");
scanf("%d", &size);
printf("\nEnter %d numbers\t", size);
printf("\n");
for (i = 0; i < size; i++)
scanf("%d", &array[i]);
selectionSort(array, size);
printf("\nSorted array is ");
for (i = 0; i < size;i++)
printf(" %d ", array[i]);
return 0;
}
OUTPUT:-
C Program to sort an array in ascending order using Insertion
Sort
#include <stdio.h>
int main(void)
{
int n, i, j, temp;
int arr[64];
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; 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; i++)
{ printf("%d\n", arr[i]); }
return 0;
}
OUTPUT:-
C Program to sort an array of integers using Quick Sort without
recursion

#include <stdio.h>
#include <stdlib.h>
int quickSort(int *arr, int low, int high)
{
int i = low, j = high;
int pivot = arr[(low + high) / 2];
while (i <= j)
{ while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j)
{ int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
if (low < j)
quickSort(arr, low, j);
if (i < high)
quickSort(arr, i, high);
return 0;
}

int main(void)
{
puts("Enter the number of elements in the array: ");
int n;
scanf("%d", &n);
int arr[n];
puts("Enter the elements of the array: ");
for (int i = 0; i < n; i++)
{
printf("arr[%d]: ", i);
scanf("%d", &arr[i]);
}
int low = 0;
int high = n - 1;
int pivot = arr[high];
int k = low - 1;
for (int j = low; j < high; j++)
{ if (arr[j] <= pivot)
{ k++;
int temp = arr[k];
arr[k] = arr[j];
arr[j] = temp;
} }
int temp = arr[k + 1];
arr[k + 1] = arr[high];
arr[high] = temp;
int pi = k + 1;
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
puts("The sorted array is: ");
for (int i = 0; i < n; i++)
{ printf("%d ", arr[i]); }
return 0;
}
OUTPUT:-
C program to solve the knapsack problem using the greedy
method
#include <stdio.h>
#include <stdlib.h>

// A structure to represent an item


struct Item {
int weight;
int value;
};

// A function to compare two items


int compare(const void *a, const void *b) {
struct Item *item1 = (struct Item *)a;
struct Item *item2 = (struct Item *)b;

// Compare the value/weight ratios of the two items


double ratio1 = (double)item1->value / item1->weight;
double ratio2 = (double)item2->value / item2->weight;

// Return 1 if the first item has a higher value/weight ratio,


// -1 if the second item has a higher value/weight ratio, and 0 if they are equal
return ratio2 - ratio1;
}

// A function to solve the knapsack problem using the greedy method


int knapsack(struct Item items[], int n, int W) {
// Sort the items by their value/weight ratios in descending order
qsort(items, n, sizeof(struct Item), compare);

// Initialize the total value of the items in the knapsack


int totalValue = 0;
// Iterate over the items in the sorted order
for (int i = 0; i < n; i++) {
// If the weight of the current item is less than or equal to the remaining capacity of the
knapsack,
// add it to the knapsack
if (items[i].weight <= W) {
totalValue += items[i].value;
W -= items[i].weight;
} else {
// Otherwise, we can only add a fraction of the current item to the knapsack
double fraction = (double)W / items[i].weight;
totalValue += fraction * items[i].value;
W = 0;
break;
}
}

// Return the total value of the items in the knapsack


return totalValue;
}

// Main function
int main() {
// Get the number of items and the maximum weight capacity of the knapsack from the user
int n;
printf("Enter the number of items: ");
scanf("%d", &n);

int W;
printf("Enter the maximum weight capacity of the knapsack: ");
scanf("%d", &W);
// Create an array to store the items
struct Item items[n];
for (int i = 0; i < n; i++) {
printf("Enter the weight and value of item %d: ", i + 1);
scanf("%d %d", &items[i].weight, &items[i].value);
}

// Solve the knapsack problem using the greedy method


int totalValue = knapsack(items, n, W);

// Print the total value of the items in the knapsack


printf("The total value of the items in the knapsack is %d\n", totalValue);

return 0;
}
OUTPUT:-

You might also like