intra to gitea
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sort_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lfirmin <[email protected]> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/16 08:33:48 by lfirmin #+# #+# */
|
||||
/* Updated: 2024/11/03 17:57:30 by lfirmin ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include "../include/pushswap.h"
|
||||
|
||||
int get_median(t_stack *stack, int size)
|
||||
{
|
||||
int *table;
|
||||
int i;
|
||||
t_node *current;
|
||||
int median;
|
||||
|
||||
if (!size || !stack || !stack->top)
|
||||
return (ft_printf_fd(2, "Error\n"), -1);
|
||||
table = malloc(size * sizeof(int));
|
||||
if (!table)
|
||||
return (ft_printf_fd(2, "Error\n"), -1);
|
||||
i = 0;
|
||||
current = stack->top;
|
||||
while (current && i < size)
|
||||
{
|
||||
table[i++] = current->value;
|
||||
current = current->next;
|
||||
}
|
||||
sort_int_table(table, size);
|
||||
median = median_calc(table, size);
|
||||
return (free(table), median);
|
||||
}
|
||||
|
||||
void sort_int_table(int *tab, int size)
|
||||
{
|
||||
int a;
|
||||
int tmp;
|
||||
|
||||
a = 1;
|
||||
if (size > 1)
|
||||
{
|
||||
while (a < size)
|
||||
{
|
||||
if (tab[a] < tab[a - 1])
|
||||
{
|
||||
tmp = tab[a];
|
||||
tab[a] = tab[a - 1];
|
||||
tab[a - 1] = tmp;
|
||||
a = 0;
|
||||
}
|
||||
++a;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int median_calc(int *tab, int size)
|
||||
{
|
||||
if (size % 2 == 0)
|
||||
return (tab[size / 2 -1]);
|
||||
else
|
||||
return (tab[(size - 1) / 2 + 1]);
|
||||
}
|
||||
|
||||
int find_index_between(t_stack *stack_a, int value)
|
||||
{
|
||||
t_node *a_cur;
|
||||
int index;
|
||||
int found;
|
||||
|
||||
a_cur = stack_a->top;
|
||||
index = 1;
|
||||
found = 0;
|
||||
while (a_cur && a_cur->next && !found)
|
||||
{
|
||||
if (a_cur->value < value && a_cur->next->value > value)
|
||||
found = 1;
|
||||
else
|
||||
{
|
||||
index++;
|
||||
a_cur = a_cur->next;
|
||||
}
|
||||
}
|
||||
if (found)
|
||||
return (index);
|
||||
return (0);
|
||||
}
|
||||
Reference in New Issue
Block a user