-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_lstsort.c
More file actions
63 lines (56 loc) · 1.77 KB
/
Copy pathft_lstsort.c
File metadata and controls
63 lines (56 loc) · 1.77 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_lstsort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sunakim <sunakim@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/31 22:03:21 by mnishimo #+# #+# */
/* Updated: 2019/04/07 18:52:21 by sunakim ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
static void ft_swap(void *ptr1, void *ptr2)
{
void *tmp;
tmp = ptr1;
ptr1 = ptr2;
ptr2 = tmp;
}
static t_list *insertion_swap(t_list *index, t_list **sorted)
{
void *tmp;
if (index == *sorted)
{
*sorted = (*sorted)->next;
return (*sorted);
}
tmp = (*sorted)->content;
(*sorted)->content = index->content;
index->content = tmp;
*sorted = (*sorted)->next;
return ((*sorted));
}
void ft_lstsort(t_list **alist, int (*cmp)(void *, void *))
{
t_list *index;
t_list *cur;
t_list *sorted;
if (!alist || !(*alist) || !((*alist)->next))
return ;
sorted = *alist;
cur = *alist;
if (!cur->next->next && cmp(cur->content, cur->next->content) > 0)
ft_swap(cur->content, cur->next->content);
while (cur)
{
index = sorted;
while (cur)
{
if (cmp(cur->content, index->content) < 0)
index = cur;
cur = cur->next;
}
cur = insertion_swap(index, &sorted);
}
}