diff --git a/Makefile b/Makefile index d1ffce9..e3d47e4 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,8 @@ SRCS = push_swap.c \ minisort.c \ minisort_by_rank.c \ minisort_2ways_bubble_sort.c \ - special_sorts_3_5.c + special_sorts_3_5.c \ + sort_utils.c ODIR = ./builds diff --git a/builds/algo.o b/builds/algo.o index e1e99f8..b06c8b1 100644 Binary files a/builds/algo.o and b/builds/algo.o differ diff --git a/builds/algo_bubble_sort.o b/builds/algo_bubble_sort.o index 1e8fb0d..d22287c 100644 Binary files a/builds/algo_bubble_sort.o and b/builds/algo_bubble_sort.o differ diff --git a/builds/minisort_2ways_bubble_sort.o b/builds/minisort_2ways_bubble_sort.o index 0468880..6e11305 100644 Binary files a/builds/minisort_2ways_bubble_sort.o and b/builds/minisort_2ways_bubble_sort.o differ diff --git a/builds/sort_utils.o b/builds/sort_utils.o new file mode 100644 index 0000000..b072987 Binary files /dev/null and b/builds/sort_utils.o differ diff --git a/builds/special_sorts_3_5.o b/builds/special_sorts_3_5.o index f214ecb..9aa3774 100644 Binary files a/builds/special_sorts_3_5.o and b/builds/special_sorts_3_5.o differ diff --git a/includes/push_swap.h b/includes/push_swap.h index dfdeb5a..1dd5765 100644 --- a/includes/push_swap.h +++ b/includes/push_swap.h @@ -14,9 +14,9 @@ typedef struct s_stack } t_stack; /* -** luke's algo +** algo_bubble_sort.c // sort_luke.c */ -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); /* ** pushswap.c @@ -35,9 +35,8 @@ void ps_error(int i); void ps_stop(t_stack *stack, t_list *lst, int i); /* -** algo.c +** algo.c // sort.c */ -int sublist_size(t_stack *list); int nbr_element_smaller(t_stack *list, int size, int pivot); int find_pivot(t_stack *list, int size); int divide_a(t_stack **a, t_stack **b, t_list *solution); @@ -46,7 +45,18 @@ void send_sublist_to_a(t_stack **a, t_stack **b, t_list *solution); void hugo_sort(t_stack **a, t_stack **b, t_list *lst); /* -** spcecial_sorts_3_5.c +** sort_recursif.c +*/ + +/* +** sort_utils.c +*/ +int find_biggest(t_stack *list, int size); +int find_smallest(t_stack *list, int size); +int sublist_size(t_stack *list); + +/* +** spcecial_sorts_3_5.c // sort_specials_3_5.c */ void special_sort_3(t_stack **a, t_list *solution); void special_sort_5(t_stack **a, t_stack **b, t_list *solution); @@ -54,11 +64,10 @@ void special_sort_5(t_stack **a, t_stack **b, t_list *solution); /* ** minisort.c */ -int find_biggest(t_stack *list, int size); void minisort(t_stack **list, t_list *solution); /* -** minisort_by_rank.c +** minisort_by_rank.c // minisort_rank.c */ int find_rank(t_stack *list, int nbr, int i); int order_is(t_stack *list, int nbr); @@ -69,11 +78,11 @@ void sort_2(t_stack **a, t_list *solution); void mini_sort(t_stack **a, t_list *solution); /* -** minisort_2ways_bubble_sort.c +** minisort_2ways_bubble_sort.c // minisort_bubble.c */ void mark_sublist(t_stack *list); -int find_smallest(t_stack *list); -void sort_big(t_stack **a, t_stack **b, t_list *solution); +int find_smallest_bubble(t_stack *list); +void bubble_sort(t_stack **a, t_stack **b, t_list *solution); /* ** print.c diff --git a/push_swap b/push_swap index b29a48e..c03bef8 100755 Binary files a/push_swap and b/push_swap differ diff --git a/srcs/algo.c b/srcs/algo.c index c0c8d07..b59bcac 100644 --- a/srcs/algo.c +++ b/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); } diff --git a/srcs/algo_bubble_sort.c b/srcs/algo_bubble_sort.c index 01812fc..c4c9e17 100644 --- a/srcs/algo_bubble_sort.c +++ b/srcs/algo_bubble_sort.c @@ -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; diff --git a/srcs/minisort.c b/srcs/minisort.c index 5a49e4e..7b1869d 100644 --- a/srcs/minisort.c +++ b/srcs/minisort.c @@ -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; diff --git a/srcs/minisort_2ways_bubble_sort.c b/srcs/minisort_2ways_bubble_sort.c index 4a9f54a..4e3dcf8 100644 --- a/srcs/minisort_2ways_bubble_sort.c +++ b/srcs/minisort_2ways_bubble_sort.c @@ -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); diff --git a/srcs/sort_utils.c b/srcs/sort_utils.c new file mode 100644 index 0000000..a37764e --- /dev/null +++ b/srcs/sort_utils.c @@ -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); +} + diff --git a/srcs/special_sorts_3_5.c b/srcs/special_sorts_3_5.c index 98357b5..c8a1543 100644 --- a/srcs/special_sorts_3_5.c +++ b/srcs/special_sorts_3_5.c @@ -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); +} diff --git a/tester.sh b/tester.sh index aec71c1..1402dda 100644 --- a/tester.sh +++ b/tester.sh @@ -29,7 +29,7 @@ LIST=($(shuf -i 0-$RANGE -n $SIZE)) echo -e "${WHITE}test lancé avec la liste de nombres suivantes :${NC}" echo -e "${LIST[@]}" -OUTPUT=$(./$PRGM ${LIST[@]}) +OUTPUT=`./$PRGM ${LIST[@]}` COUNT=$(echo "$OUTPUT" | wc -l) RESULT=$(echo "$OUTPUT" | ./checker ${LIST[@]})