This repository has been archived on 2025-11-15. You can view files and clone it, but cannot push or open issues or pull requests.
push-swap-gero/src/normalize.c

82 lines
1.7 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* normalize.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gechavia <chaviallegeraud@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/09/04 17:32:38 by gechavia #+# #+# */
/* Updated: 2025/09/04 17:32:39 by gechavia ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static void swap_vals(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
static void sort_int_array(int *arr, int n)
{
int i;
int j;
i = 0;
while (i < n)
{
j = i + 1;
while (j < n)
{
if (arr[j] < arr[i])
swap_vals(&arr[i], &arr[j]);
j = j + 1;
}
i = i + 1;
}
}
static int index_in_array(int *arr, int n, int v)
{
int i;
i = 0;
while (i < n)
{
if (arr[i] == v)
return (i);
i = i + 1;
}
return (-1);
}
void normalize_stack(t_stack *a)
{
int *tmp;
int j;
int idx;
tmp = (int *)malloc(sizeof(int) * a->size);
if (!tmp)
exit(1);
j = 0;
while (j < a->size)
{
tmp[j] = a->array[j];
j = j + 1;
}
sort_int_array(tmp, a->size);
j = 0;
while (j < a->size)
{
idx = index_in_array(tmp, a->size, a->array[j]);
a->array[j] = idx;
j = j + 1;
}
free(tmp);
}