summaryrefslogtreecommitdiff
path: root/include/al/array_sort.h
blob: 8d138d0fcb48051d4576a45b90aac14d475e43cb (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
#ifndef _AL_ARRAY_SORT_H
#define _AL_ARRAY_SORT_H

#include "lib.h"

#define AL_INSERSION_SORT(data, type, count, cmp)      \
    do {                                               \
        s32 i = 1;                                     \
        while (i < (s32)count) {                       \
            s32 j = i - 1;                             \
            type x = data[i];                          \
            while (j >= 0 && cmp(&data[j], &x) > 0) {  \
                data[j + 1] = data[j];                 \
                j--;                                   \
            }                                          \
            data[j + 1] = x;                           \
            i++;                                       \
        }                                              \
    } while (0)

#if AL_USE_STDLIB
#define AL_STDLIB_QSORT(data, type, count, cmp) qsort(data, count, sizeof(type), cmp)
#endif

#endif // _AL_ARRAY_SORT_H