diff --git a/Greedy Algorithms/Knapsack.cpp b/Greedy Algorithms/Knapsack.cpp index 89b6e8794..128023e7d 100644 --- a/Greedy Algorithms/Knapsack.cpp +++ b/Greedy Algorithms/Knapsack.cpp @@ -8,7 +8,87 @@ struct Item }; -int main(int argc, char const *argv[]) +float profitPerUnit(Item x) { + return (float)(x.profit/x.weight); +} + +int partition (Item arr[], int low, int high) +{ + Item pivot = arr[high]; // pivot + int i = (low - 1); // Index of smaller element + + for (int j = low; j itemArray[i].weight; + cin>>itemArray[i].profit; + } + + quickSort(itemArray, 0, n); + + float maxProfit=0; + int i=0; + while(capacity>0 && iitemArray[i].weight) + { + maxProfit+=itemArray[i].profit; + capacity-=itemArray[i].weight; + cout<<"\n\t"<