Compare commits
30 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae1308425c | ||
|
|
0c450d9a5a | ||
|
|
e90c354c4a | ||
|
|
fd3c78a1d7 | ||
|
|
ad26efb454 | ||
|
|
2d37058a43 | ||
|
|
60752e2faf | ||
|
|
db60d412e8 | ||
|
|
7f8d114b99 | ||
|
|
6a93eda973 | ||
|
|
bb2a08833a | ||
|
|
7d563f3362 | ||
|
|
682be2acbd | ||
|
|
f07533d101 | ||
|
|
e346fb3824 | ||
|
|
79f1ddb4ad | ||
|
|
7d9643bf63 | ||
|
|
8ded5f93cf | ||
|
|
115c03b8ca | ||
|
|
38a5a88d8a | ||
|
|
3bb142df8e | ||
|
|
8f70063f29 | ||
|
|
37b1daa814 | ||
|
|
95ae6c5c4e | ||
|
|
c6edab3883 | ||
|
|
023c7d29f6 | ||
|
|
f081f15f58 | ||
|
|
d69bb36234 | ||
|
|
fd675a88bf | ||
|
|
ed8268d7b0 |
17
Makefile
17
Makefile
@@ -27,13 +27,18 @@ SRCS = push_swap.c \
|
|||||||
push.c \
|
push.c \
|
||||||
rotate.c \
|
rotate.c \
|
||||||
reverse_rotate.c \
|
reverse_rotate.c \
|
||||||
algo_bubble_sort.c
|
algo_bubble_sort.c \
|
||||||
|
minisort.c \
|
||||||
|
minisort_by_rank.c \
|
||||||
|
minisort_2ways_bubble_sort.c \
|
||||||
|
special_sorts_3_5.c \
|
||||||
|
sort_utils.c
|
||||||
|
|
||||||
|
|
||||||
ODIR = ./builds
|
ODIR = ./builds
|
||||||
OBJS = $(SRCS:%.c=$(ODIR)/%.o)
|
OBJS = $(SRCS:%.c=$(ODIR)/%.o)
|
||||||
|
|
||||||
CFLAGS = -I$(IDIR) -g3 -Wall -Wextra -Werror
|
CFLAGS = -I$(IDIR) -I./libft/includes/ -g3 -Wall -Wextra -Werror
|
||||||
|
|
||||||
LFLAGS = -L$(LDIR) -l$(LIBS)
|
LFLAGS = -L$(LDIR) -l$(LIBS)
|
||||||
|
|
||||||
@@ -75,5 +80,13 @@ leaks: clean $(NAME)
|
|||||||
valgrind: clean $(NAME)
|
valgrind: clean $(NAME)
|
||||||
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) -p 4 67 9 76 98 7 1
|
valgrind --leak-check=full --show-leak-kinds=all ./$(NAME) -p 4 67 9 76 98 7 1
|
||||||
|
|
||||||
|
testp : clean $(NAME)
|
||||||
|
./$(NAME) -p 76 81 10 11 12 15 14 27 34 28 17 18 19 20 36 21 23 37 22 24 25 26 29 30 31 72 33 53 46 32 35 91 38 39 40 45 78 41 57 71 54 43 42 44 66 50 47 48 49 79 51 56 93 65 64 63 67 90 58 59 60 62 61 87 68 74 82 85 86 77 75 70 73 89 69 83 84 92 88 95 94 96 99 97 98 100 1 2 3 4 5 6 7 8 9 52 16 55 80 13
|
||||||
|
|
||||||
|
test : clean $(NAME)
|
||||||
|
./$(NAME) 76 81 10 11 12 15 14 27 34 28 17 18 19 20 36 21 23 37 22 24 25 26 29 30 31 72 33 53 46 32 35 91 38 39 40 45 78 41 57 71 54 43 42 44 66 50 47 48 49 79 51 56 93 65 64 63 67 90 58 59 60 62 61 87 68 74 82 85 86 77 75 70 73 89 69 83 84 92 88 95 94 96 99 97 98 100 1 2 3 4 5 6 7 8 9 52 16 55 80 13
|
||||||
|
|
||||||
|
# 83 94 28 17 18 36 20 33 39 53 23 37 22 24 45 25 29 78 26 30 31 72 46 32 35 70 38 93 50 91 40 5 41 57 71 66 92 54 67 75 65 42 43 44 74 79 47 48 49 88 51 63 7 68 87 61 82 4 90 58 59 62 60 1 85 89 96 98 100 84 69 77 73 3 86 99 97 6 2 9 8 52 80 16 55 13 76 81 10 11 12 15 14 27 34 56 21 64 95 19
|
||||||
|
|
||||||
.PHONY: all clean fclean re gcc
|
.PHONY: all clean fclean re gcc
|
||||||
|
|
||||||
|
|||||||
85
README.md
85
README.md
@@ -1,3 +1,31 @@
|
|||||||
|
## description du programme :
|
||||||
|
|
||||||
|
il faut trier une liste d'entiers, en utilisant uniquement deux piles (a et b) et 3 actions (swap, push, rotate) et leur derivées (11 actions au total)
|
||||||
|
|
||||||
|
la methode que j'ai utilisee s'inspire du tris [quicksort](https://www.youtube.com/watch?v=ywWBy6J5gz8), qui consiste a utiliser une valeur pivot et a envoyer sur une autre pile toutes les valeurs plus petites (ou plus grandes au choix), donc on se retrouve a la fin d'un tour avec deux piles qui ne sont pas triées mais avec dans l'une tous les nombres plus petits que le pivot, et dans l'autre tous ceux plus grands
|
||||||
|
|
||||||
|
et on repete l'operation jusqu'à ce que la liste soit triee
|
||||||
|
|
||||||
|
ici, comme on n'a que deux piles disponibles, la premiere operation sera simple, mais pour les operations suivantes il va falloir trouver un moyen de distinguer des sous-listes dans chaque piles
|
||||||
|
|
||||||
|
voici le deroulement :
|
||||||
|
|
||||||
|
## pistes d'ameliorations :
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## ressources :
|
||||||
|
* **testeur :** https://github.com/lmalki-h/push_swap_tester
|
||||||
|
* https://medium.com/nerd-for-tech/push-swap-tutorial-fa746e6aba1e
|
||||||
|
* https://medium.com/@jamierobertdawson/push-swap-the-least-amount-of-moves-with-two-stacks-d1e76a71789a
|
||||||
|
* ligne de commande pour generer une liste de nombres melangee :
|
||||||
|
`shuf -i MIN-MAX -n SIZE))`
|
||||||
|
avec MIN et MAX a remplacer par la valeure maximum
|
||||||
|
et SIZE par la taille de la liste
|
||||||
|
|
||||||
|
## comparaison des algorithmes de tris :
|
||||||
|
|
||||||
source des comparaisons ci-dessous : [https://www.youtube.com/watch?v=xoR-1KwQh2k](https://www.youtube.com/watch?v=xoR-1KwQh2k)
|
source des comparaisons ci-dessous : [https://www.youtube.com/watch?v=xoR-1KwQh2k](https://www.youtube.com/watch?v=xoR-1KwQh2k)
|
||||||
|
|
||||||
@@ -62,59 +90,8 @@ source des comparaisons ci-dessous : [https://www.youtube.com/watch?v=xoR-1KwQh2
|
|||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## pour utiliser l'algorithme de tris de ce programme dans votre propre programme, il faut :
|
autre source de comparaison : [https://www.toptal.com/developers/sorting-algorithms](https://www.toptal.com/developers/sorting-algorithms)
|
||||||
|
push_swap_visalizer : [https://github.com/o-reo/push_swap_visualizer](https://github.com/o-reo/push_swap_visualizer)
|
||||||
1. copier le fichier algo.c
|
|
||||||
|
|
||||||
2. mettre son prototype dans votre header.h : `void hugo_sort(t_stack **a, t_stack **b, t_list **solution)`
|
|
||||||
|
|
||||||
3. mettre le fichier dans vos srcs dans Makefile (`algo.c`)
|
|
||||||
|
|
||||||
4. et verifier que vos functions d'actions (push, swap, rotate) ont bien les memes prototypes :
|
|
||||||
- `t_list *sa(t_stack **stack, t_list **lst);`
|
|
||||||
- `t_list *ss(t_stack **a, t_stack **b, t_list **lst);`
|
|
||||||
- `t_list *pa(t_stack **dst, t_stack **src, t_list **lst);`
|
|
||||||
- `t_list *pb(t_stack **dst, t_stack **src, t_list **lst);`
|
|
||||||
- `t_list *ra(t_stack **stack, t_list **lst);`
|
|
||||||
- `t_list *rb(t_stack **stack, t_list **lst);`
|
|
||||||
- `t_list *rr(t_stack **a, t_stack **b, t_list **lst);`
|
|
||||||
- `t_list *rra(t_stack **stack, t_list **lst);`
|
|
||||||
- `t_list *rrb(t_stack **stack, t_list **lst);`
|
|
||||||
- `t_list *rrr(t_stack **a, t_stack **b, t_list **lst);`
|
|
||||||
|
|
||||||
## points particuliers :
|
|
||||||
|
|
||||||
- les fonctions d'actions (puches, swapes, rotates) recoivent une ou les deux piles a et b, et la liste chainee *solution* qui doit accueillir les noms des actions successives
|
|
||||||
- mais pour pouvoir imprimer l'état des deux piles a et b a chaque etape il faudrait que chaques fonctions recoivent les deux piles, ce qui n'est pas le cas : sa() par exemple ne recoit pas la pile b
|
|
||||||
- donc j'ai stocké les deux piles dans les deux premiers maillons de la chaine solution, car toutes les fonctions d'actions la recoivent
|
|
||||||
|
|
||||||
## ce programme push_swap.c s'organise de la facon suivante :
|
|
||||||
|
|
||||||
1. des fonctions pour faire fonctionner l'algorithme de tris (principalement parsing des donnees, et les actions utilisees par l'algorithme de tris : push, swap, rotate et reverse rotate) :
|
|
||||||
|
|
||||||
- **push_swap.c** : contient la fonction main et 4 autres fonctions :
|
|
||||||
- **is_valid** : verification d'erreur dans les arguments données, si aucun argument affiche l'usage
|
|
||||||
- **check_flag** : regarde la presence des flags, pour l'instant uniquement -p pour print les etapes du tri
|
|
||||||
- **init_stack** : parse la pile a trier dans une liste chainee
|
|
||||||
- **launch_algo** : lance l'algorithme de tri, en lui envoyant ce qui est necessaire (les deux piles a et b et la liste de solutions)
|
|
||||||
|
|
||||||
- **stop.c** : qui gere les erreurs, l'usage, mais aussi qui free avant de fermer le programme
|
|
||||||
- **ps_stop** : recoit toutes les listes chainees du programme, envoyer NULL si on ne veut pas free la liste, et le dernier int permet de savoir si on veut juste free sans arreter le programme (= -1), et si on veut signaler une erreur (>= 1)
|
|
||||||
- **ps_error** : la gestion des erreurs
|
|
||||||
- **ps_usage** : en cas d'erreur = 1, affiche l'usage du programme
|
|
||||||
|
|
||||||
- **print.c** : contient les fonctions necessaires pour afficher le resultat
|
|
||||||
- **print_result** : affiche le resultat, et si presence du flag -p affiche aussi les etapes de tris
|
|
||||||
- **fill_solution** : remplit la liste chainee solution au fur et a mesure de l'appel des fonctions push swap et rotate (pas optimisé pour raison de compatibilité avec l'appel des fonctions du programme de luke : les actions (push, swap, etc) ne recoivent pas de flag en argument, donc fill_solution remplis toutes les infos necessaires pour le flag, meme s'il n'y a pas de flag)
|
|
||||||
|
|
||||||
- toutes les fonctions d'action (push, swap, rotate, et reverse rotate)
|
|
||||||
|
|
||||||
|
|
||||||
2. les fonctions d'algorithme, qui sont appellees par launch_solution()
|
|
||||||
|
|
||||||
- **algo.c** : le fichier qui contient les fonctions d'algorithme de tri
|
|
||||||
- **hugo_sort** : pour l'instant une fonction test qui utilise toutes les actions (push, swap, rotate)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
|||||||
BIN
builds/algo.o
BIN
builds/algo.o
Binary file not shown.
Binary file not shown.
BIN
builds/minisort.o
Normal file
BIN
builds/minisort.o
Normal file
Binary file not shown.
BIN
builds/minisort_2ways_bubble_sort.o
Normal file
BIN
builds/minisort_2ways_bubble_sort.o
Normal file
Binary file not shown.
BIN
builds/minisort_by_rank.o
Normal file
BIN
builds/minisort_by_rank.o
Normal file
Binary file not shown.
BIN
builds/print.o
BIN
builds/print.o
Binary file not shown.
BIN
builds/push.o
BIN
builds/push.o
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
builds/rotate.o
BIN
builds/rotate.o
Binary file not shown.
BIN
builds/sort_utils.o
Normal file
BIN
builds/sort_utils.o
Normal file
Binary file not shown.
BIN
builds/special_sorts_3_5.o
Normal file
BIN
builds/special_sorts_3_5.o
Normal file
Binary file not shown.
BIN
builds/stop.o
BIN
builds/stop.o
Binary file not shown.
BIN
builds/swap.o
BIN
builds/swap.o
Binary file not shown.
BIN
checker_linux
Executable file
BIN
checker_linux
Executable file
Binary file not shown.
52
compare.sh
Executable file
52
compare.sh
Executable file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# usage of arguments
|
||||||
|
if [ $# -eq 3 ]
|
||||||
|
then # names = path to programs
|
||||||
|
NAME1=$1
|
||||||
|
NAME2=$2
|
||||||
|
PRGRM1=$1 # path to programs
|
||||||
|
PRGRM2=$2
|
||||||
|
SIZE=$3 # size of the list
|
||||||
|
elif [ $# -eq 5 ]
|
||||||
|
then # names given in arguments
|
||||||
|
NAME1=$1
|
||||||
|
NAME2=$3
|
||||||
|
PRGRM1=$2 # path to programs
|
||||||
|
PRGRM2=$4
|
||||||
|
SIZE=$5 # size of the list
|
||||||
|
else
|
||||||
|
echo "usage : ./compare_pushswap file1 file2 nb"
|
||||||
|
echo "or : ./compare_pushswap name1 file1 name2 file2 nb"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# range of random numbers
|
||||||
|
RANGE=100
|
||||||
|
if [ $SIZE -gt 99 ]; then RANGE=1000; fi
|
||||||
|
if [ $SIZE -gt 999 ]; then echo "size too big"; exit 0; fi
|
||||||
|
|
||||||
|
# generate the list of randoms differents numbers
|
||||||
|
LIST=($(shuf -i 0-$RANGE -n $SIZE))
|
||||||
|
echo -e "${LIST[@]}"
|
||||||
|
|
||||||
|
# player equal number of line of programm output
|
||||||
|
PLAYER1=$(./$PRGRM1 ${LIST[@]} | wc -l)
|
||||||
|
PLAYER2=$(./$PRGRM2 ${LIST[@]} | wc -l)
|
||||||
|
|
||||||
|
# colors variables
|
||||||
|
RED='\033[1;31m'
|
||||||
|
GREEN='\033[1;32m'
|
||||||
|
NC='\033[0m'
|
||||||
|
|
||||||
|
if [ $PLAYER1 -lt $PLAYER2 ]
|
||||||
|
then
|
||||||
|
COLOR1=$GREEN
|
||||||
|
COLOR2=$RED
|
||||||
|
else
|
||||||
|
COLOR1=$RED
|
||||||
|
COLOR2=$GREEN
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -e "${COLOR1}$NAME1 : $PLAYER1${NC}"
|
||||||
|
echo -e "${COLOR2}$NAME2 : $PLAYER2${NC}"
|
||||||
BIN
correction_pushswap.png
Normal file
BIN
correction_pushswap.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.3 MiB |
@@ -1,21 +1,24 @@
|
|||||||
|
|
||||||
#ifndef PUSH_SWAP_H
|
#ifndef PUSH_SWAP_H
|
||||||
# define PUSH_SWAP_H
|
# define PUSH_SWAP_H
|
||||||
# include "../libft/includes/libft.h"
|
# include "libft.h"
|
||||||
# include <unistd.h> // read(), write(), sleep()
|
# include <unistd.h> // read(), write(), sleep()
|
||||||
# include <stdlib.h> // malloc(), free(), exit(), atoi()
|
# include <stdlib.h> // malloc(), free(), exit(), atoi()
|
||||||
|
|
||||||
|
# define INT_MAX 2147483647
|
||||||
|
# define INT_MIN -2147483648
|
||||||
|
|
||||||
typedef struct s_stack
|
typedef struct s_stack
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
|
int limit;
|
||||||
struct s_stack *next;
|
struct s_stack *next;
|
||||||
} t_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
|
** pushswap.c
|
||||||
@@ -31,44 +34,91 @@ t_list *launch_algo(t_stack **a, t_stack **b, int i);
|
|||||||
void print_test(t_list *lst, char *s);
|
void print_test(t_list *lst, char *s);
|
||||||
void ps_usage(void);
|
void ps_usage(void);
|
||||||
void ps_error(int i);
|
void ps_error(int i);
|
||||||
void ps_stop(t_stack *stack, t_list *lst, int i);
|
void ps_stop(char **tab, t_list *lst, int i);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** algo.c
|
** algo.c // sort.c
|
||||||
*/
|
*/
|
||||||
void hugo_sort(t_stack **a, t_stack **b, t_list *lst);
|
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);
|
||||||
|
int divide_b(t_stack **a, t_stack **b, t_list *solution);
|
||||||
|
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 *solution);
|
||||||
|
|
||||||
|
/*
|
||||||
|
** 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);
|
||||||
|
|
||||||
|
/*
|
||||||
|
** minisort.c
|
||||||
|
*/
|
||||||
|
void minisort(t_stack **list, t_list *solution);
|
||||||
|
|
||||||
|
/*
|
||||||
|
** 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);
|
||||||
|
void sort_5(t_stack **a, t_list *solution);
|
||||||
|
void sort_4(t_stack **a, t_list *solution);
|
||||||
|
void sort_3(t_stack **a, t_list *solution);
|
||||||
|
void sort_2(t_stack **a, t_list *solution);
|
||||||
|
void mini_sort(t_stack **a, t_list *solution);
|
||||||
|
|
||||||
|
/*
|
||||||
|
** minisort_2ways_bubble_sort.c // minisort_bubble.c
|
||||||
|
*/
|
||||||
|
void mark_sublist(t_stack *list);
|
||||||
|
int find_smallest_bubble(t_stack *list);
|
||||||
|
void bubble_sort(t_stack **a, t_stack **b, t_list *solution);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** print.c
|
** print.c
|
||||||
*/
|
*/
|
||||||
|
void mark_step(t_list *solution, char *step);
|
||||||
void fill_solution(t_list *lst, char *s);
|
void fill_solution(t_list *lst, char *s);
|
||||||
|
char *fill_line(t_stack *list, char *stack);
|
||||||
void print_result(t_list *lst, int i);
|
void print_result(t_list *lst, int i);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** swap
|
** swap
|
||||||
*/
|
*/
|
||||||
t_list *sa(t_stack **a, t_list **lst);
|
t_list *sa(t_stack **a, t_list **solution);
|
||||||
t_list *sb(t_stack **b, t_list **lst);
|
t_list *sb(t_stack **b, t_list **solution);
|
||||||
t_list *ss(t_stack **a, t_stack **b, t_list **lst);
|
t_list *ss(t_stack **a, t_stack **b, t_list **solution);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** push
|
** push
|
||||||
*/
|
*/
|
||||||
t_list *pa(t_stack **dst, t_stack **src, t_list **lst);
|
t_list *pa(t_stack **a, t_stack **b, t_list **solution);
|
||||||
t_list *pb(t_stack **dst, t_stack **src, t_list **lst);
|
t_list *pb(t_stack **b, t_stack **a, t_list **solution);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** rotate
|
** rotate
|
||||||
*/
|
*/
|
||||||
t_list *ra(t_stack **stack, t_list **lst);
|
t_list *ra(t_stack **a, t_list **solution);
|
||||||
t_list *rb(t_stack **stack, t_list **lst);
|
t_list *rb(t_stack **b, t_list **solution);
|
||||||
t_list *rr(t_stack **a, t_stack **b, t_list **lst);
|
t_list *rr(t_stack **a, t_stack **b, t_list **solution);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** reverse rotate
|
** reverse rotate
|
||||||
*/
|
*/
|
||||||
t_list *rra(t_stack **stack, t_list **lst);
|
t_list *rra(t_stack **a, t_list **solution);
|
||||||
t_list *rrb(t_stack **stack, t_list **lst);
|
t_list *rrb(t_stack **b, t_list **solution);
|
||||||
t_list *rrr(t_stack **a, t_stack **b, t_list **lst);
|
t_list *rrr(t_stack **a, t_stack **b, t_list **solution);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
34
lucky_opti.sh
Normal file
34
lucky_opti.sh
Normal file
File diff suppressed because one or more lines are too long
1
push_swap_tester
Submodule
1
push_swap_tester
Submodule
Submodule push_swap_tester added at 0fd5c414c6
135
srcs/algo.c
135
srcs/algo.c
@@ -1,21 +1,124 @@
|
|||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
int nbr_element_smaller(t_stack *list, int size, int pivot)
|
||||||
|
{
|
||||||
|
int nbr;
|
||||||
|
|
||||||
|
nbr = 0;
|
||||||
|
while (size-- > 0)
|
||||||
|
{
|
||||||
|
if (list->n < pivot)
|
||||||
|
nbr++;
|
||||||
|
list = list->next;
|
||||||
|
}
|
||||||
|
return (nbr);
|
||||||
|
}
|
||||||
|
|
||||||
|
// pivot is the smallest of the bigger group
|
||||||
|
int find_pivot(t_stack *list, int size)
|
||||||
|
{
|
||||||
|
int pivot;
|
||||||
|
t_stack *head;
|
||||||
|
|
||||||
|
pivot = list->n;
|
||||||
|
head = list;
|
||||||
|
while (head->next && nbr_element_smaller(list, size, pivot) != size / 2)
|
||||||
|
{
|
||||||
|
head = head->next;
|
||||||
|
pivot = head->n;
|
||||||
|
}
|
||||||
|
return (pivot);
|
||||||
|
}
|
||||||
|
|
||||||
|
int divide_a(t_stack **a, t_stack **b, t_list *solution)
|
||||||
|
{
|
||||||
|
int list_size;
|
||||||
|
int pivot;
|
||||||
|
t_stack *first_rotate;
|
||||||
|
|
||||||
|
first_rotate = NULL;
|
||||||
|
list_size = sublist_size(*a);
|
||||||
|
pivot = find_pivot(*a, list_size);
|
||||||
|
(*a)->limit = 0;
|
||||||
|
while (list_size-- > 0)
|
||||||
|
{
|
||||||
|
if ((*a)->n >= pivot)
|
||||||
|
{
|
||||||
|
if (!first_rotate)
|
||||||
|
first_rotate = *a;
|
||||||
|
ra(a, &solution);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
pb(b, a, &solution);
|
||||||
|
}
|
||||||
|
while (*a != first_rotate)
|
||||||
|
rra(a, &solution);
|
||||||
|
(*a)->limit = 1;
|
||||||
|
(*b)->limit = 1;
|
||||||
|
mark_step(solution, "divide_a");
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int divide_b(t_stack **a, t_stack **b, t_list *solution)
|
||||||
|
{
|
||||||
|
int list_size;
|
||||||
|
int pivot;
|
||||||
|
t_stack *first_rotate;
|
||||||
|
|
||||||
|
first_rotate = NULL;
|
||||||
|
list_size = sublist_size(*b);
|
||||||
|
pivot = find_pivot(*b, list_size);
|
||||||
|
(*b)->limit = 0;
|
||||||
|
while (list_size-- > 0)
|
||||||
|
{
|
||||||
|
if ((*b)->n >= pivot)
|
||||||
|
pa(a, b, &solution);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (!first_rotate)
|
||||||
|
first_rotate = *b;
|
||||||
|
rb(b, &solution);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (*b != first_rotate)
|
||||||
|
rrb(b, &solution);
|
||||||
|
(*a)->limit = 1;
|
||||||
|
(*b)->limit = 1;
|
||||||
|
mark_step(solution, "divide_b");
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void send_sublist_to_a(t_stack **a, t_stack **b, t_list *solution)
|
||||||
|
{
|
||||||
|
int list_size;
|
||||||
|
|
||||||
|
list_size = sublist_size(*b);
|
||||||
|
(*b)->limit = 0;
|
||||||
|
while (list_size-- > 0)
|
||||||
|
pa(a, b, &solution);
|
||||||
|
(*a)->limit = 1;
|
||||||
|
if (*b)
|
||||||
|
(*b)->limit = 1;
|
||||||
|
mark_step(solution, "send_sublist_to_a");
|
||||||
|
}
|
||||||
|
|
||||||
void hugo_sort(t_stack **a, t_stack **b, t_list *solution)
|
void hugo_sort(t_stack **a, t_stack **b, t_list *solution)
|
||||||
{
|
{
|
||||||
sa(a, &solution);
|
if (sublist_size(*a) > 4)
|
||||||
pb(b, a, &solution);
|
divide_a(a, b, solution);
|
||||||
pb(b, a, &solution);
|
else
|
||||||
pb(b, a, &solution);
|
{
|
||||||
pb(b, a, &solution);
|
|
||||||
pb(b, a, &solution);
|
// bubble_sort(a, b, solution)
|
||||||
pa(a, b, &solution);
|
mini_sort(a, solution);
|
||||||
sa(a, &solution);
|
// minisort(a, solution);
|
||||||
sb(b, &solution);
|
if (sublist_size(*b) > 4)
|
||||||
sa(a, &solution);
|
divide_b(a, b, solution);
|
||||||
rra(a, &solution);
|
else if (sublist_size(*b) > 0)
|
||||||
rrb(b, &solution);
|
send_sublist_to_a(a, b, solution);
|
||||||
rrr(a, b, &solution);
|
else
|
||||||
ra(a, &solution);
|
return ;
|
||||||
rb(b, &solution);
|
}
|
||||||
rr(a, b, &solution);
|
hugo_sort(a, b, solution);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ static int search_smaller(t_stack *stack, int *smaller)
|
|||||||
return (i_small);
|
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;
|
int smaller;
|
||||||
|
|
||||||
|
|||||||
45
srcs/minisort.c
Normal file
45
srcs/minisort.c
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
void push_biggest(t_stack **list, t_list *solution, int size, int big)
|
||||||
|
{
|
||||||
|
t_stack *head;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
head = *list;
|
||||||
|
i = size;
|
||||||
|
while (i--)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void minisort(t_stack **list, t_list *solution)
|
||||||
|
{
|
||||||
|
int list_size;
|
||||||
|
int next_big;
|
||||||
|
|
||||||
|
list_size = sublist_size(*list);
|
||||||
|
if(list_size == 1)
|
||||||
|
return ;
|
||||||
|
(*list)->limit = 0;
|
||||||
|
next_big = find_biggest(*list, list_size);
|
||||||
|
push_biggest(list, solution, list_size, next_big);
|
||||||
|
(*list)->limit = 1;
|
||||||
|
mark_step(solution, "minisort");
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
t_stack *test;
|
||||||
|
test = *list;
|
||||||
|
int w;
|
||||||
|
w = list_size;
|
||||||
|
while (w--)
|
||||||
|
{
|
||||||
|
ft_printf("%i ", test->n);
|
||||||
|
test = test->next;
|
||||||
|
}
|
||||||
|
ft_printf("%i\n", next_big);
|
||||||
|
*/
|
||||||
|
|
||||||
63
srcs/minisort_2ways_bubble_sort.c
Normal file
63
srcs/minisort_2ways_bubble_sort.c
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
void mark_sublist(t_stack *list)
|
||||||
|
{
|
||||||
|
while (list && list->limit != 1)
|
||||||
|
{
|
||||||
|
if (list->limit == 2)
|
||||||
|
list->limit = 0;
|
||||||
|
else if (list->limit == 0)
|
||||||
|
list->limit = 2;
|
||||||
|
list = list->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int find_smallest_bubble(t_stack *list)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
int smallest;
|
||||||
|
int position;
|
||||||
|
|
||||||
|
smallest = list->n;
|
||||||
|
position = 1;
|
||||||
|
i = 0;
|
||||||
|
while (list)
|
||||||
|
{
|
||||||
|
i++;
|
||||||
|
if (list->limit == 2 && list->n < smallest)
|
||||||
|
{
|
||||||
|
smallest = list->n;
|
||||||
|
position = i;
|
||||||
|
}
|
||||||
|
list = list->next;
|
||||||
|
}
|
||||||
|
if (i - position + 1 < position)
|
||||||
|
position = position - i - 1;
|
||||||
|
return (position);
|
||||||
|
}
|
||||||
|
|
||||||
|
void bubble_sort(t_stack **a, t_stack **b, t_list *solution)
|
||||||
|
{
|
||||||
|
int list_size;
|
||||||
|
int next;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
list_size = sublist_size(*a);
|
||||||
|
mark_sublist(*a);
|
||||||
|
while (--list_size >= 0)
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
next = find_smallest_bubble(*a);
|
||||||
|
if (next > 0)
|
||||||
|
while (++i < next)
|
||||||
|
ra(a, &solution);
|
||||||
|
else if (next < 0)
|
||||||
|
while (i-- > next)
|
||||||
|
rra(a, &solution);
|
||||||
|
pb(b, a, &solution);
|
||||||
|
}
|
||||||
|
list_size = sublist_size(*b);
|
||||||
|
while (list_size-- > 0)
|
||||||
|
pa(a, b, &solution);
|
||||||
|
mark_sublist(*a);
|
||||||
|
}
|
||||||
632
srcs/minisort_by_rank.c
Normal file
632
srcs/minisort_by_rank.c
Normal file
@@ -0,0 +1,632 @@
|
|||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
int find_rank(t_stack *list, int nbr, int i)
|
||||||
|
{
|
||||||
|
t_stack *head;
|
||||||
|
int comp;
|
||||||
|
int rank;
|
||||||
|
int j;
|
||||||
|
|
||||||
|
head = list;
|
||||||
|
j = 0;
|
||||||
|
while (++j <= nbr - i)
|
||||||
|
head = head->next;
|
||||||
|
comp = head->n;
|
||||||
|
head = list;
|
||||||
|
j = 0;
|
||||||
|
rank = 1;
|
||||||
|
while (++j <= nbr)
|
||||||
|
{
|
||||||
|
if (comp > head->n)
|
||||||
|
rank++;
|
||||||
|
head = head->next;
|
||||||
|
}
|
||||||
|
return (rank);
|
||||||
|
}
|
||||||
|
|
||||||
|
int order_is(t_stack *list, int nbr)
|
||||||
|
{
|
||||||
|
int order;
|
||||||
|
int rank;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
order = 0;
|
||||||
|
while (++i <= nbr)
|
||||||
|
{
|
||||||
|
rank = find_rank(list, nbr, i);
|
||||||
|
order = order * 10 + rank;
|
||||||
|
}
|
||||||
|
return (order);
|
||||||
|
}
|
||||||
|
|
||||||
|
// |12345 |....5
|
||||||
|
// s |12354 |...5.
|
||||||
|
// r 4|1235 .|...5
|
||||||
|
// s 4|1253 .|..5.
|
||||||
|
// r 34|125 ..|..5
|
||||||
|
// s 34|152 ..|.5.
|
||||||
|
// r 34|15 ...|.5
|
||||||
|
// s 34|51 ...|5.
|
||||||
|
//
|
||||||
|
// rr 34|512 ..|5..
|
||||||
|
// rr 4|5123 .|5...
|
||||||
|
// rr |51234 |5....
|
||||||
|
//
|
||||||
|
// s |51243 |.1243
|
||||||
|
// r 3|5124 3|.124
|
||||||
|
// s 3|5142 3|.142
|
||||||
|
// r 23|514 23|.14
|
||||||
|
// s 23|541 23|.41
|
||||||
|
// rr 3|5412 3|.412
|
||||||
|
// rr |54123 |.4123
|
||||||
|
// s |54132 |.4132
|
||||||
|
// r 2|5413 2|.413
|
||||||
|
// s 2|5431 2|.431
|
||||||
|
// rr |54312 |.4312
|
||||||
|
// s |54321 |.4321
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// s |1243 |..4.
|
||||||
|
// r 3|124 .|..4
|
||||||
|
// s 3|142 .|.4.
|
||||||
|
// r 23|14 ..|.4
|
||||||
|
// s 23|41 ..|4.
|
||||||
|
// rr 3|412 .|4..
|
||||||
|
// rr |4123 |4...
|
||||||
|
//
|
||||||
|
// s |4132 |.132
|
||||||
|
// r 2|413 2|.13
|
||||||
|
// s 2|431 2|.31
|
||||||
|
// rr |4312 |.312
|
||||||
|
// s |4321 |.321
|
||||||
|
//
|
||||||
|
void sort_5(t_stack **a, t_list *solution)
|
||||||
|
{
|
||||||
|
int order;
|
||||||
|
|
||||||
|
// algos de 5 uniquement necessaires pour trier cette liste :
|
||||||
|
// 5 10 2 7 65 -12 -3 6 12 28 17 13 54 83 20 11 34 21 67 48
|
||||||
|
// 48 67 83 54 65 | 21 34 20 17 28 | 11 13 12 7 10 | 6 -3 -12 2 5
|
||||||
|
// 1 4 5 2 3 | 3 5 2 1 4 | 3 4 5 1 2 | 5 2 1 3 4
|
||||||
|
|
||||||
|
order = order_is(*a, 5);
|
||||||
|
if (order == 12345)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
else if (order == 12354)
|
||||||
|
else if (order == 12435)
|
||||||
|
else if (order == 12453)
|
||||||
|
else if (order == 12534)
|
||||||
|
else if (order == 12543)
|
||||||
|
|
||||||
|
else if (order == 13245)
|
||||||
|
else if (order == 13254)
|
||||||
|
else if (order == 13425)
|
||||||
|
else if (order == 13452)
|
||||||
|
else if (order == 13524)
|
||||||
|
else if (order == 13542)
|
||||||
|
|
||||||
|
else if (order == 14235)
|
||||||
|
*/
|
||||||
|
else if (order == 14253)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
else if (order == 14325)
|
||||||
|
else if (order == 14352)
|
||||||
|
else if (order == 14523)
|
||||||
|
else if (order == 14532)
|
||||||
|
|
||||||
|
else if (order == 15234)
|
||||||
|
else if (order == 15243)
|
||||||
|
else if (order == 15324)
|
||||||
|
else if (order == 15342)
|
||||||
|
else if (order == 15423)
|
||||||
|
else if (order == 15432)
|
||||||
|
|
||||||
|
|
||||||
|
else if (order == 21345)
|
||||||
|
else if (order == 21354)
|
||||||
|
else if (order == 21435)
|
||||||
|
else if (order == 21453)
|
||||||
|
else if (order == 21534)
|
||||||
|
else if (order == 21543)
|
||||||
|
|
||||||
|
else if (order == 23145)
|
||||||
|
else if (order == 23154)
|
||||||
|
else if (order == 23415)
|
||||||
|
else if (order == 23451)
|
||||||
|
else if (order == 23514)
|
||||||
|
else if (order == 23541)
|
||||||
|
|
||||||
|
else if (order == 24135)
|
||||||
|
else if (order == 24153)
|
||||||
|
else if (order == 24315)
|
||||||
|
else if (order == 24351)
|
||||||
|
else if (order == 24513)
|
||||||
|
else if (order == 24531)
|
||||||
|
|
||||||
|
else if (order == 25134)
|
||||||
|
else if (order == 25143)
|
||||||
|
else if (order == 25314)
|
||||||
|
else if (order == 25341)
|
||||||
|
else if (order == 25413)
|
||||||
|
else if (order == 25431)
|
||||||
|
|
||||||
|
|
||||||
|
else if (order == 31245)
|
||||||
|
else if (order == 31254)
|
||||||
|
else if (order == 31425)
|
||||||
|
else if (order == 31452)
|
||||||
|
else if (order == 31524)
|
||||||
|
else if (order == 31542)
|
||||||
|
|
||||||
|
else if (order == 32145)
|
||||||
|
else if (order == 32154)
|
||||||
|
else if (order == 32415)
|
||||||
|
else if (order == 32451)
|
||||||
|
else if (order == 32514)
|
||||||
|
else if (order == 32541)
|
||||||
|
|
||||||
|
else if (order == 34125)
|
||||||
|
else if (order == 34152)
|
||||||
|
else if (order == 34215)
|
||||||
|
else if (order == 34251)
|
||||||
|
*/
|
||||||
|
else if (order == 34512)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
else if (order == 34521)
|
||||||
|
|
||||||
|
else if (order == 35124)
|
||||||
|
else if (order == 35142)
|
||||||
|
*/
|
||||||
|
else if (order == 35214)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
else if (order == 35241)
|
||||||
|
else if (order == 35412)
|
||||||
|
else if (order == 35421)
|
||||||
|
|
||||||
|
|
||||||
|
else if (order == 41235)
|
||||||
|
else if (order == 41253)
|
||||||
|
else if (order == 41325)
|
||||||
|
else if (order == 41352)
|
||||||
|
else if (order == 41523)
|
||||||
|
else if (order == 41532)
|
||||||
|
|
||||||
|
else if (order == 42135)
|
||||||
|
else if (order == 42153)
|
||||||
|
else if (order == 42315)
|
||||||
|
else if (order == 42351)
|
||||||
|
else if (order == 42513)
|
||||||
|
else if (order == 42531)
|
||||||
|
|
||||||
|
else if (order == 43125)
|
||||||
|
else if (order == 43152)
|
||||||
|
else if (order == 43215)
|
||||||
|
else if (order == 43251)
|
||||||
|
else if (order == 43512)
|
||||||
|
else if (order == 43521)
|
||||||
|
|
||||||
|
else if (order == 45123)
|
||||||
|
else if (order == 45132)
|
||||||
|
else if (order == 45213)
|
||||||
|
else if (order == 45231)
|
||||||
|
else if (order == 45312)
|
||||||
|
else if (order == 45321)
|
||||||
|
|
||||||
|
|
||||||
|
else if (order == 51234)
|
||||||
|
else if (order == 51243)
|
||||||
|
else if (order == 51324)
|
||||||
|
else if (order == 51342)
|
||||||
|
else if (order == 51423)
|
||||||
|
else if (order == 51432)
|
||||||
|
*/
|
||||||
|
|
||||||
|
else if (order == 52134)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
else if (order == 52143)
|
||||||
|
else if (order == 52314)
|
||||||
|
else if (order == 52341)
|
||||||
|
else if (order == 52413)
|
||||||
|
else if (order == 52431)
|
||||||
|
|
||||||
|
else if (order == 53124)
|
||||||
|
else if (order == 53142)
|
||||||
|
else if (order == 53214)
|
||||||
|
else if (order == 53241)
|
||||||
|
else if (order == 53412)
|
||||||
|
else if (order == 53421)
|
||||||
|
|
||||||
|
else if (order == 54123)
|
||||||
|
else if (order == 54132)
|
||||||
|
else if (order == 54213)
|
||||||
|
else if (order == 54231)
|
||||||
|
else if (order == 54312)
|
||||||
|
else if (order == 54321)
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
|
void sort_4(t_stack **a, t_list *solution)
|
||||||
|
{
|
||||||
|
int order;
|
||||||
|
|
||||||
|
order = order_is(*a, 4);
|
||||||
|
if (order == 1234)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 1243)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 1324)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 1342)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 1423)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 1432)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 2134)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 2143)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 2314)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 2341)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 2413)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 2431)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 3124)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 3142)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 3214)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 3241)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 3412)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 3421)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 4123)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 4132)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 4213)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 4231)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 4312)
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sort_3(t_stack **a, t_list *solution)
|
||||||
|
{
|
||||||
|
int order;
|
||||||
|
|
||||||
|
order = order_is(*a, 3);
|
||||||
|
if (order == 123)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 132)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 213)
|
||||||
|
{
|
||||||
|
sa(a, &solution);
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 231)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
rra(a, &solution);
|
||||||
|
}
|
||||||
|
else if (order == 312)
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sort_2(t_stack **a, t_list *solution)
|
||||||
|
{
|
||||||
|
int order;
|
||||||
|
|
||||||
|
order = order_is(*a, 2);
|
||||||
|
if (order == 12)
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mini_sort(t_stack **a, t_list *solution)
|
||||||
|
{
|
||||||
|
int list_size;
|
||||||
|
|
||||||
|
list_size = sublist_size(*a);
|
||||||
|
if(list_size == 1)
|
||||||
|
return ;
|
||||||
|
(*a)->limit = 0;
|
||||||
|
if(list_size == 2)
|
||||||
|
sort_2(a, solution);
|
||||||
|
else if(list_size == 3)
|
||||||
|
sort_3(a, solution);
|
||||||
|
else if(list_size == 4)
|
||||||
|
sort_4(a, solution);
|
||||||
|
else if(list_size == 5)
|
||||||
|
sort_5(a, solution);
|
||||||
|
/*
|
||||||
|
else if(list_size == 6)
|
||||||
|
sort_6(a, solution);
|
||||||
|
else
|
||||||
|
sort_big(a, b, solution);
|
||||||
|
*/
|
||||||
|
(*a)->limit = 1;
|
||||||
|
mark_step(solution, "mini_sort");
|
||||||
|
}
|
||||||
107
srcs/print.c
107
srcs/print.c
@@ -1,16 +1,49 @@
|
|||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
char *fill_line(t_stack *list, char *stack)
|
||||||
|
{
|
||||||
|
while (list != NULL)
|
||||||
|
{
|
||||||
|
if (list->limit == 1)
|
||||||
|
stack = ft_strjoinfree(ft_strdup("]"), stack);
|
||||||
|
stack = ft_strjoinfree(ft_itoa(list->n), stack);
|
||||||
|
stack = ft_strjoinfree(ft_strdup(" "), stack);
|
||||||
|
list = list->next;
|
||||||
|
}
|
||||||
|
return (stack);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mark_step(t_list *solution, char *step)
|
||||||
|
{
|
||||||
|
char *line;
|
||||||
|
|
||||||
|
while (solution->next != NULL)
|
||||||
|
solution = solution->next;
|
||||||
|
line = solution->content;
|
||||||
|
line = ft_strjoinfree(line, ft_strdup("!"));
|
||||||
|
line = ft_strjoinfree(line, ft_strdup(step));
|
||||||
|
solution->content = line;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** this function is called by actions like sa()
|
** this function is called by actions like sa()
|
||||||
** it fills a new element on the chained list like this :
|
** it fills a new str element on the chained list like this :
|
||||||
** name!a: nb nb nb nb nb!b: nb nb nb
|
|
||||||
**
|
**
|
||||||
** it will later be printed like this ("!" is the char to split) :
|
** b: nbb3 nbb2 nbb1!a: nba5 nba4 nba3 nba2 nba1!name
|
||||||
** name
|
** (it fills it by the end, "!name" first, then "nba1" on its back, and so on)
|
||||||
** a: nb nb nb nb nb
|
|
||||||
** b: nb nb nb
|
|
||||||
**
|
**
|
||||||
** or only "name" if no flag -p
|
** so it can later be printed like that (in case of flag -p) :
|
||||||
|
** the "!" split the str :
|
||||||
|
**
|
||||||
|
** [0]b: nbb3 nbb2 nbb1 [1]a: nba5 nba4 nba3 nba2 nba1 [2]name
|
||||||
|
**
|
||||||
|
** to produce :
|
||||||
|
**
|
||||||
|
** [2]name
|
||||||
|
** [1] a: nba5 nba4 nba3 nba2 nba1
|
||||||
|
** [0] b: nbb3 nbb2 nbb1
|
||||||
|
**
|
||||||
|
** (if no flag -p, only [2]name is displayed)
|
||||||
*/
|
*/
|
||||||
void fill_solution(t_list *solution, char *sp)
|
void fill_solution(t_list *solution, char *sp)
|
||||||
{
|
{
|
||||||
@@ -20,41 +53,53 @@ void fill_solution(t_list *solution, char *sp)
|
|||||||
|
|
||||||
a = *(t_stack **)(solution->content);
|
a = *(t_stack **)(solution->content);
|
||||||
b = *(t_stack **)(solution->next->content);
|
b = *(t_stack **)(solution->next->content);
|
||||||
stack = ft_strjoinfree(ft_strdup(sp), ft_strdup("!a:"));
|
stack = ft_strjoinfree(ft_strdup("!"), ft_strdup(sp));
|
||||||
while (a != NULL)
|
stack = fill_line(a, stack);
|
||||||
{
|
stack = ft_strjoinfree(ft_strdup("!a:"), stack);
|
||||||
stack = ft_strjoinfree(stack, ft_strdup(" "));
|
stack = fill_line(b, stack);
|
||||||
stack = ft_strjoinfree(stack, ft_itoa(a->n));
|
stack = ft_strjoinfree(ft_strdup("b:"), stack);
|
||||||
a = a->next;
|
|
||||||
}
|
|
||||||
stack = ft_strjoinfree(stack, ft_strdup("!b:"));
|
|
||||||
while (b != NULL)
|
|
||||||
{
|
|
||||||
stack = ft_strjoinfree(stack, ft_strdup(" "));
|
|
||||||
stack = ft_strjoinfree(stack, ft_itoa(b->n));
|
|
||||||
b = b->next;
|
|
||||||
}
|
|
||||||
ft_lstadd_back(&solution, ft_lstnew(stack));
|
ft_lstadd_back(&solution, ft_lstnew(stack));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// some drafty code to print sublist with flag
|
||||||
|
void print_flags(char **part, int flag)
|
||||||
|
{
|
||||||
|
int last_a;
|
||||||
|
int last_b;
|
||||||
|
|
||||||
|
|
||||||
|
last_a = part[1][ft_strlen(part[1]) - 1];
|
||||||
|
last_b = part[0][ft_strlen(part[0]) - 1];
|
||||||
|
if (flag && part[3] != NULL)
|
||||||
|
ft_printf(" ( %s )", part[3]);
|
||||||
|
if (flag == 1 || (flag == 2 && part[3] != NULL))
|
||||||
|
{
|
||||||
|
ft_printf("\n %s", part[1]);
|
||||||
|
if (part[3] != NULL && last_a != ']')
|
||||||
|
ft_putchar(']');
|
||||||
|
ft_printf("\n %s", part[0]);
|
||||||
|
if (part[3] != NULL && last_b != ']' && last_b != ':')
|
||||||
|
ft_putchar(']');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void print_result(t_list *result, int flag)
|
void print_result(t_list *result, int flag)
|
||||||
{
|
{
|
||||||
|
int i;
|
||||||
char **part;
|
char **part;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
result = result->next->next;
|
result = result->next->next;
|
||||||
while (result)
|
while (result)
|
||||||
{
|
{
|
||||||
|
i++;
|
||||||
part = ft_split(result->content, '!');
|
part = ft_split(result->content, '!');
|
||||||
ft_printf("%s\n", part[0]);
|
ft_printf("%s", part[2]);
|
||||||
if (flag)
|
print_flags(part, flag);
|
||||||
{
|
ft_putchar('\n');
|
||||||
ft_printf(" %s\n", part[1]);
|
|
||||||
ft_printf(" %s\n", part[2]);
|
|
||||||
}
|
|
||||||
result = result->next;
|
result = result->next;
|
||||||
free(part[0]);
|
ft_free_tab(part);
|
||||||
free(part[1]);
|
|
||||||
free(part[2]);
|
|
||||||
free(part);
|
|
||||||
}
|
}
|
||||||
|
if (flag)
|
||||||
|
ft_putnbrendl(i);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,13 +5,14 @@ void push(t_stack **dst, t_stack **src)
|
|||||||
t_stack *tmp1;
|
t_stack *tmp1;
|
||||||
t_stack *tmp2;
|
t_stack *tmp2;
|
||||||
|
|
||||||
if (!((*src)->next))
|
if (*src)
|
||||||
return ;
|
{
|
||||||
tmp1 = *src;
|
tmp1 = *src;
|
||||||
tmp2 = *dst;
|
tmp2 = *dst;
|
||||||
*src = (*src)->next;
|
*src = (*src)->next;
|
||||||
tmp1->next = tmp2;
|
tmp1->next = tmp2;
|
||||||
*dst = tmp1;
|
*dst = tmp1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t_list *pa(t_stack **a, t_stack **b, t_list **solution)
|
t_list *pa(t_stack **a, t_stack **b, t_list **solution)
|
||||||
|
|||||||
128
srcs/push_swap.c
128
srcs/push_swap.c
@@ -1,42 +1,114 @@
|
|||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
|
||||||
// check more error
|
int is_ordered(t_stack *list)
|
||||||
void is_valid(int ac, char **av)
|
|
||||||
{
|
{
|
||||||
if (ac < 2)
|
int cmp;
|
||||||
ps_stop(NULL, NULL, 1);
|
|
||||||
(void)av;
|
while (list && list->next)
|
||||||
|
{
|
||||||
|
cmp = list->n;
|
||||||
|
list = list->next;
|
||||||
|
if (list->n < cmp)
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int has_doubles(t_stack *list)
|
||||||
|
{
|
||||||
|
t_stack *head;
|
||||||
|
int nbr;
|
||||||
|
|
||||||
|
while (list)
|
||||||
|
{
|
||||||
|
head = list;
|
||||||
|
nbr = head->n;
|
||||||
|
while(head && head->next)
|
||||||
|
{
|
||||||
|
head = head->next;
|
||||||
|
if (head->n == nbr)
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
list = list->next;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_valid(t_stack *list)
|
||||||
|
{
|
||||||
|
if (!list)
|
||||||
|
return (0);
|
||||||
|
if (has_doubles(list) == 1)
|
||||||
|
ps_stop(NULL, (t_list *)list, 3);
|
||||||
|
if (is_ordered(list) == 1)
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
int check_flag(int *ac, char ***av)
|
int check_flag(int *ac, char ***av)
|
||||||
{
|
{
|
||||||
if (ft_strcmp((*av)[1], "-p") != 0)
|
if (ft_strcmp((*av)[1], "-p") == 0)
|
||||||
return (0);
|
{
|
||||||
(*av)++;
|
(*av)++;
|
||||||
(*ac)--;
|
(*ac)--;
|
||||||
return (1);
|
return (1);
|
||||||
|
}
|
||||||
|
if (ft_strcmp((*av)[1], "-P") == 0)
|
||||||
|
{
|
||||||
|
(*av)++;
|
||||||
|
(*ac)--;
|
||||||
|
return (2);
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_stack *fill_stack(t_stack *list, char *nb, char **tab)
|
||||||
|
{
|
||||||
|
t_stack *new;
|
||||||
|
long nbl;
|
||||||
|
|
||||||
|
new = ft_calloc(1, sizeof(t_stack));
|
||||||
|
if (!new)
|
||||||
|
ps_stop(tab, (t_list *)list, 3);
|
||||||
|
new->next = list;
|
||||||
|
list = new;
|
||||||
|
if (ft_isnumber(nb) == 0)
|
||||||
|
ps_stop(tab, (t_list *)list, 3);
|
||||||
|
nbl = ft_atol(nb);
|
||||||
|
if (nbl < INT_MIN || nbl > INT_MAX)
|
||||||
|
ps_stop(tab, (t_list *)list, 3);
|
||||||
|
list->n = nbl;
|
||||||
|
list->limit = 0;
|
||||||
|
return (list);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_stack *init_stack(int ac, char **av)
|
t_stack *init_stack(int ac, char **av)
|
||||||
{
|
{
|
||||||
t_stack *start;
|
char **split;
|
||||||
t_stack *tmp;
|
t_stack *list;
|
||||||
|
int i;
|
||||||
|
|
||||||
tmp = NULL;
|
list = NULL;
|
||||||
|
if (ac > 2)
|
||||||
while (--ac)
|
while (--ac)
|
||||||
|
list = fill_stack(list, av[ac], av);
|
||||||
|
else if (ac == 2)
|
||||||
{
|
{
|
||||||
start = ft_calloc(1, sizeof(t_stack));
|
split = ft_split(av[1], ' ');
|
||||||
if (!start)
|
i = 0;
|
||||||
ps_stop(NULL, NULL, 2);
|
while (split[i] != NULL)
|
||||||
start->n = ft_atoi(av[ac]);
|
i++;
|
||||||
start->next = tmp;
|
while (--i >= 0)
|
||||||
tmp = start;
|
list = fill_stack(list, split[i], split);
|
||||||
|
ft_free_tab(split);
|
||||||
}
|
}
|
||||||
return (start);
|
return (list);
|
||||||
}
|
}
|
||||||
|
|
||||||
// the chained list "solution" is created with specials first two elements :
|
/*
|
||||||
// they are pointers to stack a and b, so it can be accessed by fill_solution()
|
** the chained list "solution" is created with specials first two elements :
|
||||||
|
** they are pointers to stack a and b, so it can be accessed by fill_solution()
|
||||||
|
*/
|
||||||
t_list *launch_algo(t_stack **a, t_stack **b, int flag)
|
t_list *launch_algo(t_stack **a, t_stack **b, int flag)
|
||||||
{
|
{
|
||||||
t_list *solution;
|
t_list *solution;
|
||||||
@@ -44,11 +116,19 @@ t_list *launch_algo(t_stack **a, t_stack **b, int flag)
|
|||||||
solution = ft_lstnew(a);
|
solution = ft_lstnew(a);
|
||||||
ft_lstadd_back(&solution, ft_lstnew(b));
|
ft_lstadd_back(&solution, ft_lstnew(b));
|
||||||
if (flag)
|
if (flag)
|
||||||
|
{
|
||||||
fill_solution(solution, "start");
|
fill_solution(solution, "start");
|
||||||
|
mark_step(solution, "start");
|
||||||
|
}
|
||||||
|
if (sublist_size(*a) <= 3)
|
||||||
|
special_sort_3(a, solution);
|
||||||
|
else if (sublist_size(*a) <= 5)
|
||||||
|
special_sort_5(a, b, solution);
|
||||||
|
else
|
||||||
hugo_sort(a, b, solution);
|
hugo_sort(a, b, solution);
|
||||||
|
// bubble_sort_luke(a, b, solution);
|
||||||
return (solution);
|
return (solution);
|
||||||
}
|
}
|
||||||
// bubble_sort(&a, &b, solution);
|
|
||||||
|
|
||||||
int main(int ac, char **av)
|
int main(int ac, char **av)
|
||||||
{
|
{
|
||||||
@@ -57,12 +137,18 @@ int main(int ac, char **av)
|
|||||||
t_list *solution;
|
t_list *solution;
|
||||||
int flag;
|
int flag;
|
||||||
|
|
||||||
is_valid(ac, av);
|
if (ac < 2)
|
||||||
|
ps_stop(NULL, NULL, 1);
|
||||||
flag = check_flag(&ac, &av);
|
flag = check_flag(&ac, &av);
|
||||||
a = init_stack(ac, av);
|
a = init_stack(ac, av);
|
||||||
b = NULL;
|
b = NULL;
|
||||||
|
if (check_valid(a) == 1)
|
||||||
|
{
|
||||||
solution = launch_algo(&a, &b, flag);
|
solution = launch_algo(&a, &b, flag);
|
||||||
print_result(solution, flag);
|
print_result(solution, flag);
|
||||||
ps_stop(NULL, solution, 0);
|
ps_stop(NULL, solution, 0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
ps_stop(NULL, (t_list *)a, -1);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ void reverse_rotate(t_stack **stack)
|
|||||||
t_stack *tmp;
|
t_stack *tmp;
|
||||||
t_stack *before;
|
t_stack *before;
|
||||||
|
|
||||||
|
if (*stack && (*stack)->next)
|
||||||
|
{
|
||||||
tmp = *stack;
|
tmp = *stack;
|
||||||
if (!tmp || !(tmp->next))
|
|
||||||
return ;
|
|
||||||
while (tmp->next)
|
while (tmp->next)
|
||||||
{
|
{
|
||||||
before = tmp;
|
before = tmp;
|
||||||
@@ -16,6 +16,7 @@ void reverse_rotate(t_stack **stack)
|
|||||||
tmp->next = *stack;
|
tmp->next = *stack;
|
||||||
*stack = tmp;
|
*stack = tmp;
|
||||||
before->next = NULL;
|
before->next = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t_list *rra(t_stack **a, t_list **lst)
|
t_list *rra(t_stack **a, t_list **lst)
|
||||||
|
|||||||
@@ -5,15 +5,16 @@ void rotate(t_stack **stack)
|
|||||||
t_stack *tmp;
|
t_stack *tmp;
|
||||||
t_stack *first;
|
t_stack *first;
|
||||||
|
|
||||||
|
if (*stack && (*stack)->next)
|
||||||
|
{
|
||||||
first = *stack;
|
first = *stack;
|
||||||
tmp = *stack;
|
tmp = *stack;
|
||||||
if (!tmp || !(tmp->next))
|
|
||||||
return ;
|
|
||||||
*stack = (*stack)->next;
|
*stack = (*stack)->next;
|
||||||
while (tmp->next)
|
while (tmp->next)
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
tmp->next = first;
|
tmp->next = first;
|
||||||
first->next = NULL;
|
first->next = NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t_list *ra(t_stack **a, t_list **lst)
|
t_list *ra(t_stack **a, t_list **lst)
|
||||||
|
|||||||
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);
|
||||||
|
}
|
||||||
|
|
||||||
56
srcs/special_sorts_3_5.c
Normal file
56
srcs/special_sorts_3_5.c
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
#include "push_swap.h"
|
||||||
|
|
||||||
|
void special_sort_3(t_stack **a, t_list *solution)
|
||||||
|
{
|
||||||
|
int order;
|
||||||
|
int size;
|
||||||
|
|
||||||
|
size = sublist_size(*a);
|
||||||
|
order = order_is(*a, size);
|
||||||
|
if (order == 12)
|
||||||
|
sa(a, &solution);
|
||||||
|
if (order == 123)
|
||||||
|
{
|
||||||
|
ra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
if (order == 132)
|
||||||
|
rra(a, &solution);
|
||||||
|
if (order == 213)
|
||||||
|
ra(a, &solution);
|
||||||
|
if (order == 231)
|
||||||
|
{
|
||||||
|
rra(a, &solution);
|
||||||
|
sa(a, &solution);
|
||||||
|
}
|
||||||
|
if (order == 312)
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
42
srcs/stop.c
42
srcs/stop.c
@@ -1,28 +1,20 @@
|
|||||||
#include "push_swap.h"
|
#include "push_swap.h"
|
||||||
|
|
||||||
void ps_usage(void)
|
/*
|
||||||
{
|
** if err = 0, the list are freed but the program doesn't stop
|
||||||
ft_printf("usage :\n");
|
** if err = 1, 2 or 3, a message is printed and the program stop
|
||||||
ft_printf("launch executable : ./push_swap [flag -p] nb nb nb nb nb ...\n");
|
** if err = 3 ou -1, the parameter list is just a t_stack *list
|
||||||
}
|
*/
|
||||||
|
void ps_stop(char **tab, t_list *lst, int err)
|
||||||
void ps_error(int err)
|
|
||||||
{
|
|
||||||
if (err == 1)
|
|
||||||
ps_usage();
|
|
||||||
if (err == 2)
|
|
||||||
ft_printf("error\n");
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ps_stop(t_stack *stack, t_list *lst, int err)
|
|
||||||
{
|
{
|
||||||
t_stack **a;
|
t_stack **a;
|
||||||
t_stack **b;
|
t_stack **b;
|
||||||
|
|
||||||
if (stack)
|
if (tab != NULL)
|
||||||
ft_lstclear((t_list **)&stack, NULL);
|
ft_free_tab(tab);
|
||||||
if (lst)
|
if (err == 3 || err == -1)
|
||||||
|
ft_lstclear(&lst, NULL);
|
||||||
|
else if (lst)
|
||||||
{
|
{
|
||||||
a = lst->content;
|
a = lst->content;
|
||||||
b = lst->next->content;
|
b = lst->next->content;
|
||||||
@@ -32,8 +24,12 @@ void ps_stop(t_stack *stack, t_list *lst, int err)
|
|||||||
lst->next->content = NULL;
|
lst->next->content = NULL;
|
||||||
ft_lstclear(&lst, free);
|
ft_lstclear(&lst, free);
|
||||||
}
|
}
|
||||||
if (err < 0)
|
if (err > 0)
|
||||||
return ;
|
{
|
||||||
else if (err > 0)
|
if (err == 1)
|
||||||
ps_error(err);
|
ft_printf("usage :\nlaunch executable : ./push_swap [flag -p] nb nb nb nb nb ...\n");
|
||||||
|
if (err >= 2)
|
||||||
|
ft_putstr_fd("Error\n", 2);
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,12 +4,13 @@ void swap(t_stack **stack)
|
|||||||
{
|
{
|
||||||
t_stack *tmp;
|
t_stack *tmp;
|
||||||
|
|
||||||
|
if (*stack && (*stack)->next)
|
||||||
|
{
|
||||||
tmp = *stack;
|
tmp = *stack;
|
||||||
if (!(tmp->next))
|
|
||||||
return ;
|
|
||||||
*stack = (*stack)->next;
|
*stack = (*stack)->next;
|
||||||
tmp->next = (*stack)->next;
|
tmp->next = (*stack)->next;
|
||||||
(*stack)->next = tmp;
|
(*stack)->next = tmp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t_list *sa(t_stack **a, t_list **solution)
|
t_list *sa(t_stack **a, t_list **solution)
|
||||||
|
|||||||
51
tester.sh
Normal file
51
tester.sh
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# colors variables
|
||||||
|
GREY='\033[1;30m'
|
||||||
|
RED='\033[1;31m'
|
||||||
|
GREEN='\033[1;32m'
|
||||||
|
ORANGE='\033[1;33m'
|
||||||
|
BLUE='\033[1;34m'
|
||||||
|
PURPLE='\033[1;35m'
|
||||||
|
LIGHTBLUE='\033[1;36m'
|
||||||
|
WHITE='\033[1;37m'
|
||||||
|
NC='\033[0m'
|
||||||
|
|
||||||
|
# usage of arguments
|
||||||
|
if [ $# -eq 1 ]
|
||||||
|
then
|
||||||
|
SIZE=$1 # size of the list
|
||||||
|
elif [ $# -eq 2 ]
|
||||||
|
then
|
||||||
|
FLAG=$1 # flag for leaks and errors // not in use so far
|
||||||
|
SIZE=$2
|
||||||
|
else
|
||||||
|
echo "usage : ./tester.sh path/to/push_swap size_of_list"
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
PRGM="push_swap" # path to program
|
||||||
|
# range of random numbers
|
||||||
|
RANGE=$(($SIZE * 2))
|
||||||
|
|
||||||
|
# generate the list of randoms differents numbers
|
||||||
|
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[@]})
|
||||||
|
COUNT=$(echo "$OUTPUT" | wc -l)
|
||||||
|
RESULT=$(echo "$OUTPUT" | ./checker ${LIST[@]})
|
||||||
|
|
||||||
|
if [[ $RESULT == "OK" ]]
|
||||||
|
then
|
||||||
|
COLOR=$GREEN
|
||||||
|
ANNONCE="réussi !"
|
||||||
|
else
|
||||||
|
COLOR=$RED
|
||||||
|
ANNONCE="raté :("
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo -en "${WHITE}le test est ${NC}${COLOR}${ANNONCE}${NC}${WHITE} et a été "
|
||||||
|
echo -e "effectué en ${NC}${ORANGE}${COUNT}${NC}${WHITE} opérations${NC}"
|
||||||
|
|
||||||
Reference in New Issue
Block a user