From 246376f6dfb2cf15fb8611d84b1c2e83bdec27d5 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Sat, 20 May 2017 20:06:38 +0200 Subject: [PATCH] better insert sort --- libft/includes/lst.h | 4 ++-- libft/srcs/lst/lst_insert_sort.c | 39 +++++++++++--------------------- 2 files changed, 15 insertions(+), 28 deletions(-) diff --git a/libft/includes/lst.h b/libft/includes/lst.h index b1c95b82..3f4adb6a 100644 --- a/libft/includes/lst.h +++ b/libft/includes/lst.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:27:46 by jhalford #+# #+# */ -/* Updated: 2017/05/19 18:51:36 by ariard ### ########.fr */ +/* Updated: 2017/05/20 20:01:20 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -86,6 +86,6 @@ t_list *ft_id(t_list *a); t_list *ft_lst_at(t_list *list, unsigned int nbr); void lst_insert_sort(t_list **head, - int (cmp)(void *elem1, void *elem2)); + int (cmp)()); #endif diff --git a/libft/srcs/lst/lst_insert_sort.c b/libft/srcs/lst/lst_insert_sort.c index cb18d4fc..d1a62412 100644 --- a/libft/srcs/lst/lst_insert_sort.c +++ b/libft/srcs/lst/lst_insert_sort.c @@ -1,42 +1,29 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* lst_inser_sort.c :+: :+: :+: */ +/* lst_insert_sort.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2017/05/19 17:59:14 by ariard #+# #+# */ -/* Updated: 2017/05/19 18:56:53 by ariard ### ########.fr */ +/* Created: 2017/05/20 19:59:41 by ariard #+# #+# */ +/* Updated: 2017/05/20 20:05:32 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -void lst_insert_sort(t_list **head, - int (cmp)(void *elem1, void *elem2)) +void lst_insert_sort(t_list **head, + int (cmp)()) { - t_list **indirect; - t_list *ins; - t_list *tmp; + t_list *new; + t_list *ptr; - indirect = head; - while (indirect && *indirect && (*indirect)->next) + new = NULL; + while (*head) { - ins = *head; - if ((cmp)(*indirect, (*indirect)->next) > 0) - while (ins->next) - { - if ((cmp)(*indirect, ins->next) < 0) - { - tmp->next = (*indirect)->next; - (*indirect)->next = ins->next; - ins->next = (*indirect); - indirect = &tmp; - break; - } - ins = ins->next; - } - tmp = *indirect; - indirect = &(*indirect)->next; + ptr = *head; + *head = (*head)->next; + ft_lst_sorted_insert(&new, ptr, cmp); } + *head = new; }