-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknapsack.c
70 lines (57 loc) · 1.78 KB
/
knapsack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <stdlib.h>
int unsorted(int *set, int length) {
for(int i = 0; i < length - 1; i++) {
if(set[i] > set[i + 1]) { return 1; }
}
return 0;
}
void selectionSort(int *set, int length) {
int min; // index to the leftmost (minimum) element in unsorted subset
for(int i = 0; i < length; i++) {
min = i; // denotes the start of unsorted subset
for(int j = i; j < length; j++) {
if(set[j] < set[min]) { // swap elements
int temp = set[min];
set[min] = set[j];
set[j] = temp;
}
}
}
}
void printSet(int *set, int length, int isSubset) {
if(isSubset) { printf("subset = {%d", set[0]); }
else { printf("set = {%d", set[0]); }
for(int i = 1; i < length; i++) {
int element = set[i];
if(isSubset && element == 0) { break; } // stop when reach end of subset (denoted by 0)
printf(", %d", element);
}
printf("}\n");
}
void parseSet(int *set, char **argv, int length) {
for(int i = 0; i < length; i++) {
set[i] = atoi(argv[i + 2]); // minus 2 because argc includes command to execute program and target value, which are not elements of the set
}
}
int *findSubset(int *set, int *subset, int length, int target, int sum, int index, int takeCount) {
if(sum == target) {
if(takeCount < length) {
// to denote end of subset
subset[takeCount] = 0;
}
return subset;
} else if(sum < target && index < length) {
// skip current number in set
int *skip = findSubset(set, subset, length, target, sum, index + 1, takeCount);
if(skip != 0) { return skip; }
// take current number in set
int num = set[index];
if(num != 0) { // subset should have no zeros
subset[takeCount] = num;
int *take = findSubset(set, subset, length, target, sum + num, index + 1, takeCount + 1);
if(take != 0) { return take; }
}
}
return 0;
}