Data update
This commit is contained in:
parent
72eb4943cb
commit
4d5544505c
2347 changed files with 62432 additions and 16731 deletions
|
|
@ -1,54 +1,65 @@
|
|||
#include <stdio.h>
|
||||
int main (int argc, char *argv[]) {
|
||||
//here we check arguments
|
||||
if (argc < 2) {
|
||||
printf("Enter an argument. Example 1234 or dcba:\n");
|
||||
return 0;
|
||||
}
|
||||
//it calculates an array's length
|
||||
int x;
|
||||
for (x = 0; argv[1][x] != '\0'; x++);
|
||||
//buble sort the array
|
||||
int f, v, m;
|
||||
for(f=0; f < x; f++) {
|
||||
for(v = x-1; v > f; v-- ) {
|
||||
if (argv[1][v-1] > argv[1][v]) {
|
||||
m=argv[1][v-1];
|
||||
argv[1][v-1]=argv[1][v];
|
||||
argv[1][v]=m;
|
||||
#include <stdlib.h>
|
||||
|
||||
#define ARR_SIZE 4
|
||||
|
||||
int ifactorial(int n) {
|
||||
if (n == 0) {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
int result = 1;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
result *= i;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//it calculates a factorial to stop the algorithm
|
||||
char a[x];
|
||||
int k=0;
|
||||
int fact=k+1;
|
||||
while (k!=x) {
|
||||
a[k]=argv[1][k];
|
||||
k++;
|
||||
fact = k*fact;
|
||||
}
|
||||
a[k]='\0';
|
||||
//Main part: here we permutate
|
||||
int i, j;
|
||||
int y=0;
|
||||
char c;
|
||||
while (y != fact) {
|
||||
printf("%s\n", a);
|
||||
i=x-2;
|
||||
while(a[i] > a[i+1] ) i--;
|
||||
j=x-1;
|
||||
while(a[j] < a[i] ) j--;
|
||||
c=a[j];
|
||||
a[j]=a[i];
|
||||
a[i]=c;
|
||||
i++;
|
||||
for (j = x-1; j > i; i++, j--) {
|
||||
c = a[i];
|
||||
a[i] = a[j];
|
||||
a[j] = c;
|
||||
}
|
||||
y++;
|
||||
}
|
||||
void swap(char *a, char *b) {
|
||||
char tmp = *a;
|
||||
*a = *b;
|
||||
*b = tmp;
|
||||
}
|
||||
|
||||
void permutations(char *arr, int arr_size) {
|
||||
int factorial = ifactorial(arr_size);
|
||||
|
||||
int k = 0;
|
||||
while (k < factorial) {
|
||||
printf("%s\n", arr);
|
||||
|
||||
int i = arr_size - 2;
|
||||
while (arr[i] > arr[i + 1]) {
|
||||
i--;
|
||||
}
|
||||
|
||||
int j = arr_size - 1;
|
||||
while (arr[j] < arr[i]) {
|
||||
j--;
|
||||
}
|
||||
|
||||
swap(&arr[j], &arr[i]);
|
||||
i++;
|
||||
|
||||
for (j = arr_size - 1; j > i; i++, j--) {
|
||||
swap(&arr[i], &arr[j]);
|
||||
}
|
||||
|
||||
k++;
|
||||
}
|
||||
}
|
||||
|
||||
int compare(const void *a, const void *b) {
|
||||
return (int)(*(char *)a - *(char *)b); // ascending order
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
char arr[ARR_SIZE + 1] = "dbac";
|
||||
|
||||
qsort(arr, ARR_SIZE, sizeof(char), compare); // "abcd"
|
||||
permutations(arr, ARR_SIZE);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,23 +1,65 @@
|
|||
#include <stdio.h>
|
||||
int main() {
|
||||
char a[] = "4321"; //array
|
||||
int i, j;
|
||||
int f=24; //factorial
|
||||
char c; //buffer
|
||||
while (f--) {
|
||||
printf("%s\n", a);
|
||||
i=1;
|
||||
while(a[i] > a[i-1]) i++;
|
||||
j=0;
|
||||
while(a[j] < a[i])j++;
|
||||
c=a[j];
|
||||
a[j]=a[i];
|
||||
a[i]=c;
|
||||
i--;
|
||||
for (j = 0; j < i; i--, j++) {
|
||||
c = a[i];
|
||||
a[i] = a[j];
|
||||
a[j] = c;
|
||||
}
|
||||
}
|
||||
#include <stdlib.h>
|
||||
|
||||
#define ARR_SIZE 4
|
||||
|
||||
int ifactorial(int n) {
|
||||
if (n == 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int result = 1;
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
result *= i;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void swap(char *a, char *b) {
|
||||
char tmp = *a;
|
||||
*a = *b;
|
||||
*b = tmp;
|
||||
}
|
||||
|
||||
void permutations(char *arr, int arr_size) {
|
||||
int factorial = ifactorial(arr_size);
|
||||
|
||||
int k = factorial;
|
||||
while (k > 0) {
|
||||
printf("%s\n", arr);
|
||||
|
||||
int i = 1;
|
||||
while (arr[i] > arr[i - 1]) {
|
||||
i++;
|
||||
}
|
||||
|
||||
int j = 0;
|
||||
while (arr[j] < arr[i]) {
|
||||
j++;
|
||||
}
|
||||
|
||||
swap(&arr[j], &arr[i]);
|
||||
i--;
|
||||
|
||||
for (j = 0; j < i; i--, j++) {
|
||||
swap(&arr[i], &arr[j]);
|
||||
}
|
||||
|
||||
k--;
|
||||
}
|
||||
}
|
||||
|
||||
int compare(const void *a, const void *b) {
|
||||
return (int)(*(char *)b - *(char *)a); // descending order
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
char arr[ARR_SIZE + 1] = "2431";
|
||||
|
||||
qsort(arr, ARR_SIZE, sizeof(char), compare); // "4321"
|
||||
permutations(arr, ARR_SIZE);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,100 +0,0 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* print a list of ints */
|
||||
int show(int *x, int len)
|
||||
{
|
||||
int i;
|
||||
for (i = 0; i < len; i++)
|
||||
printf("%d%c", x[i], i == len - 1 ? '\n' : ' ');
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* next lexicographical permutation */
|
||||
int next_lex_perm(int *a, int n) {
|
||||
# define swap(i, j) {t = a[i]; a[i] = a[j]; a[j] = t;}
|
||||
int k, l, t;
|
||||
|
||||
/* 1. Find the largest index k such that a[k] < a[k + 1]. If no such
|
||||
index exists, the permutation is the last permutation. */
|
||||
for (k = n - 1; k && a[k - 1] >= a[k]; k--);
|
||||
if (!k--) return 0;
|
||||
|
||||
/* 2. Find the largest index l such that a[k] < a[l]. Since k + 1 is
|
||||
such an index, l is well defined */
|
||||
for (l = n - 1; a[l] <= a[k]; l--);
|
||||
|
||||
/* 3. Swap a[k] with a[l] */
|
||||
swap(k, l);
|
||||
|
||||
/* 4. Reverse the sequence from a[k + 1] to the end */
|
||||
for (k++, l = n - 1; l > k; l--, k++)
|
||||
swap(k, l);
|
||||
return 1;
|
||||
# undef swap
|
||||
}
|
||||
|
||||
void perm1(int *x, int n, int callback(int *, int))
|
||||
{
|
||||
do {
|
||||
if (callback) callback(x, n);
|
||||
} while (next_lex_perm(x, n));
|
||||
}
|
||||
|
||||
/* Boothroyd method; exactly N! swaps, about as fast as it gets */
|
||||
void boothroyd(int *x, int n, int nn, int callback(int *, int))
|
||||
{
|
||||
int c = 0, i, t;
|
||||
while (1) {
|
||||
if (n > 2) boothroyd(x, n - 1, nn, callback);
|
||||
if (c >= n - 1) return;
|
||||
|
||||
i = (n & 1) ? 0 : c;
|
||||
c++;
|
||||
t = x[n - 1], x[n - 1] = x[i], x[i] = t;
|
||||
if (callback) callback(x, nn);
|
||||
}
|
||||
}
|
||||
|
||||
/* entry for Boothroyd method */
|
||||
void perm2(int *x, int n, int callback(int*, int))
|
||||
{
|
||||
if (callback) callback(x, n);
|
||||
boothroyd(x, n, n, callback);
|
||||
}
|
||||
|
||||
/* same as perm2, but flattened recursions into iterations */
|
||||
void perm3(int *x, int n, int callback(int*, int))
|
||||
{
|
||||
/* calloc isn't strictly necessary, int c[32] would suffice
|
||||
for most practical purposes */
|
||||
int d, i, t, *c = calloc(n, sizeof(int));
|
||||
|
||||
/* curiously, with GCC 4.6.1 -O3, removing next line makes
|
||||
it ~25% slower */
|
||||
if (callback) callback(x, n);
|
||||
for (d = 1; ; c[d]++) {
|
||||
while (d > 1) c[--d] = 0;
|
||||
while (c[d] >= d)
|
||||
if (++d >= n) goto done;
|
||||
|
||||
t = x[ i = (d & 1) ? c[d] : 0 ], x[i] = x[d], x[d] = t;
|
||||
if (callback) callback(x, n);
|
||||
}
|
||||
done: free(c);
|
||||
}
|
||||
|
||||
#define N 4
|
||||
|
||||
int main()
|
||||
{
|
||||
int i, x[N];
|
||||
for (i = 0; i < N; i++) x[i] = i + 1;
|
||||
|
||||
/* three different methods */
|
||||
perm1(x, N, show);
|
||||
perm2(x, N, show);
|
||||
perm3(x, N, show);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
proc permlist k . list[] .
|
||||
proc permlist k &list[] .
|
||||
if k = len list[]
|
||||
print list[]
|
||||
return
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
prm:{$[x~*x;;:x@o@#x];(x-1){,/'((,(#*x)##x),x)m*(!l)+&\m:~=l:1+#x}/0}
|
||||
perm:{x[prm[#x]]
|
||||
perm:{x[prm[#x]]}
|
||||
|
||||
perm[" "\"some random text"]
|
||||
(("text";"text";"random";"some";"random";"some")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue