diff --git a/Greedy Algorithms/fractionalKnapsack.java b/Greedy Algorithms/fractionalKnapsack.java new file mode 100644 index 0000000..7969a43 --- /dev/null +++ b/Greedy Algorithms/fractionalKnapsack.java @@ -0,0 +1,87 @@ +import java.lang.*; +import java.util.Arrays; +import java.util.Comparator; +import java.util.Scanner; +// Greedy approach +public class fractionalKnapsack { + + // Function to get maximum value + private static double getMaxValue(ItemValue[] arr,int capacity) + { + // 1. Sorting items by profit/weight ratio; + Arrays.sort(arr, new Comparator() { + public int compare(ItemValue item1,ItemValue item2) + { + double cpr1 = Double.valueOf(item1.profit/item1.weight); + double cpr2 = Double.valueOf(item2.profit/item2.weight); + + if (cpr1 < cpr2) + return 1; + else + return -1; + } + }); + + double totalValue = 0; + + for (ItemValue i : arr) { + + int curWt = (int)i.weight; + int curProfit = (int)i.profit; + + if (capacity - curWt >= 0) { + + // This weight can be picked whole + capacity = capacity - curWt; + totalValue += curProfit; + } + else { + + // Item cant be picked whole + double fraction = ((double)capacity / (double)curWt); + totalValue += (curProfit * fraction); + capacity= (int)(capacity - (curWt * fraction)); + break; + } + } + + return totalValue; + } + + // Item value class + static class ItemValue { + + int profit, weight; + // Item value function + // public ItemValue(int profit, int wt) + // { + // this.weight = wt; + // this.profit = profit; + // } + } + + // Driver code + public static void main(String[] args) + { + Scanner sc=new Scanner(System.in); + System.out.println("Enter the no. of objects"); + int n=sc.nextInt(); + ItemValue[] arr=new ItemValue[n]; + + for(int i=0;i