2
* Copyright (c) 2005 Sergey Bondari
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions
9
* - Redistributions of source code must retain the above copyright
10
* notice, this list of conditions and the following disclaimer.
11
* - Redistributions in binary form must reproduce the above copyright
12
* notice, this list of conditions and the following disclaimer in the
13
* documentation and/or other materials provided with the distribution.
14
* - The name of the author may not be used to endorse or promote products
15
* derived from this software without specific prior written permission.
17
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
/** @addtogroup generic
35
* @brief Sorting functions.
37
* This files contains functions implementing several sorting
38
* algorithms (e.g. quick sort and bubble sort).
48
void _qsort(void * data, size_t n, size_t e_size, int (* cmp) (void * a, void * b), void *tmp, void *pivot);
49
void _bubblesort(void * data, size_t n, size_t e_size, int (* cmp) (void * a, void * b), void *slot);
53
* This is only a wrapper that takes care of memory allocations for storing
54
* the pivot and temporary elements for generic quicksort algorithm.
56
* This function _can_ sleep
58
* @param data Pointer to data to be sorted.
59
* @param n Number of elements to be sorted.
60
* @param e_size Size of one element.
61
* @param cmp Comparator function.
64
void qsort(void * data, size_t n, size_t e_size, int (* cmp) (void * a, void * b))
66
uint8_t buf_tmp[EBUFSIZE];
67
uint8_t buf_pivot[EBUFSIZE];
69
void * pivot = buf_pivot;
71
if (e_size > EBUFSIZE) {
72
pivot = (void *) malloc(e_size, 0);
73
tmp = (void *) malloc(e_size, 0);
76
_qsort(data, n, e_size, cmp, tmp, pivot);
78
if (e_size > EBUFSIZE) {
86
* Apply generic quicksort algorithm on supplied data, using pre-allocated buffers.
88
* @param data Pointer to data to be sorted.
89
* @param n Number of elements to be sorted.
90
* @param e_size Size of one element.
91
* @param cmp Comparator function.
92
* @param tmp Pointer to scratch memory buffer e_size bytes long.
93
* @param pivot Pointer to scratch memory buffer e_size bytes long.
96
void _qsort(void * data, size_t n, size_t e_size, int (* cmp) (void * a, void * b), void *tmp, void *pivot)
99
unsigned int i = 0, j = n - 1;
101
memcpy(pivot, data, e_size);
104
while ((cmp(data + i * e_size, pivot) < 0) && (i < n))
106
while ((cmp(data + j * e_size, pivot) >= 0) && (j > 0))
110
memcpy(tmp, data + i * e_size, e_size);
111
memcpy(data + i * e_size, data + j * e_size, e_size);
112
memcpy(data + j * e_size, tmp, e_size);
118
_qsort(data, j + 1, e_size, cmp, tmp, pivot);
119
_qsort(data + (j + 1) * e_size, n - j - 1, e_size, cmp, tmp, pivot);
121
_bubblesort(data, n, e_size, cmp, tmp);
125
/** Bubblesort wrapper
127
* This is only a wrapper that takes care of memory allocation for storing
128
* the slot element for generic bubblesort algorithm.
130
* @param data Pointer to data to be sorted.
131
* @param n Number of elements to be sorted.
132
* @param e_size Size of one element.
133
* @param cmp Comparator function.
136
void bubblesort(void * data, size_t n, size_t e_size, int (* cmp) (void * a, void * b))
138
uint8_t buf_slot[EBUFSIZE];
139
void * slot = buf_slot;
141
if (e_size > EBUFSIZE) {
142
slot = (void *) malloc(e_size, 0);
145
_bubblesort(data, n, e_size, cmp, slot);
147
if (e_size > EBUFSIZE) {
154
* Apply generic bubblesort algorithm on supplied data, using pre-allocated buffer.
156
* @param data Pointer to data to be sorted.
157
* @param n Number of elements to be sorted.
158
* @param e_size Size of one element.
159
* @param cmp Comparator function.
160
* @param slot Pointer to scratch memory buffer e_size bytes long.
163
void _bubblesort(void * data, size_t n, size_t e_size, int (* cmp) (void * a, void * b), void *slot)
170
for (p = data; p < data + e_size * (n - 1); p = p + e_size) {
171
if (cmp(p, p + e_size) == 1) {
172
memcpy(slot, p, e_size);
173
memcpy(p, p + e_size, e_size);
174
memcpy(p + e_size, slot, e_size);
183
* Comparator returns 1 if a > b, 0 if a == b, -1 if a < b
185
int int_cmp(void * a, void * b)
187
return (* (int *) a > * (int*)b) ? 1 : (*(int *)a < * (int *)b) ? -1 : 0;
190
int uint8_t_cmp(void * a, void * b)
192
return (* (uint8_t *) a > * (uint8_t *)b) ? 1 : (*(uint8_t *)a < * (uint8_t *)b) ? -1 : 0;
195
int uint16_t_cmp(void * a, void * b)
197
return (* (uint16_t *) a > * (uint16_t *)b) ? 1 : (*(uint16_t *)a < * (uint16_t *)b) ? -1 : 0;
200
int uint32_t_cmp(void * a, void * b)
202
return (* (uint32_t *) a > * (uint32_t *)b) ? 1 : (*(uint32_t *)a < * (uint32_t *)b) ? -1 : 0;