special sort for 5 ok
This commit is contained in:
21
srcs/algo.c
21
srcs/algo.c
@@ -1,22 +1,5 @@
|
||||
#include "push_swap.h"
|
||||
|
||||
// size is initialized to 1 because the loop check the next element, so it will end one step before reaching it
|
||||
// it checks the next one to avoid handle the first one with ->limit set to 1
|
||||
int sublist_size(t_stack *list)
|
||||
{
|
||||
int size;
|
||||
|
||||
if (list == NULL)
|
||||
return (0);
|
||||
size = 1;
|
||||
while (list->next != NULL && list->next->limit != 1)
|
||||
{
|
||||
list = list->next;
|
||||
size++;
|
||||
}
|
||||
return (size);
|
||||
}
|
||||
|
||||
int nbr_element_smaller(t_stack *list, int size, int pivot)
|
||||
{
|
||||
int nbr;
|
||||
@@ -126,7 +109,7 @@ void recursif_sort(t_stack **a, t_stack **b, t_list *solution)
|
||||
else
|
||||
{
|
||||
|
||||
// sort_big(a, b, solution)
|
||||
// bubble_sort(a, b, solution)
|
||||
mini_sort(a, solution);
|
||||
// minisort(a, solution);
|
||||
if (sublist_size(*b) > 4)
|
||||
@@ -143,10 +126,8 @@ void hugo_sort(t_stack **a, t_stack **b, t_list *solution)
|
||||
{
|
||||
if (sublist_size(*a) <= 3)
|
||||
special_sort_3(a, solution);
|
||||
/*
|
||||
else if (sublist_size(*a) <= 5)
|
||||
special_sort_5(a, b, solution);
|
||||
*/
|
||||
else
|
||||
recursif_sort(a, b, solution);
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ static int search_smaller(t_stack *stack, int *smaller)
|
||||
return (i_small);
|
||||
}
|
||||
|
||||
int bubble_sort(t_stack **a, t_stack **b, t_list *solution)
|
||||
int luke_bubble_sort(t_stack **a, t_stack **b, t_list *solution)
|
||||
{
|
||||
int smaller;
|
||||
|
||||
|
||||
@@ -1,28 +1,6 @@
|
||||
#include "push_swap.h"
|
||||
|
||||
/*
|
||||
int find_biggest(t_stack *list, int size)
|
||||
{
|
||||
int biggest;
|
||||
int position;
|
||||
int i;
|
||||
|
||||
biggest = list->n;
|
||||
position = 1;
|
||||
i = 1;
|
||||
while (list && --size)
|
||||
{
|
||||
i++;
|
||||
list = list->next;
|
||||
if (biggest < list->n)
|
||||
{
|
||||
position = i;
|
||||
biggest = list->n;
|
||||
}
|
||||
}
|
||||
return (position);
|
||||
}
|
||||
|
||||
void push_biggest(t_stack **list, t_list *solution, int size, int big)
|
||||
{
|
||||
t_stack *head;
|
||||
|
||||
@@ -12,7 +12,7 @@ void mark_sublist(t_stack *list)
|
||||
}
|
||||
}
|
||||
|
||||
int find_smallest(t_stack *list)
|
||||
int find_smallest_bubble(t_stack *list)
|
||||
{
|
||||
int i;
|
||||
int smallest;
|
||||
@@ -36,7 +36,7 @@ int find_smallest(t_stack *list)
|
||||
return (position);
|
||||
}
|
||||
|
||||
void sort_big(t_stack **a, t_stack **b, t_list *solution)
|
||||
void bubble_sort(t_stack **a, t_stack **b, t_list *solution)
|
||||
{
|
||||
int list_size;
|
||||
int next;
|
||||
@@ -47,7 +47,7 @@ void sort_big(t_stack **a, t_stack **b, t_list *solution)
|
||||
while (--list_size >= 0)
|
||||
{
|
||||
i = 0;
|
||||
next = find_smallest(*a);
|
||||
next = find_smallest_bubble(*a);
|
||||
if (next > 0)
|
||||
while (++i < next)
|
||||
ra(a, &solution);
|
||||
|
||||
63
srcs/sort_utils.c
Normal file
63
srcs/sort_utils.c
Normal file
@@ -0,0 +1,63 @@
|
||||
#include "push_swap.h"
|
||||
|
||||
// size is initialized to 1 because the loop check the next element, so it will end one step before reaching it
|
||||
// it checks the next one to avoid handle the first one with ->limit set to 1
|
||||
int sublist_size(t_stack *list)
|
||||
{
|
||||
int size;
|
||||
|
||||
if (list == NULL)
|
||||
return (0);
|
||||
size = 1;
|
||||
while (list->next != NULL && list->next->limit != 1)
|
||||
{
|
||||
list = list->next;
|
||||
size++;
|
||||
}
|
||||
return (size);
|
||||
}
|
||||
|
||||
int find_smallest(t_stack *list, int size)
|
||||
{
|
||||
int smallest;
|
||||
int position;
|
||||
int i;
|
||||
|
||||
smallest = list->n;
|
||||
position = 1;
|
||||
i = 1;
|
||||
while (list && --size)
|
||||
{
|
||||
i++;
|
||||
list = list->next;
|
||||
if (smallest > list->n)
|
||||
{
|
||||
position = i;
|
||||
smallest = list->n;
|
||||
}
|
||||
}
|
||||
return (position);
|
||||
}
|
||||
|
||||
int find_biggest(t_stack *list, int size)
|
||||
{
|
||||
int biggest;
|
||||
int position;
|
||||
int i;
|
||||
|
||||
biggest = list->n;
|
||||
position = 1;
|
||||
i = 1;
|
||||
while (list && --size)
|
||||
{
|
||||
i++;
|
||||
list = list->next;
|
||||
if (biggest < list->n)
|
||||
{
|
||||
position = i;
|
||||
biggest = list->n;
|
||||
}
|
||||
}
|
||||
return (position);
|
||||
}
|
||||
|
||||
@@ -27,9 +27,30 @@ void special_sort_3(t_stack **a, t_list *solution)
|
||||
sa(a, &solution);
|
||||
}
|
||||
|
||||
/*
|
||||
void special_sort_5(t_stack **a, t_stack **b, t_list *solution)
|
||||
{
|
||||
}
|
||||
*/
|
||||
int size;
|
||||
int position;
|
||||
int i;
|
||||
|
||||
size = sublist_size(*a);
|
||||
i = size - 3;
|
||||
while (size > 3)
|
||||
{
|
||||
position = find_smallest(*a, size);
|
||||
if (size - position >= position - 1)
|
||||
{
|
||||
while(--position)
|
||||
ra(a, &solution);
|
||||
}
|
||||
else
|
||||
while(position++ <= size)
|
||||
rra(a, &solution);
|
||||
pb(b, a, &solution);
|
||||
size--;
|
||||
}
|
||||
special_sort_3(a, solution);
|
||||
while (i--)
|
||||
pa(a, b, &solution);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user