aboutsummaryrefslogtreecommitdiff
path: root/c12/ex14/ft_list_sort.c
blob: 0d8142581ed24affaa03c8ba193a4270a25a1f00 (plain)
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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_list_sort.c                                     :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: cacharle <charles.cabergs@gmail.com>       +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2019/07/19 13:43:11 by cacharle          #+#    #+#             */
/*   Updated: 2019/07/23 15:41:20 by cacharle         ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include <stdlib.h>
#include "ft_list.h"

static int	is_sorted(t_list *cursor, int (*cmp)())
{
	if (cursor == NULL)
		return (1);
	else if (cursor->next == NULL)
		return (1);
	while (cursor->next)
	{
		if ((*cmp)(cursor->data, cursor->next->data) > 0)
			return (0);
		cursor = cursor->next;
	}
	return (1);
}

void		ft_list_sort(t_list **begin_list, int (*cmp)())
{
	t_list	*cursor;
	void	*tmp;

	while (!is_sorted(*begin_list, cmp))
	{
		cursor = *begin_list;
		while (cursor->next)
		{
			if ((*cmp)(cursor->data, cursor->next->data) > 0)
			{
				tmp = cursor->data;
				cursor->data = cursor->next->data;
				cursor->next->data = tmp;
			}
			cursor = cursor->next;
		}
	}
}