copy on git

This commit is contained in:
Thomas Rieg 2025-11-28 19:50:58 +01:00
commit 42653de246
205 changed files with 7459 additions and 0 deletions

View file

@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_radixsort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/18 14:18:55 by alier #+# #+# */
/* Updated: 2025/02/16 19:06:42 by thrieg ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <stdbool.h>
#include "ft_sorting.h"
static void ft_naive_countsort2(struct s_countsort cs)
{
size_t i;
i = cs.size - 1;
while (true)
{
cs.tmp[cs.count[(cs.a[i] / cs.exp) % 10] - 1] = cs.a[i];
cs.count[(cs.a[i] / cs.exp) % 10]--;
if (i == 0)
break ;
i--;
}
ft_memcpy(cs.a, cs.tmp, cs.size * sizeof(*cs.a));
}
/*
* used by `ft_naive_radixsort`
*/
static void ft_naive_countsort(size_t size, unsigned int a[size],
unsigned int exp)
{
unsigned int *tmp;
unsigned int count[10];
size_t i;
tmp = malloc(size * sizeof(unsigned int));
ft_bzero(count, 10 * sizeof(unsigned int));
i = 0;
while (i < size)
{
count[(a[i] / exp) % 10]++;
i++;
}
i = 1;
while (i < 10)
{
count[i] += count[i - 1];
i++;
}
ft_naive_countsort2((struct s_countsort){size, a, exp, tmp, count});
free(tmp);
}
/*
* Worst-case performance: O(size * max_digits)
* Worst-case space complexity: O(size + max_digits)
*/
void ft_naive_radixsort(size_t size, unsigned int a[size])
{
unsigned int m;
unsigned int exp;
m = ft_max_uint(size, a);
exp = 1;
while (m / exp > 0)
{
ft_naive_countsort(size, a, exp);
exp *= 10;
}
}

Binary file not shown.

View file

@ -0,0 +1,93 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sorting.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alier <alier@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/31 09:34:43 by alier #+# #+# */
/* Updated: 2025/01/17 11:07:04 by alier ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <limits.h>
#include "ft_sorting.h"
static void ft_swap_bytes(void *a, void *b, size_t count)
{
uint8_t *x;
uint8_t *y;
uint8_t t;
size_t i;
x = (uint8_t *) a;
y = (uint8_t *) b;
i = 0;
while (i < count)
{
t = x[i];
x[i] = y[i];
y[i] = t;
i++;
}
}
/*
* Worst-case performance: O(n^2) comparisons, O(n^2) swaps
* Worst-case space complexity: O(n) total, O(1) auxiliary
*/
void ft_naive_bubblesort(void *base, size_t nmemb, size_t size,
t_compar compar)
{
bool swapped;
size_t i;
if (nmemb == 0)
return ;
swapped = true;
while (swapped)
{
swapped = false;
i = 0;
while (i < nmemb * size - size)
{
if (compar(base + i, base + i + size) > 0)
{
ft_swap_bytes(base + i, base + i + size, size);
swapped = true;
}
i += size;
}
}
}
static int compare_ints_asc(const void *a, const void *b)
{
if (*(int *) a > *(int *)b)
return (1);
else
return (-1);
}
int compare_strs_ptrs_asc(const void *a, const void *b)
{
const char *left;
const char *right;
left = *(const char **)a;
right = *(const char **)b;
return (ft_strcmp(left, right));
}
/*
* Worst-case performance: O(n^2) comparisons, O(n^2) swaps
* Worst-case space complexity: O(n) total, O(1) auxiliary
*/
void ft_naive_bubblesort_int_asc(int *base, size_t size)
{
ft_naive_bubblesort(base, size, sizeof(int), compare_ints_asc);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sorting.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alier <alier@student.42mulhouse.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/04 16:45:04 by alier #+# #+# */
/* Updated: 2025/01/17 11:07:18 by alier ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_SORTING_H
# define FT_SORTING_H
# include <stddef.h>
# include "../libft.h"
struct s_countsort
{
size_t size;
unsigned int *a;
unsigned int exp;
unsigned int *tmp;
unsigned int *count;
};
typedef int (*t_compar)(const void *, const void *);
int compare_strs_ptrs_asc(const void *a, const void *b);
void ft_naive_radixsort(size_t size, unsigned int a[size]);
void ft_naive_bubblesort_int_asc(int *base, size_t size);
void ft_naive_bubblesort(void *base, size_t nmemb, size_t size,
t_compar compar);
#endif

BIN
libft/sorting/ft_sorting.o Normal file

Binary file not shown.