better insert sort
This commit is contained in:
parent
a571372cbf
commit
246376f6df
2 changed files with 15 additions and 28 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2016/11/07 13:27:46 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);
|
t_list *ft_lst_at(t_list *list, unsigned int nbr);
|
||||||
|
|
||||||
void lst_insert_sort(t_list **head,
|
void lst_insert_sort(t_list **head,
|
||||||
int (cmp)(void *elem1, void *elem2));
|
int (cmp)());
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -1,42 +1,29 @@
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* lst_inser_sort.c :+: :+: :+: */
|
/* lst_insert_sort.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/05/19 17:59:14 by ariard #+# #+# */
|
/* Created: 2017/05/20 19:59:41 by ariard #+# #+# */
|
||||||
/* Updated: 2017/05/19 18:56:53 by ariard ### ########.fr */
|
/* Updated: 2017/05/20 20:05:32 by ariard ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "libft.h"
|
#include "libft.h"
|
||||||
|
|
||||||
void lst_insert_sort(t_list **head,
|
void lst_insert_sort(t_list **head,
|
||||||
int (cmp)(void *elem1, void *elem2))
|
int (cmp)())
|
||||||
{
|
{
|
||||||
t_list **indirect;
|
t_list *new;
|
||||||
t_list *ins;
|
t_list *ptr;
|
||||||
t_list *tmp;
|
|
||||||
|
|
||||||
indirect = head;
|
new = NULL;
|
||||||
while (indirect && *indirect && (*indirect)->next)
|
while (*head)
|
||||||
{
|
{
|
||||||
ins = *head;
|
ptr = *head;
|
||||||
if ((cmp)(*indirect, (*indirect)->next) > 0)
|
*head = (*head)->next;
|
||||||
while (ins->next)
|
ft_lst_sorted_insert(&new, ptr, cmp);
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
*head = new;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue