-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermutations.h
More file actions
55 lines (43 loc) · 1.08 KB
/
permutations.h
File metadata and controls
55 lines (43 loc) · 1.08 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "bahash.h"
void show(char *array[], int size) {
int i;
for (i=0; size > i; i++)
printf("%s, ", array[i]);
printf("\n");
}
void swap (char **x, char **y) {
char *tmp = *x;
*x = *y;
*y = tmp;
}
unsigned long perm_size(int size) {
unsigned long permutations = 1;
int x;
for (x=1; x <= size; x++)
permutations = permutations * x;
return permutations;
}
dict *perms(char *array[], int size) {
dict *table;
int size_of_table = 12354;
int i;
unsigned long count=0;
unsigned long size_perm = perm_size(size) / 2; //ignore reverse cases
// Create hash table
table = create_dict(size_of_table);
// go through perms, only one way
while (count < size_perm) {
for(i=0; i < size-1; i++) {
swap(&array[i], &array[i+1]);
add_string(table, array, size);
count++;
}
swap(&array[0], &array[1]);
add_string(table, array, size);
count++;
}
return table;
}