Greedy algorithm for knapsack in java
<p>I am trying to write a very simple greedy algorithm for the knapsack problem. My inputs are two parallel arrays. One array contains the value of the item and the other array contains the weights. </p>

<p>The greedy algorithm that I’m trying to write would go as follows: check which item has the highest value and put this in the knapsack. Then set the value of this item to zero. Check again which item has the highest value and put it in the knapsack if the knapsack can still hold it. If it can hold it, set the value again to zero (after you’ve put it in) and start looking for the highest value again. If that knapsack cannot hold it anymore then just end the program.</p>

<p>I know that there are a lot of better greedy algorithms out there but this seems to me to be a pretty simple one and I think I could manage this. My problem is that I have to go through the entire values array to find the maximum value. Then when I’ve found it I put it in the knapsack and set the value to zero. But the problem then is that I have to go back in the for loop to find the new highest value item and put this in the knapsack. But I don’t know how I would go about doing this. </p>

<p>I am writing this in Java.</p>