aboutsummaryrefslogtreecommitdiffstats
path: root/src/common/sort.c
blob: 3e2470679d53fb76215867da610514abc7b6698c (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
/*
 * blogc: A blog compiler.
 * Copyright (C) 2014-2019 Rafael G. Martins <rafael@rafaelmartins.eng.br>
 *
 * This program can be distributed under the terms of the BSD License.
 * See the file LICENSE.
 */

#include <stdbool.h>
#include "utils.h"
#include "sort.h"


bc_slist_t*
bc_slist_sort(bc_slist_t *l, bc_sort_func_t cmp)
{
    if (l == NULL) {
        return NULL;
    }

    bool swapped = false;
    bc_slist_t *lptr = NULL;
    bc_slist_t *rptr = NULL;

    do {
        swapped = false;
        lptr = l;

        while (lptr->next != rptr) {
            if (0 < cmp(lptr->data, lptr->next->data)) {
                void *tmp = lptr->data;
                lptr->data = lptr->next->data;
                lptr->next->data = tmp;
                swapped = true;
            }

            lptr = lptr->next;
        }

        rptr = lptr;
    } while(swapped);

    return l;
}