1
0
This repository has been archived on 2024-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
42-1st-piscine/ended/rush01/ex00/permutations.c

68 lines
1.6 KiB
C
Raw Permalink Normal View History

2023-08-06 20:12:38 +02:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* permutations.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kjimenez <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 14:35:22 by kjimenez #+# #+# */
/* Updated: 2022/07/24 14:35:25 by kjimenez ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "lib.h"
int perm_next(int n[])
{
int tail;
int i;
int j;
i = 3;
while (i > 0 && n[i - 1] >= n[i])
i--;
tail = i;
if (tail > 0)
{
j = 3;
while (j > tail && n[j] <= n[tail - 1])
j--;
ft_swap(&n[tail - 1], &n[j]);
}
i = tail;
j = 3;
while (i < j)
{
ft_swap(&n[i], &n[j]);
i++;
j--;
}
return (tail != 0);
}
int **generate_permutations(void)
{
int **permut;
int perm[4];
int i;
int j;
permut = (int **) malloc(24 * sizeof(int *));
j = 0;
i = 0;
while (++i <= 4)
perm[i - 1] = i;
while (1)
{
permut[j] = (int *) malloc(4 * sizeof(int));
i = -1;
while (++i < 4)
permut[j][i] = perm[i];
j++;
if (!perm_next(perm))
break ;
}
return (permut);
}