-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_lstsort.c
executable file
·64 lines (59 loc) · 1.75 KB
/
ft_lstsort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/* ************************************************************************** */
/* LE - / */
/* / */
/* ft_lstsort.c .:: .:/ . .:: */
/* +:+:+ +: +: +:+:+ */
/* By: brobicho <[email protected]> +:+ +: +: +:+ */
/* #+# #+ #+ #+# */
/* Created: 2018/07/25 15:57:13 by brobicho #+# ## ## #+# */
/* Updated: 2018/07/25 15:57:16 by brobicho ### #+. /#+ ###.fr */
/* / */
/* / */
/* ************************************************************************** */
#include "libft.h"
static int get_lesser(const t_list *lst,
int (*f)(void *, size_t, void *, size_t))
{
size_t index_min;
size_t i;
const t_list *mini;
i = 0;
index_min = 0;
mini = lst;
while (lst)
{
if (f(mini->content, mini->content_size, lst->content,
lst->content_size))
{
index_min = i;
mini = lst;
}
lst = lst->next;
++i;
}
return (index_min);
}
void ft_lstsort(t_list **lst, int (*f)(void *, size_t, void *, size_t))
{
t_list *mini;
t_list *first;
t_list *tmp;
size_t index_min;
size_t i;
i = 0;
first = *lst;
while (first)
{
index_min = get_lesser(first, f);
if (index_min != 0)
{
mini = ft_lstat(first, index_min - 1);
tmp = mini->next;
mini->next = mini->next->next;
ft_lstins(lst, tmp, i);
}
else
first = first->next;
++i;
}
}