Although these are two different questions, I wish to know the difference between these two loops. What is the difference between iterating over all weights for each item and iterating over all items for each weight?
for(int i=0; i < n; i++)
for(int j=W; j>=wt[i]; j--)
dp[j] = max(dp[j] , val[i] + dp[j-wt[i]]);
for (int i=0; i<=W; i++)
for (int j=0; j<n; j++)
if (wt[j] <= i)
dp[i] = max(dp[i], dp[i-wt[j]] + val[j]);