special sort for 5 ok

This commit is contained in:
hugogogo
2021-09-27 02:13:21 +02:00
parent bb2a08833a
commit 6a93eda973
15 changed files with 114 additions and 61 deletions

View File

@@ -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);
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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
View 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);
}

View File

@@ -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);
}