0% found this document useful (0 votes)
8 views4 pages

Daa Lab Program 11to13

DAA lab file 3year aktu
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)
8 views4 pages

Daa Lab Program 11to13

DAA lab file 3year aktu
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/ 4

11.

Quick Sort with Time Complexity Analysis#include <iostream>


#include <vector>
#include <chrono>
#include <cstdlib>

using namespace std;


using namespace chrono;

int partition(vector<int>& arr, int low, int high) {


int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
swap(arr[i + 1], arr[high]);
return i + 1;
}

void quickSort(vector<int>& arr, int low, int high) {


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

int main() {
int n = 10000; // Example size greater than 5000
vector<int> arr(n);
for (int i = 0; i < n; i++) arr[i] = rand() % 100000;

auto start = high_resolution_clock::now();


quickSort(arr, 0, arr.size() - 1);
auto stop = high_resolution_clock::now();

auto duration = duration_cast<milliseconds>(stop - start);


cout << "Time taken for Quick Sort: " << duration.count() << " ms" << endl;

return 0;
}

12. Merge Sort with Time Complexity Analysis


#include <iostream>
#include <vector>
#include <chrono>
#include <cstdlib>

using namespace std;


using namespace chrono;

void merge(vector<int>& arr, int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;
vector<int> L(n1), R(n2);
for (int i = 0; i < n1; i++) L[i] = arr[left + i];
for (int j = 0; j < n2; j++) R[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) arr[k++] = L[i++];
else arr[k++] = R[j++];
}
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}

void mergeSort(vector<int>& arr, int left, int right) {


if (left >= right) return;
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}

int main() {
int n = 10000; // Example size greater than 5000
vector<int> arr(n);
for (int i = 0; i < n; i++) arr[i] = rand() % 100000;

auto start = high_resolution_clock::now();


mergeSort(arr, 0, arr.size() - 1);
auto stop = high_resolution_clock::now();

auto duration = duration_cast<milliseconds>(stop - start);


cout << "Time taken for Merge Sort: " << duration.count() << " ms" << endl;

return 0;
}

13.6. 0/1 Knapsack Problem

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// Dynamic Programming Knapsack


int knapsackDP(int W, vector<int>& wt, vector<int>& val, int n) {
vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));
for (int i = 1; i <= n; i++) {
for (int w = 1; w <= W; w++) {
if (wt[i - 1] <= w)
dp[i][w] = max(val[i - 1] + dp[i - 1][w - wt[i - 1]], dp[i - 1][w]);
else
dp[i][w] = dp[i - 1][w];
}
}
return dp[n][W];
}

// Greedy Knapsack
struct Item {
int value, weight;
double ratio;
};

bool compare(Item a, Item b) {


return a.ratio > b.ratio;
}

double knapsackGreedy(int W, vector<Item>& items) {


sort(items.begin(), items.end(), compare);
double totalValue = 0.0;
for (auto& item : items) {
if (W >= item.weight) {
W -= item.weight;
totalValue += item.value;
} else {
totalValue += item.ratio * W;
break;
}
}
return totalValue;
}

int main() {
vector<int> wt = {10, 20, 30};
vector<int> val = {60, 100, 120};
int W = 50;
int n = wt.size();

cout << "Dynamic Programming Knapsack: " << knapsackDP(W, wt, val, n) << endl;

vector<Item> items = {{60, 10}, {100, 20}, {120, 30}};


for (auto& item : items) item.ratio = (double)item.value / item.weight;
cout << "Greedy Knapsack: " << knapsackGreedy(W, items) << endl;

return 0;
}

You might also like