Professional Documents
Culture Documents
17 34 724 2 Algorithm SecB
17 34 724 2 Algorithm SecB
Here,
Item n=5 & maximum weight capacity m = 16
X1 X2 X3 X4 X5
Item 1 2 3 4 5
Value 15 30 40 20 25
Weight 2 4 6 8 10
Step 1:
Value / Weight = P
P 7.5 7.5 6.6 2.5 2.5
Step 2:
Including highest (value/weight) first
X X1 X2 X3 X4 X5
1 1 1 1/2 0
Step 3:
Maximum profit is = 1*15+1*30+1*40+1/2*20=95
Step 4:
Verifying the weight = 1*2+1*4+1*6+1/2*8+0*10=16
1
2.
#include <iostream>
// The size of count must be at least the (max+1) but we cannot assign declare it as int count(max+1) in
C++ as it does not support dynamic memory allocation. So, its size is provided statically
int output[10];
int count[10];
max = array[i];
count[i] = 0;
count[array[i]]++;
// Find the index of each element of the original array in count array, and place the elements in output
array
2
output[count[array[i]] - 1] = array[i];
count[array[i]]--;
array[i] = output[i];
// Driver code
int main() {
countSort(array, n);
printArray(array, n);